Remove json argument and extend Cachable

This commit is contained in:
Rylan Polster 2021-08-09 10:29:55 -04:00
parent a8c03c1cc6
commit eab0f88c3c
No known key found for this signature in database
GPG Key ID: 46A744940CFF4D64
6 changed files with 14 additions and 16 deletions

View File

@ -6,6 +6,7 @@ require "api/bottle"
require "api/cask" require "api/cask"
require "api/formula" require "api/formula"
require "api/versions" require "api/versions"
require "extend/cachable"
module Homebrew module Homebrew
# Helper functions for using Homebrew's formulae.brew.sh API. # Helper functions for using Homebrew's formulae.brew.sh API.
@ -14,24 +15,21 @@ module Homebrew
module API module API
extend T::Sig extend T::Sig
extend Cachable
module_function module_function
API_DOMAIN = "https://formulae.brew.sh/api" API_DOMAIN = "https://formulae.brew.sh/api"
sig { params(endpoint: String, json: T::Boolean).returns(T.any(String, Hash)) } sig { params(endpoint: String).returns(T.any(String, Hash)) }
def fetch(endpoint, json: false) def fetch(endpoint)
return @cache[endpoint] if @cache.present? && @cache.key?(endpoint) return cache[endpoint] if cache.present? && cache.key?(endpoint)
api_url = "#{API_DOMAIN}/#{endpoint}" api_url = "#{API_DOMAIN}/#{endpoint}"
output = Utils::Curl.curl_output("--fail", "--max-time", "5", api_url) 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? raise ArgumentError, "No file found at #{Tty.underline}#{api_url}#{Tty.reset}" unless output.success?
@cache ||= {} cache[endpoint] = JSON.parse(output.stdout)
@cache[endpoint] = if json
JSON.parse(output.stdout)
else
output.stdout
end
rescue JSON::ParserError rescue JSON::ParserError
raise ArgumentError, "Invalid JSON file: #{Tty.underline}#{api_url}#{Tty.reset}" raise ArgumentError, "Invalid JSON file: #{Tty.underline}#{api_url}#{Tty.reset}"
end end

View File

@ -19,7 +19,7 @@ module Homebrew
sig { params(category: String, days: T.any(Integer, String)).returns(Hash) } sig { params(category: String, days: T.any(Integer, String)).returns(Hash) }
def fetch(category, days) 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 end
end end

View File

@ -23,7 +23,7 @@ module Homebrew
sig { params(name: String).returns(Hash) } sig { params(name: String).returns(Hash) }
def fetch(name) def fetch(name)
Homebrew::API.fetch "#{bottle_api_path}/#{name}.json", json: true Homebrew::API.fetch "#{bottle_api_path}/#{name}.json"
end end
sig { params(name: String).returns(T::Boolean) } sig { params(name: String).returns(T::Boolean) }

View File

@ -13,7 +13,7 @@ module Homebrew
sig { params(name: String).returns(Hash) } sig { params(name: String).returns(Hash) }
def fetch(name) def fetch(name)
Homebrew::API.fetch "cask/#{name}.json", json: true Homebrew::API.fetch "cask/#{name}.json"
end end
end end
end end

View File

@ -19,7 +19,7 @@ module Homebrew
sig { params(name: String).returns(Hash) } sig { params(name: String).returns(Hash) }
def fetch(name) def fetch(name)
Homebrew::API.fetch "#{formula_api_path}/#{name}.json", json: true Homebrew::API.fetch "#{formula_api_path}/#{name}.json"
end end
end end
end end

View File

@ -13,17 +13,17 @@ module Homebrew
def formulae def formulae
# The result is cached by Homebrew::API.fetch # The result is cached by Homebrew::API.fetch
Homebrew::API.fetch "versions-formulae.json", json: true Homebrew::API.fetch "versions-formulae.json"
end end
def linux def linux
# The result is cached by Homebrew::API.fetch # The result is cached by Homebrew::API.fetch
Homebrew::API.fetch "versions-linux.json", json: true Homebrew::API.fetch "versions-linux.json"
end end
def casks def casks
# The result is cached by Homebrew::API.fetch # The result is cached by Homebrew::API.fetch
Homebrew::API.fetch "versions-casks.json", json: true Homebrew::API.fetch "versions-casks.json"
end end
sig { params(name: String).returns(T.nilable(PkgVersion)) } sig { params(name: String).returns(T.nilable(PkgVersion)) }