Sha256: 419812a0e85844f19181ef3235b95fa8d1db22c3fd5157abe180b8fc45eb1dc3

Contents?: true

Size: 1.24 KB

Versions: 3

Compression:

Stored size: 1.24 KB

Contents

# 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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bundler-alive-0.1.2 lib/bundler/alive/client/gems_api.rb
bundler-alive-0.1.1 lib/bundler/alive/client/gems_api.rb
bundler-alive-0.1.0 lib/bundler/alive/client/gems_api.rb