Class: TNSPayments::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, method, path, payload = nil) ⇒ Request

Returns a new instance of Request.



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

def initialize connection, method, path, payload = nil
  self.connection = connection
  self.method     = method
  self.path       = path
  self.payload    = payload
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



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

def connection
  @connection
end

#methodObject

Returns the value of attribute method.



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

def method
  @method
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

#payloadObject

Returns the value of attribute payload.



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

def payload
  @payload
end

Instance Method Details

#headersObject

Public: A hash representing the headers for the request.

Returns a hash of headers.



18
19
20
21
22
23
24
# File 'lib/tns_payments/request.rb', line 18

def headers
  headers = {}
  headers[:accept]        = '*/*'
  headers[:content_type]  = 'Application/json;charset=UTF-8'
  headers[:Authorization] = encode_credentials
  headers
end

#performObject

Public: Execute the request.

Returns a response object.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tns_payments/request.rb', line 29

def perform
  args = {
    :method       => method,
    :url          => url,
    :headers      => headers,
    :timeout      => 120,
    :open_timeout => 30
  }
  args[:payload] = payload.to_json if payload

  Response.new RestClient::Request.execute(args)
rescue RestClient::Exception => e
  response = e.response

  if e.is_a?(RestClient::RequestTimeout)
    response = JSON.generate({:error => {:cause => 'REQUEST_TIMEDOUT'}})
  end

  ErrorResponse.new response
end