Class: CanadaPost::Request::Base
- Inherits:
-
Object
- Object
- CanadaPost::Request::Base
- Includes:
- Helpers, HTTParty
- Defined in:
- lib/canada_post/request/base.rb
Direct Known Subclasses
Constant Summary collapse
- TEST_URL =
CanadaPost API Test URL
"https://ct.soa-gw.canadapost.ca"
- PRODUCTION_URL =
CanadaPost API Production URL
"https://soa-gw.canadapost.ca"
- TEST_CONTRACT_ID =
CanadaPost API TEST CONTRACT ID
"0042708517"
- OPTION_CODES =
List of available Option Codes SO - Signature COV - Coverage (requires qualifier) COD - COD (requires qualifier) PA18 - Proof of Age Required - 18 PA19 - Proof of Age Required - 19 HFP - Card for pickup DNS - Do not safe drop LAD - Leave at door - do not card
["SO", "COV", "COD", "PA18", "PA19", "HFP", "DNS", "LAD"]
- SERVICE_CODES =
List of available Service Codes DOM.RP - Regular Parcel DOM.EP - Expedited Parcel DOM.XP - Xpresspost DOM.XP.CERT - Xpresspost Certified DOM.PC - Priority DOM.DT - Delivered Tonight DOM.LIB - Library Books USA.EP - Expedited Parcel USA USA.PW.ENV - Priority Worldwide Envelope USA USA.PW.PAK - Priority Worldwide pak USA USA.PW.Parcel - Priority Worldwide Parcel USA USA.SP.AIR - Small Packet USA Air USA.TP - Tracked Package - USA USA.TP.LVM - Tracked Package - USA (LVM) (large volume mailers) USA.XP - Xpresspost USA INT.XP - Xpresspost international INT.IP.AIR - International Parcel Air INT.IP.SURF - International Parcel Surface INT.PW.ENV - Priority Worldwide Envelope Int’l INT.PW.PAK - Priority Worldwide pak Int’l INT.PW.PARCEL - Priority Worldwide Parcel Int’l INT.SP.AIR - Small Packet International Air INT.SP.SURF - Small Packet International Surface INT.TP - Tracked Package - International
{ "DOM.RP" => 'Regular Parcel', "DOM.EP" => 'Expedited Parcel', "DOM.XP" => 'Xpresspost', "DOM.XP.CERT" => 'Xpresspost Certified', "DOM.PC" => 'Priority', "DOM.DT" => 'Delivered Tonight', "DOM.LIB" => 'Library Books', "USA.EP" => 'Expedited Parcel USA', "USA.PW.ENV" => 'Priority Worldwide Envelope USA', "USA.PW.PAK" => 'Priority Worldwide pak USA', "USA.PW.PARCEL" => 'Priority Worldwide Parcel USA', "USA.SP.AIR" => 'Small Packet USA Air', "USA.TP" => 'Tracked Package - USA', "USA.TP.LVM" => 'Tracked Package - USA (LVM) (large volume mailers)', "USA.XP" => 'Xpresspost USA', "INT.XP" => 'Xpresspost international', "INT.IP.AIR" => 'International Parcel Air', "INT.IP.SURF" => 'International Parcel Surface', "INT.PW.ENV" => "Priority Worldwide Envelope Int'l", "INT.PW.PAK" => "Priority Worldwide pak Int'l", "INT.PW.PARCEL" => "Priority Worldwide Parcel Int'l", "INT.SP.AIR" => 'Small Packet International Air', "INT.SP.SURF" => 'Small Packet International Surface', "INT.TP" => 'Tracked Package - International' }
Instance Method Summary collapse
- #api_url ⇒ Object
- #build_xml ⇒ Object
-
#client(url, body, headers) ⇒ Object
Sends POST request to CanadaPost API and parse the response, a class object (Shipment, Rate…) is created if the response is successful.
-
#initialize(credentials, options = {}) ⇒ Base
constructor
A new instance of Base.
-
#parse_response(response) ⇒ Object
Parse response, convert keys to underscore symbols.
-
#process_request ⇒ Object
def initialize(credentials, options={}) requires!(options, :shipper, :recipient, :package) @credentials = credentials @shipper, @recipient, @package, @service_type = options, options, options, options @authorization = { username: @credentials.username, password: @credentials.password } @customer_number = @credentials.customer_number end.
- #process_response(api_response) ⇒ Object
-
#sanitize_response_keys(response) ⇒ Object
Recursively sanitizes the response object by cleaning up any hash keys.
Constructor Details
#initialize(credentials, options = {}) ⇒ Base
Returns a new instance of Base.
85 86 87 88 89 |
# File 'lib/canada_post/request/base.rb', line 85 def initialize(credentials, = {}) @credentials = credentials @authorization = {username: @credentials.username, password: @credentials.password} @customer_number = @credentials.customer_number end |
Instance Method Details
#api_url ⇒ Object
114 115 116 |
# File 'lib/canada_post/request/base.rb', line 114 def api_url @credentials.mode == "production" ? PRODUCTION_URL : TEST_URL end |
#build_xml ⇒ Object
118 119 120 |
# File 'lib/canada_post/request/base.rb', line 118 def build_xml raise NotImplementedError, "Override #build_xml in subclass" end |
#client(url, body, headers) ⇒ Object
Sends POST request to CanadaPost API and parse the response, a class object (Shipment, Rate…) is created if the response is successful
105 106 107 108 109 110 111 112 |
# File 'lib/canada_post/request/base.rb', line 105 def client(url, body, headers) self.class.post( url, body: body, headers: headers, basic_auth: @authorization ) end |
#parse_response(response) ⇒ Object
Parse response, convert keys to underscore symbols
123 124 125 126 |
# File 'lib/canada_post/request/base.rb', line 123 def parse_response(response) response = Hash.from_xml(response.parsed_response.gsub("\n", "")) if response.parsed_response.is_a? String response = sanitize_response_keys(response) end |
#process_request ⇒ Object
def initialize(credentials, options={})
requires!(, :shipper, :recipient, :package)
@credentials = credentials
@shipper, @recipient, @package, @service_type = [:shipper], [:recipient], [:package], [:service_type]
@authorization = { username: @credentials.username, password: @credentials.password }
@customer_number = @credentials.customer_number
end
99 100 101 |
# File 'lib/canada_post/request/base.rb', line 99 def process_request raise NotImplementedError, "Override #process_request in subclass" end |
#process_response(api_response) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/canada_post/request/base.rb', line 128 def process_response(api_response) shipping_response = {errors: ''} response = parse_response(api_response) if response[:messages].present? response[:messages].each do |key, | shipping_response[:errors] << [:description].split('}').last end return shipping_response end return response end |
#sanitize_response_keys(response) ⇒ Object
Recursively sanitizes the response object by cleaning up any hash keys.
142 143 144 145 146 147 148 149 150 |
# File 'lib/canada_post/request/base.rb', line 142 def sanitize_response_keys(response) if response.is_a?(Hash) response.inject({}) { |result, (key, value)| result[underscorize(key).to_sym] = sanitize_response_keys(value); result } elsif response.is_a?(Array) response.collect { |result| sanitize_response_keys(result) } else response end end |