# frozen_string_literal: true require "faraday" require "json" module Bundler module Alive module Client # # API Client for RubyGems.org API # # @see https://guides.rubygems.org/rubygems-org-api/ # class GemsApi # # Not found in rubygems.org error # class NotFound < StandardError end # # Returns repository url # # @param [String] gem_name # # @return [SourceCodeRepositoryUrl] # def get_repository_url(gem_name) url = api_url(gem_name) response = connection.get(url) raise NotFound, "#{gem_name} is not found in gems.org." if response.status == 404 body = JSON.parse(response.body) raw_url = body["source_code_uri"] || body["homepage_uri"] SourceCodeRepositoryUrl.new(raw_url) end private def api_url(gem_name) "https://rubygems.org/api/v1/gems/#{gem_name}.json" end def connection return @connection if instance_variable_defined?(:@connection) @connection = Faraday.new do |connection| connection.adapter :net_http end end end end end end