Sha256: 3ee1e60906ac025f8ba3f239eb243981015dbda40b5b647f59ee9215e011f209

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

require "uri"
require "net/http"

module Foobara
  module RubyGemsApi
    class Search < Foobara::Command
      API_URL = "https://rubygems.org/api/v1/search.json".freeze

      inputs do
        query :string, :required
      end

      result [Gem]

      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 = { query: }
      end

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

      def issue_http_request
        uri = URI(API_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 #{API_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/search.rb