Class: PayTrace::API::Gateway

Inherits:
Object
  • Object
show all
Defined in:
lib/paytrace/api/gateway.rb

Overview

Helper for sending requests

Constant Summary collapse

@@debug =
false
@@last_request =
nil
@@last_response =
nil
@@last_response_object =
nil
@@next_response =
nil
@@raise_exceptions =
true

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection = nil) ⇒ Gateway

Creates a new gateway object, optionally using a supplied connection object



20
21
22
# File 'lib/paytrace/api/gateway.rb', line 20

def initialize(connection = nil)
  @connection = connection || PayTrace.configuration.connection
end

Instance Attribute Details

#connectionObject

:nodoc:



10
11
12
# File 'lib/paytrace/api/gateway.rb', line 10

def connection
  @connection
end

Class Method Details

.debug=(enable) ⇒ Object

Sets or clears a debug flag to enable testing



25
26
27
# File 'lib/paytrace/api/gateway.rb', line 25

def self.debug=(enable)
  @@debug = enable
end

.last_requestObject

Returns the last request sent (as raw text)



38
39
40
# File 'lib/paytrace/api/gateway.rb', line 38

def self.last_request
  @@last_request
end

.last_responseObject

Returns the last response received (as raw text)



43
44
45
# File 'lib/paytrace/api/gateway.rb', line 43

def self.last_response
  @@last_response
end

.last_response_objectObject

Returns the last response object received



48
49
50
# File 'lib/paytrace/api/gateway.rb', line 48

def self.last_response_object
  @@last_response_object
end

.next_response=(next_response) ⇒ Object

Use this to set the raw text of the next response; only used when debug is true



53
54
55
# File 'lib/paytrace/api/gateway.rb', line 53

def self.next_response=(next_response)
  @@next_response = next_response
end

.raise_exceptions=(raise_exceptions) ⇒ Object

Sets or clears a flag to raise exceptions on receiving server errors



58
59
60
# File 'lib/paytrace/api/gateway.rb', line 58

def self.raise_exceptions=(raise_exceptions)
  @@raise_exceptions = raise_exceptions
end

.reset_traceObject

Clears debug data



30
31
32
33
34
35
# File 'lib/paytrace/api/gateway.rb', line 30

def self.reset_trace
  @@last_request = nil
  @@last_response = nil
  @@last_response_object = nil
  @@next_response = nil
end

.send_request(method, params, required = [], optional = []) {|request| ... } ⇒ Object

Helper method to abstract away a common use pattern. Creates a request object, sets parameters, creates a gateway object, sends the request, and returns the response.

Arguments:

  • param_names – the array of parameter names to be set from arguments

  • arguments – the arguments to be set in the request

Yields:

  • (request)


68
69
70
71
72
73
74
75
76
# File 'lib/paytrace/api/gateway.rb', line 68

def self.send_request(method, params, required = [], optional = [])
  request = Request.new
  request.set_param(:method, method)
  request.set_params(params, required, optional)
  yield request if block_given?

  gateway = Gateway.new
  gateway.send_request(request)
end

Instance Method Details

#send_request(request) ⇒ Object

Sends a request object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/paytrace/api/gateway.rb', line 79

def send_request(request)
  @@last_request = request.to_parms_string if @@debug
  unless (@@debug && @@next_response)
    res = @connection.post PayTrace.configuration.url, parmlist: request.to_parms_string
    raw_response = res.body
  else
    raw_response = @@next_response
  end
  
  @@last_response = raw_response
  response = PayTrace::API::Response.new(raw_response)
  @@last_response_object = response

  @@next_response = nil # just to be sure

  if @@raise_exceptions && response.has_errors?
    raise PayTrace::Exceptions::ErrorResponse.new(response)
  else
    response
  end
end