Class: EWS::Transporter

Inherits:
Object
  • Object
show all
Includes:
CertificateHelper
Defined in:
lib/ews/transporter.rb

Overview

A Transporter is responsible for communicating with the E-xact Web Service in whichever dialect is chosen by the user. The available options are:

:json     REST with JSON payload
:rest     REST with XML payload (default)
:soap     SOAP

The Transporter will connect to the service, using SSL if required, and will encode Reqests to send to the service, and decode Responses received from the service.

Once configured to connect to a particular service, it can be used repeatedly to send as many transactions as required.

Constant Summary

Constants included from CertificateHelper

CertificateHelper::EXACT_ISSUER_CERT_FILE, CertificateHelper::EXACT_SERVER_CERT_FILE

Instance Attribute Summary

Attributes included from CertificateHelper

#client_cert, #client_key, #issuer_cert_file, #server_cert

Instance Method Summary collapse

Constructor Details

#initialize(url = "https://api.e-xact.com", options = {}) ⇒ Transporter

Initialize a Transporter.

You can specify the URL you would like the Transporter to connect to, although it defaults to api.e-xact.com, the location of our transaction processing web service.

You can also specify a hash of options as follows:

:transport_type   the transport_type for this transporter (defaults to :rest)
:server_cert      the path to the server's certificate file (defaults to E-xact's Server Cert)
:issuer_cert      the path to the issuer's certificate file (defaults to E-xact's Issuer's Cert)
:client_cert      the path to the client's X.509 certificate file (optional)
:client_key       the path to the client's key file (optional)

The default certificates are those required to connect to api.e-xact.com and the default transport_type is :rest. The default transport_type can be overridden on a per-transaction basis, if you choose to do so, by specifying it as a parameter to the submit method.



37
38
39
40
41
42
# File 'lib/ews/transporter.rb', line 37

def initialize(url = "https://api.e-xact.com", options = {})
  @url = URI.parse(url.gsub(/\/$/,''))
  @transport_type = options[:transport_type] || :rest
  
  configure_certificates(options)
end

Instance Method Details

#submit(transaction, transport_type = nil) ⇒ Object

Submit a transaction request to the server

transaction

the Request object to encode for transmission to the server

transport_type

(optional) the transport type to use for this transaction only. If it is not specified, the Transporter’s transport type will be used

Raises:

  • (ArgumentError)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ews/transporter.rb', line 48

def submit(transaction, transport_type = nil)
  raise ArgumentError, "Request not supplied" if transaction.nil?
  return false unless transaction.valid?

  transport_type ||= @transport_type

  raise ArgumentError, "Transport type #{transport_type} is not supported" unless @@transport_types.include? transport_type

  transport_details = @@transport_types[transport_type]
  
  request = build_http_request(transaction, transport_type, transport_details[:suffix])
  request.basic_auth(transaction.gateway_id, transaction.password)
  request.add_field "Accept", transport_details[:content_type]
  request.add_field "User-Agent", "exact4r v1.7"
  request.add_field "Content-type", "#{transport_details[:content_type]}; charset=UTF-8"

  response = get_connection.request(request)

  case response
  when Net::HTTPSuccess then EWS::Transaction::Mapping.send "#{transport_type}_to_response", response.body
  else
    r = ::EWS::Transaction::Response.new
    if(transport_type == :soap)
      # we may have a SOAP Fault
      r = EWS::Transaction::Mapping.send "#{transport_type}_to_response", response.body
    end

    # SOAP Fault may already have populated the error_number etc.
    unless r.error_number
      # populate the error number and description
      r.error_number = response.code.to_i
      r.error_description = response.message
    end
    
    r
  end

end