Class: RandomGem::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/random_gem/request.rb

Constant Summary collapse

HOST =
"https://rubygems.org"
PATH =
"/api/v1/search"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keyword:, page:) ⇒ Request

Returns a new instance of Request.



12
13
14
15
# File 'lib/random_gem/request.rb', line 12

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

Instance Attribute Details

#keywordObject (readonly)

Returns the value of attribute keyword.



10
11
12
# File 'lib/random_gem/request.rb', line 10

def keyword
  @keyword
end

#pageObject (readonly)

Returns the value of attribute page.



10
11
12
# File 'lib/random_gem/request.rb', line 10

def page
  @page
end

Instance Method Details

#doObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/random_gem/request.rb', line 17

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