class SimpleShipping::Abstract::Client
Abstract
class which provides common interfaces for the next concrete clients:
-
{Fedex::Client}
-
{Ups::Client}
Public Class Methods
Create an instance of a client.
Parameters:¶ ↑
* credentials - a hash with credentials.
# File lib/simple_shipping/abstract/client.rb, line 38 def initialize(options) @options = options.dup @live = options.delete(:live) @debug = options.delete(:debug) @debug_path = options.delete(:debug_path) credentials = options.delete(:credentials) validate_credentials(credentials) @credentials = OpenStruct.new(credentials) @client = Savon.client(client_options(options)) end
Set the production endpoint.
@param address [String]
# File lib/simple_shipping/abstract/client.rb, line 24 def self.set_production_address(address) self.production_address = address end
Set credentials which should be validated.
# File lib/simple_shipping/abstract/client.rb, line 12 def self.set_required_credentials(*args) self.required_credentials = args end
Set the testing endpoint.
@param address [String]
# File lib/simple_shipping/abstract/client.rb, line 31 def self.set_testing_address(address) self.testing_address = address end
Set the WSDL document used by Savon.
# File lib/simple_shipping/abstract/client.rb, line 17 def self.set_wsdl_document(wsdl_path) self.wsdl_document = wsdl_path end
Protected Instance Methods
@param [Hash] options Savon client options @return [Hash{Symbol => Object}] Savon client options
# File lib/simple_shipping/abstract/client.rb, line 53 def client_options(options = {}) endpoint = @live ? self.class.production_address : self.class.testing_address options.symbolize_keys.reverse_merge( :wsdl => wsdl_document, :endpoint => endpoint ) end
Private Instance Methods
Build the {Shipment shipment} model.
# File lib/simple_shipping/abstract/client.rb, line 73 def create_shipment(shipper, recipient, package, opts = {}) shipment = SimpleShipping::Shipment.new( :shipper => shipper, :recipient => recipient, :package => package) shipment.payor = opts[:payor] if opts[:payor] shipment end
Write the request information to request.xml.
@param soap [Savon::HTTPRequest]
# File lib/simple_shipping/abstract/client.rb, line 86 def log_request(soap) log_soap("request", soap) end
Write the response information to response.xml.
@param [Savon::Response] soap
# File lib/simple_shipping/abstract/client.rb, line 94 def log_response(soap) log_soap("response", soap) end
Write the request/response to .xml file.
@param name [String] file name without .xml @param soap [Savon::HTTPRequest, Savon::Response]
# File lib/simple_shipping/abstract/client.rb, line 103 def log_soap(name, soap) if @debug path = File.join(@debug_path, "#{name}.xml") File.open(path, 'w') {|f| f.write soap.to_xml} end end
Validate that all required credentials are passed.
# File lib/simple_shipping/abstract/client.rb, line 65 def validate_credentials(credentials) credentials.assert_valid_keys(required_credentials) missing = required_credentials - credentials.keys raise(Error.new "The next credentials are missing for #{self}: #{missing.join(', ')}") unless missing.empty? end