Class | EWS::Transporter |
In: |
lib/ews/transporter.rb
|
Parent: | Object |
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.
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:
:server_cert the path to the server's certificate :issuer_cert the path to the certificate of the issuer of the server certificate :transport_type the transport_type for this transporter (defaults to :rest)
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.
# File lib/ews/transporter.rb, line 32 32: def initialize(url = "https://api.e-xact.com/", options = {}) 33: @url = URI.parse(url.gsub(/\/$/,'')) 34: base = File.dirname(__FILE__) 35: @server_cert = options[:server_cert] || base+"/../../certs/exact.cer" 36: @issuer_cert = options[:issuer_cert] || base+"/../../certs/equifax_ca.cer" 37: @transport_type = options[:transport_type] || :json 38: end
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 |
# File lib/ews/transporter.rb, line 44 44: def submit(transaction, transport_type = nil) 45: raise "Request not supplied" if transaction.nil? 46: return false unless transaction.valid? 47: 48: transport_type ||= @transport_type 49: 50: raise "Transport type #{transport_type} is not supported" unless @@transport_types.include? transport_type 51: 52: transport_details = @@transport_types[transport_type] 53: 54: request = build_http_request(transaction, transport_type, transport_details[:suffix]) 55: request.basic_auth(transaction.gateway_id, transaction.password) 56: request.add_field "Accept", transport_details[:content_type] 57: request.add_field "User-Agent", "Ruby" 58: request.add_field "Content-type", "#{transport_details[:content_type]}; charset=UTF-8" 59: 60: response = get_connection.request(request) 61: 62: case response 63: when Net::HTTPSuccess then EWS::Transaction::Mapping.send "#{transport_type}_to_response", response.body 64: else 65: r = ::EWS::Transaction::Response.new 66: if(transport_type == :soap) 67: # we may have a SOAP Fault 68: r = EWS::Transaction::Mapping.send "#{transport_type}_to_response", response.body 69: end 70: 71: # SOAP Fault may already have populated the error_number etc. 72: unless r.error_number 73: # populate the error number and description 74: r.error_number = response.code.to_i 75: r.error_description = response.message 76: end 77: 78: r 79: end 80: 81: end