Sha256: f7e6709832bfa5cab78866dff1702c20e8616af5b3ca9e039074090845fb3f55

Contents?: true

Size: 794 Bytes

Versions: 2

Compression:

Stored size: 794 Bytes

Contents

# frozen_string_literal: true

require 'json'

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

    class << self
      def gem_info(name, version)
        parse(Faraday.get(SERVER + format(GET_GEM, name, version)), name)
      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.8.0 lib/unwrappr/ruby_gems.rb
unwrappr-0.7.0 lib/unwrappr/ruby_gems.rb