class PayTrace::API::Gateway
Helper for sending requests
Public Class Methods
Sets or clears a debug flag to enable testing
# File lib/paytrace/api/gateway.rb, line 25 def self.debug=(enable) @@debug = enable end
Returns the last request sent (as raw text)
# File lib/paytrace/api/gateway.rb, line 38 def self.last_request @@last_request end
Returns the last response received (as raw text)
# File lib/paytrace/api/gateway.rb, line 43 def self.last_response @@last_response end
Returns the last response object received
# File lib/paytrace/api/gateway.rb, line 48 def self.last_response_object @@last_response_object end
Creates a new gateway object, optionally using a supplied connection object
# File lib/paytrace/api/gateway.rb, line 20 def initialize(connection = nil) @connection = connection || PayTrace.configuration.connection end
Use this to set the raw text of the next response; only used when debug is true
# File lib/paytrace/api/gateway.rb, line 53 def self.next_response=(next_response) @@next_response = next_response end
Sets or clears a flag to raise exceptions on receiving server errors
# File lib/paytrace/api/gateway.rb, line 58 def self.raise_exceptions=(raise_exceptions) @@raise_exceptions = raise_exceptions end
Clears debug data
# 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
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
# 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
Public Instance Methods
Sends a request object
# 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