Class: Trustpilot::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, headers: {}, params: {}, verb: 'get') ⇒ Request

Initializes a new request to the API

args:

path: string
headers: { [key: string]: string }
params: { [key: string]: string }
verb: 'delete' | 'get' | 'patch' | 'post' | 'put'


16
17
18
19
20
21
# File 'lib/trustpilot/request.rb', line 16

def initialize path, headers: {}, params: {}, verb: 'get'
  @headers = headers
  @params = params
  @path = path
  @verb = verb
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



7
8
9
# File 'lib/trustpilot/request.rb', line 7

def headers
  @headers
end

#paramsObject (readonly)

Returns the value of attribute params.



7
8
9
# File 'lib/trustpilot/request.rb', line 7

def params
  @params
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/trustpilot/request.rb', line 7

def path
  @path
end

#verbObject (readonly)

Returns the value of attribute verb.



7
8
9
# File 'lib/trustpilot/request.rb', line 7

def verb
  @verb
end

Instance Method Details

#to_http_requestObject

Returns the request as a Net::HTTPRequest



24
25
26
27
28
29
# File 'lib/trustpilot/request.rb', line 24

def to_http_request
  build_request.tap do |req|
    # Set headers
    headers.each { |h, v| req[ h ] = v }
  end
end

#uriObject

Returns the full URI for the request



32
33
34
35
36
37
38
39
# File 'lib/trustpilot/request.rb', line 32

def uri
  @uri ||=
    if path.start_with? 'http'
      URI path
    else
      URI( Trustpilot.api_url + path )
    end
end