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/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

View File

@ -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

View File

@ -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) }

View File

@ -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

View File

@ -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

View File

@ -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)) }