Class: Pastvu::Request

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, params) ⇒ Request

Returns a new instance of Request.



8
9
10
11
12
13
# File 'lib/pastvu/request.rb', line 8

def initialize(method, params)
  @method = method
  @params = params

  request(build_uri)
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



6
7
8
# File 'lib/pastvu/request.rb', line 6

def response
  @response
end

Class Method Details

.download(uri, path) ⇒ Object



15
16
17
18
19
# File 'lib/pastvu/request.rb', line 15

def self.download(uri, path)
  download = URI.parse(uri).open("User-Agent" => Pastvu.config.user_agent)
  IO.copy_stream(download, path)
  File.new(path)
end

Instance Method Details

#build_uriObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/pastvu/request.rb', line 27

def build_uri
  query = {
    method: @method,
    params: JSON.dump(@params)
  }

  URI::HTTPS.build(host: Pastvu.config.host,
                   path: Pastvu.config.path,
                   query: URI.encode_www_form(query))
end

#request(uri) ⇒ Object



21
22
23
24
25
# File 'lib/pastvu/request.rb', line 21

def request(uri)
  Net::HTTP.get_response(uri, "User-Agent" => Pastvu.config.user_agent) do |response|
    @response = response.body
  end
end