Sha256: d6ffe7071b397cf8e1391aba31a1928d242f19189763935a4bcdc3ef91d92579

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

require "uri"
require "net/http"

module Foobara
  module RubyGemsApi
    class GetOwners < Foobara::Command
      API_URL = ->(gem_name) { "https://rubygems.org/api/v1/gems/#{gem_name}/owners.json" }

      inputs do
        gem_name :string, :required
      end

      result [User]

      def execute
        build_request_body
        build_request_headers
        issue_http_request
        parse_response
        build_gems

        gems
      end

      attr_accessor :request_body, :request_headers, :response, :response_body, :gems

      def build_request_body
        self.request_body = {}
      end

      def build_request_headers
        self.request_headers = {
          "Content-Type" => "application/json"
        }
      end

      def url
        API_URL.call(gem_name)
      end

      def issue_http_request
        uri = URI(url)
        uri.query = URI.encode_www_form(request_body)
        self.response = Net::HTTP.get_response(uri, request_headers)
      end

      def parse_response
        json = if response.is_a?(Net::HTTPSuccess)
                 response.body
               else
                 # :nocov:
                 raise "Could not successfully hit #{url}: " \
                       "#{response.code} #{response.message}"
                 # :nocov:
               end

        self.response_body = JSON.parse(json)
      end

      def build_gems
        self.gems = response_body
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
foobara-ruby-gems-api-0.0.1 src/foobara/ruby_gems_api/get_owners.rb