Sha256: 4b09efda71cd6e1d11da14de9cb7edeb3f537abe97c179a85bd5e34c21fc6d92

Contents?: true

Size: 653 Bytes

Versions: 2

Compression:

Stored size: 653 Bytes

Contents

require 'net/http'
require 'rubygems'
require 'open-uri'
require 'json'

module RandomGem
  class Request
    HOST = "https://rubygems.org"
    PATH = "/api/v1/search"
    attr_reader :keyword, :page

    def initialize(keyword:, page:)
      @keyword = keyword
      @page = page
    end

    def do
      uri = URI("#{HOST}#{PATH}")
      params = { query: keyword, page: page }
      uri.query = URI.encode_www_form(params)

      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = true

      res = http.get(uri.request_uri)
      respond(res: res)
    end

    private

    def respond(res:)
      JSON.parse(res.body)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
random_gem-0.1.1 lib/random_gem/request.rb
random_gem-0.1.0 lib/random_gem/request.rb