| 
									
										
										
										
											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" | 
					
						
							| 
									
										
										
										
											2021-08-09 10:29:55 -04:00
										 |  |  | 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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-09 10:29:55 -04:00
										 |  |  |     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) | 
					
						
							| 
									
										
										
										
											2021-08-09 10:29:55 -04:00
										 |  |  |       return cache[endpoint] if cache.present? && cache.key?(endpoint) | 
					
						
							| 
									
										
										
										
											2021-08-06 02:30:44 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |       api_url = "#{API_DOMAIN}/#{endpoint}" | 
					
						
							| 
									
										
										
										
											2021-09-06 22:56:25 -04:00
										 |  |  |       output = Utils::Curl.curl_output("--fail", api_url, max_time: 5) | 
					
						
							| 
									
										
										
										
											2021-08-06 02:30:44 -04:00
										 |  |  |       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 |