Sha256: 891731ad23a2d3f0418e3647024ccc88ff577364d426b1f429d5464cb4617e8a

Contents?: true

Size: 869 Bytes

Versions: 2

Compression:

Stored size: 869 Bytes

Contents

# frozen_string_literal: true

require 'json'

module Unwrappr
  # A wrapper around RubyGems' API
  module RubyGems
    SERVER = 'https://rubygems.org'
    GET_GEM = '/api/v1/gems/%s.json'

    class << self
      def gem_info(name)
        parse(Faraday.get(SERVER + GET_GEM % name), name)
      end

      def try_get_source_code_uri(gem_name)
        Unwrappr::RubyGems.gem_info(gem_name)&.source_code_uri
      end

      private

      def parse(response, name)
        case response.status
        when 200
          JSON.parse(response.body, object_class: OpenStruct)
        when 404
          nil
        else
          warn(error_message(response: response, name: name))
        end
      end

      def error_message(response:, name:)
        "Rubygems response for #{name}: "\
        "HTTP #{response.status}: #{response.body}"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
unwrappr-0.3.4 lib/unwrappr/ruby_gems.rb
unwrappr-0.3.3 lib/unwrappr/ruby_gems.rb