diff --git a/Library/Homebrew/api.rb b/Library/Homebrew/api.rb index ac21298f64..38d2748db5 100644 --- a/Library/Homebrew/api.rb +++ b/Library/Homebrew/api.rb @@ -6,6 +6,7 @@ require "api/bottle" require "api/cask" require "api/formula" require "api/versions" +require "extend/cachable" module Homebrew # Helper functions for using Homebrew's formulae.brew.sh API. @@ -14,24 +15,21 @@ module Homebrew module API extend T::Sig + extend Cachable + module_function API_DOMAIN = "https://formulae.brew.sh/api" - sig { params(endpoint: String, json: T::Boolean).returns(T.any(String, Hash)) } - def fetch(endpoint, json: false) - return @cache[endpoint] if @cache.present? && @cache.key?(endpoint) + sig { params(endpoint: String).returns(T.any(String, Hash)) } + def fetch(endpoint) + return cache[endpoint] if cache.present? && cache.key?(endpoint) api_url = "#{API_DOMAIN}/#{endpoint}" output = Utils::Curl.curl_output("--fail", "--max-time", "5", api_url) raise ArgumentError, "No file found at #{Tty.underline}#{api_url}#{Tty.reset}" unless output.success? - @cache ||= {} - @cache[endpoint] = if json - JSON.parse(output.stdout) - else - output.stdout - end + cache[endpoint] = JSON.parse(output.stdout) rescue JSON::ParserError raise ArgumentError, "Invalid JSON file: #{Tty.underline}#{api_url}#{Tty.reset}" end diff --git a/Library/Homebrew/api/analytics.rb b/Library/Homebrew/api/analytics.rb index f7967d4d1d..bdec017f10 100644 --- a/Library/Homebrew/api/analytics.rb +++ b/Library/Homebrew/api/analytics.rb @@ -19,7 +19,7 @@ module Homebrew sig { params(category: String, days: T.any(Integer, String)).returns(Hash) } def fetch(category, days) - Homebrew::API.fetch "#{analytics_api_path}/#{category}/#{days}d.json", json: true + Homebrew::API.fetch "#{analytics_api_path}/#{category}/#{days}d.json" end end end diff --git a/Library/Homebrew/api/bottle.rb b/Library/Homebrew/api/bottle.rb index 1e6a595d86..54f32cdc30 100644 --- a/Library/Homebrew/api/bottle.rb +++ b/Library/Homebrew/api/bottle.rb @@ -23,7 +23,7 @@ module Homebrew sig { params(name: String).returns(Hash) } def fetch(name) - Homebrew::API.fetch "#{bottle_api_path}/#{name}.json", json: true + Homebrew::API.fetch "#{bottle_api_path}/#{name}.json" end sig { params(name: String).returns(T::Boolean) } diff --git a/Library/Homebrew/api/cask.rb b/Library/Homebrew/api/cask.rb index c1dd4ddcd6..8a89ae0187 100644 --- a/Library/Homebrew/api/cask.rb +++ b/Library/Homebrew/api/cask.rb @@ -13,7 +13,7 @@ module Homebrew sig { params(name: String).returns(Hash) } def fetch(name) - Homebrew::API.fetch "cask/#{name}.json", json: true + Homebrew::API.fetch "cask/#{name}.json" end end end diff --git a/Library/Homebrew/api/formula.rb b/Library/Homebrew/api/formula.rb index 329f80bfde..4582ba7a76 100644 --- a/Library/Homebrew/api/formula.rb +++ b/Library/Homebrew/api/formula.rb @@ -19,7 +19,7 @@ module Homebrew sig { params(name: String).returns(Hash) } def fetch(name) - Homebrew::API.fetch "#{formula_api_path}/#{name}.json", json: true + Homebrew::API.fetch "#{formula_api_path}/#{name}.json" end end end diff --git a/Library/Homebrew/api/versions.rb b/Library/Homebrew/api/versions.rb index 0ad01b93c4..b4f971b4a1 100644 --- a/Library/Homebrew/api/versions.rb +++ b/Library/Homebrew/api/versions.rb @@ -13,17 +13,17 @@ module Homebrew def formulae # The result is cached by Homebrew::API.fetch - Homebrew::API.fetch "versions-formulae.json", json: true + Homebrew::API.fetch "versions-formulae.json" end def linux # The result is cached by Homebrew::API.fetch - Homebrew::API.fetch "versions-linux.json", json: true + Homebrew::API.fetch "versions-linux.json" end def casks # The result is cached by Homebrew::API.fetch - Homebrew::API.fetch "versions-casks.json", json: true + Homebrew::API.fetch "versions-casks.json" end sig { params(name: String).returns(T.nilable(PkgVersion)) }