43 lines
1.1 KiB
Ruby
Raw Normal View History

2021-08-06 02:30:44 -04:00
# typed: false
# frozen_string_literal: true
require "api/analytics"
require "api/bottle"
require "api/cask"
2021-08-06 11:56:42 -04:00
require "api/cask-source"
2021-08-06 02:30:44 -04:00
require "api/formula"
require "api/versions"
require "extend/cachable"
2021-08-06 02:30:44 -04:00
module Homebrew
# Helper functions for using Homebrew's formulae.brew.sh API.
#
# @api private
module API
extend T::Sig
extend Cachable
2021-08-06 02:30:44 -04:00
module_function
API_DOMAIN = "https://formulae.brew.sh/api"
2021-08-06 11:56:42 -04:00
sig { params(endpoint: String, json: T::Boolean).returns(T.any(String, Hash)) }
def fetch(endpoint, json: true)
return cache[endpoint] if cache.present? && cache.key?(endpoint)
2021-08-06 02:30:44 -04:00
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?
2021-08-06 11:56:42 -04:00
cache[endpoint] = if json
JSON.parse(output.stdout)
else
output.stdout
end
2021-08-06 02:30:44 -04:00
rescue JSON::ParserError
raise ArgumentError, "Invalid JSON file: #{Tty.underline}#{api_url}#{Tty.reset}"
end
end
end