Class: ActiveShipping::CanadaPostPWS
- Defined in:
- lib/active_shipping/carriers/canada_post_pws.rb
Constant Summary collapse
- SHIPPING_SERVICES =
{ "DOM.RP" => "Regular Parcel", "DOM.EP" => "Expedited Parcel", "DOM.XP" => "Xpresspost", "DOM.XP.CERT" => "Xpresspost Certified", "DOM.PC" => "Priority", "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.SP.SURF" => "Small Packet USA Surface", "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" }
- ENDPOINT =
production
"https://soa-gw.canadapost.ca/"
- SHIPMENT_MIMETYPE =
"application/vnd.cpc.ncshipment+xml"
- RATE_MIMETYPE =
"application/vnd.cpc.ship.rate+xml"
- TRACK_MIMETYPE =
"application/vnd.cpc.track+xml"
- REGISTER_MIMETYPE =
"application/vnd.cpc.registration+xml"
- LANGUAGE =
{ 'en' => 'en-CA', 'fr' => 'fr-CA' }
- SHIPPING_OPTIONS =
[:d2po, :d2po_office_id, :cov, :cov_amount, :cod, :cod_amount, :cod_includes_shipping, :cod_method_of_payment, :so, :dc, :dns, :pa18, :pa19, :hfp, :lad, :rase, :rts, :aban]
- RATES_OPTIONS =
[:cov, :cov_amount, :cod, :so, :dc, :dns, :pa18, :pa19, :hfp, :lad]
- MAX_WEIGHT =
kg
30
- @@name =
"Canada Post PWS"
Instance Attribute Summary collapse
-
#customer_number ⇒ Object
Returns the value of attribute customer_number.
-
#endpoint ⇒ Object
Returns the value of attribute endpoint.
-
#language ⇒ Object
Returns the value of attribute language.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#platform_id ⇒ Object
Returns the value of attribute platform_id.
Attributes inherited from Carrier
Instance Method Summary collapse
-
#build_rates_request(origin, destination, line_items = [], options = {}, package = nil, services = []) ⇒ Object
rating.
-
#build_shipment_request(origin, destination, package, line_items = [], options = {}) ⇒ Object
options :service => 'DOM.EP' :notification_email :packing_instructions :show_postage_rate :cod, :cod_amount, :insurance, :insurance_amount, :signature_required, :pa18, :pa19, :hfp, :dns, :lad.
- #build_tracking_events(events) ⇒ Object
-
#create_shipment(origin, destination, package, line_items = [], options = {}) ⇒ Object
line_items should be a list of PackageItem's.
- #error_response(response, response_klass) ⇒ Object
- #find_option_details(option_code, options = {}) ⇒ Object
- #find_rates(origin, destination, line_items = [], options = {}, package = nil, services = []) ⇒ Object
- #find_service_options(service_code, country, options = {}) ⇒ Object
- #find_services(country = nil, options = {}) ⇒ Object
- #find_shipment_receipt(shipping_id, options = {}) ⇒ Object
- #find_tracking_info(pin, options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ CanadaPostPWS
constructor
A new instance of CanadaPostPWS.
- #log(msg) ⇒ Object
- #maximum_weight ⇒ Object
- #parse_merchant_details_response(response) ⇒ Object
- #parse_option_response(response) ⇒ Object
- #parse_rates_response(response, origin, destination) ⇒ Object
- #parse_register_token_response(response) ⇒ Object
- #parse_service_options_response(response) ⇒ Object
-
#parse_services_response(response) ⇒ Object
service discovery.
- #parse_shipment_receipt_response(response) ⇒ Object
- #parse_shipment_response(response) ⇒ Object
-
#parse_tracking_response(response) ⇒ Object
tracking.
- #references_node(xml, options) ⇒ Object
- #register_merchant(options = {}) ⇒ Object
- #requirements ⇒ Object
- #retrieve_merchant_details(options = {}) ⇒ Object
- #retrieve_shipment(shipping_id, options = {}) ⇒ Object
- #retrieve_shipping_label(shipping_response, options = {}) ⇒ Object
- #shipment_customs_node(xml, destination, line_items, options) ⇒ Object
- #shipment_destination_node(xml, location, options) ⇒ Object
- #shipment_notification_node(xml, options) ⇒ Object
- #shipment_options_node(xml, options) ⇒ Object
- #shipment_parcel_node(xml, package, options = {}) ⇒ Object
- #shipment_preferences_node(xml, options) ⇒ Object
- #shipment_sender_node(xml, location, options) ⇒ Object
- #shipment_service_code_node(xml, options) ⇒ Object
Methods inherited from Carrier
default_location, #save_request, #timestamp_from_business_day, #valid_credentials?
Constructor Details
#initialize(options = {}) ⇒ CanadaPostPWS
Returns a new instance of CanadaPostPWS.
53 54 55 56 57 58 59 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 53 def initialize( = {}) @language = LANGUAGE[[:language]] || LANGUAGE['en'] @endpoint = [:endpoint] || ENDPOINT @platform_id = [:platform_id] @customer_number = [:customer_number] super() end |
Instance Attribute Details
#customer_number ⇒ Object
Returns the value of attribute customer_number.
51 52 53 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 51 def customer_number @customer_number end |
#endpoint ⇒ Object
Returns the value of attribute endpoint.
51 52 53 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 51 def endpoint @endpoint end |
#language ⇒ Object
Returns the value of attribute language.
51 52 53 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 51 def language @language end |
#logger ⇒ Object
Returns the value of attribute logger.
51 52 53 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 51 def logger @logger end |
#platform_id ⇒ Object
Returns the value of attribute platform_id.
51 52 53 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 51 def platform_id @platform_id end |
Instance Method Details
#build_rates_request(origin, destination, line_items = [], options = {}, package = nil, services = []) ⇒ Object
rating
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 240 def build_rates_request(origin, destination, line_items = [], = {}, package = nil, services = []) line_items = Array(line_items) builder = Nokogiri::XML::Builder.new do |xml| xml.public_send('mailing-scenario', :xmlns => "http://www.canadapost.ca/ws/ship/rate") do customer_number_node(xml, ) contract_id_node(xml, ) quote_type_node(xml, ) expected_mailing_date_node(xml, shipping_date()) if [:shipping_date] (xml, RATES_OPTIONS, ) parcel_node(xml, line_items, package) origin_node(xml, origin) destination_node(xml, destination) services_node(xml, services) unless services.blank? end end builder.to_xml end |
#build_shipment_request(origin, destination, package, line_items = [], options = {}) ⇒ Object
options :service => 'DOM.EP' :notification_email :packing_instructions :show_postage_rate :cod, :cod_amount, :insurance, :insurance_amount, :signature_required, :pa18, :pa19, :hfp, :dns, :lad
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 336 def build_shipment_request(origin, destination, package, line_items = [], = {}) origin = sanitize_location(origin) destination = sanitize_location(destination) builder = Nokogiri::XML::Builder.new do |xml| xml.public_send('non-contract-shipment', :xmlns => "http://www.canadapost.ca/ws/ncshipment") do xml.public_send('delivery-spec') do shipment_service_code_node(xml, ) shipment_sender_node(xml, origin, ) shipment_destination_node(xml, destination, ) (xml, ) shipment_parcel_node(xml, package) shipment_notification_node(xml, ) shipment_preferences_node(xml, ) references_node(xml, ) # optional > user defined custom notes shipment_customs_node(xml, destination, line_items, ) # COD Remittance defaults to sender end end end builder.to_xml end |
#build_tracking_events(events) ⇒ Object
312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 312 def build_tracking_events(events) events.map do |event| date = event.at('event-date').text time = event.at('event-time').text zone = event.at('event-time-zone').text = DateTime.parse("#{date} #{time} #{zone}") time = Time.utc(.utc.year, .utc.month, .utc.day, .utc.hour, .utc.min, .utc.sec) = event.at('event-description').text location = [event.at('event-retail-name'), event.at('event-site'), event.at('event-province')]. reject { |e| e.nil? || e.text.empty? }.join(", ") name = event.at('event-identifier').text ShipmentEvent.new(name, time, location, ) end end |
#create_shipment(origin, destination, package, line_items = [], options = {}) ⇒ Object
line_items should be a list of PackageItem's
88 89 90 91 92 93 94 95 96 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 88 def create_shipment(origin, destination, package, line_items = [], = {}) request_body = build_shipment_request(origin, destination, package, line_items, ) response = ssl_post(create_shipment_url(), request_body, headers(, SHIPMENT_MIMETYPE, SHIPMENT_MIMETYPE)) parse_shipment_response(response) rescue ActiveUtils::ResponseError, ActiveShipping::ResponseError => e error_response(e.response.body, CPPWSShippingResponse) rescue MissingCustomerNumberError CPPWSShippingResponse.new(false, "Missing Customer Number", {}, :carrier => @@name) end |
#error_response(response, response_klass) ⇒ Object
547 548 549 550 551 552 553 554 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 547 def error_response(response, response_klass) doc = Nokogiri.XML(response) doc.remove_namespaces! = doc.xpath('messages/message') = .map { |m| m.at('description').text }.join(", ") code = .map { |m| m.at('code').text }.join(", ") response_klass.new(false, , {}, :carrier => @@name, :code => code) end |
#find_option_details(option_code, options = {}) ⇒ Object
146 147 148 149 150 151 152 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 146 def find_option_details(option_code, = {}) url = endpoint + "rs/ship/option/#{option_code}" response = ssl_get(url, headers(, RATE_MIMETYPE)) parse_option_response(response) rescue ActiveUtils::ResponseError, ActiveShipping::ResponseError => e error_response(e.response.body, CPPWSRateResponse) end |
#find_rates(origin, destination, line_items = [], options = {}, package = nil, services = []) ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 65 def find_rates(origin, destination, line_items = [], = {}, package = nil, services = []) url = endpoint + "rs/ship/price" request = build_rates_request(origin, destination, line_items, , package, services) response = ssl_post(url, request, headers(, RATE_MIMETYPE, RATE_MIMETYPE)) parse_rates_response(response, origin, destination) rescue ActiveUtils::ResponseError, ActiveShipping::ResponseError => e error_response(e.response.body, CPPWSRateResponse) end |
#find_service_options(service_code, country, options = {}) ⇒ Object
139 140 141 142 143 144 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 139 def (service_code, country, = {}) response = ssl_get(services_url(country, service_code), headers(, RATE_MIMETYPE)) (response) rescue ActiveUtils::ResponseError, ActiveShipping::ResponseError => e error_response(e.response.body, CPPWSRateResponse) end |
#find_services(country = nil, options = {}) ⇒ Object
132 133 134 135 136 137 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 132 def find_services(country = nil, = {}) response = ssl_get(services_url(country), headers(, RATE_MIMETYPE)) parse_services_response(response) rescue ActiveUtils::ResponseError, ActiveShipping::ResponseError => e error_response(e.response.body, CPPWSRateResponse) end |
#find_shipment_receipt(shipping_id, options = {}) ⇒ Object
103 104 105 106 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 103 def find_shipment_receipt(shipping_id, = {}) response = ssl_get(shipment_receipt_url(shipping_id, ), headers(, SHIPMENT_MIMETYPE, SHIPMENT_MIMETYPE)) parse_shipment_receipt_response(response) end |
#find_tracking_info(pin, options = {}) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 74 def find_tracking_info(pin, = {}) response = ssl_get(tracking_url(pin), headers(, TRACK_MIMETYPE)) parse_tracking_response(response) rescue ActiveUtils::ResponseError, ActiveShipping::ResponseError => e if e.response error_response(e.response.body, CPPWSTrackingResponse) else CPPWSTrackingResponse.new(false, e., {}, :carrier => @@name) end rescue InvalidPinFormatError CPPWSTrackingResponse.new(false, "Invalid Pin Format", {}, :carrier => @@name) end |
#log(msg) ⇒ Object
556 557 558 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 556 def log(msg) logger.debug(msg) if logger end |
#maximum_weight ⇒ Object
154 155 156 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 154 def maximum_weight Mass.new(MAX_WEIGHT, :kilograms) end |
#parse_merchant_details_response(response) ⇒ Object
497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 497 def parse_merchant_details_response(response) doc = Nokogiri.XML(response) doc.remove_namespaces! raise "No Merchant Info" unless doc.at('merchant-info') raise "No Merchant Info" if doc.root.at('customer-number').blank? = { :customer_number => doc.root.at('customer-number').text, :contract_number => doc.root.at('contract-number').text, :username => doc.root.at('merchant-username').text, :password => doc.root.at('merchant-password').text, :has_default_credit_card => doc.root.at('has-default-credit-card').text == 'true' } CPPWSMerchantDetailsResponse.new(true, "", {}, ) end |
#parse_option_response(response) ⇒ Object
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 218 def parse_option_response(response) doc = Nokogiri.XML(response) doc.remove_namespaces! conflicts = doc.root.xpath('conflicting-options/option-code').map(&:text) prereqs = doc.root.xpath('prerequisite-options/option-code').map(&:text) option = { :code => doc.root.at('option-code').text, :name => doc.root.at('option-name').text, :class => doc.root.at('option-class').text, :prints_on_label => doc.root.at('prints-on-label').text != "false", :qualifier_required => doc.root.at('qualifier-required').text != "false", } option[:conflicting_options] = conflicts if conflicts option[:prerequisite_options] = prereqs if prereqs option[:qualifier_max] = doc.root.at("qualifier-max").text.to_i if doc.root.at("qualifier-max") option end |
#parse_rates_response(response, origin, destination) ⇒ Object
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 259 def parse_rates_response(response, origin, destination) doc = Nokogiri.XML(response) doc.remove_namespaces! raise ActiveShipping::ResponseError, "No Quotes" unless doc.at('price-quotes') rates = doc.root.xpath('price-quote').map do |node| service_name = node.at("service-name").text service_code = node.at("service-code").text total_price = node.at('price-details/due').text expected_date = expected_date_from_node(node) = { :service_code => service_code, :total_price => total_price, :currency => 'CAD', :delivery_range => [expected_date, expected_date] } RateEstimate.new(origin, destination, @@name, service_name, ) end CPPWSRateResponse.new(true, "", {}, :rates => rates) end |
#parse_register_token_response(response) ⇒ Object
487 488 489 490 491 492 493 494 495 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 487 def parse_register_token_response(response) doc = Nokogiri.XML(response) doc.remove_namespaces! raise ActiveShipping::ResponseError, "No Registration Token" unless doc.at('token') = { :token_id => doc.root.at('token-id').text } CPPWSRegisterResponse.new(true, "", {}, ) end |
#parse_service_options_response(response) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 178 def (response) doc = Nokogiri.XML(response) doc.remove_namespaces! service_code = doc.root.at("service-code").text service_name = doc.root.at("service-name").text option_nodes = doc.root.xpath('options/option') = option_nodes.map do |node| option = { :code => node.at("option-code").text, :name => node.at("option-name").text, :required => node.at("mandatory").text != "false", :qualifier_required => node.at("qualifier-required").text != "false", } option[:qualifier_max] = node.at("qualifier-max").text.to_i if node.at("qualifier-max") option end restrictions_node = doc.root.at('restrictions') dimensions_node = restrictions_node.at('dimensional-restrictions') restrictions = { :min_weight => restrictions_node.at("weight-restriction").attributes['min'].value.to_i, :max_weight => restrictions_node.at("weight-restriction").attributes['max'].value.to_i, :min_length => dimensions_node.at("length").attributes['min'].value.to_f, :max_length => dimensions_node.at("length").attributes['max'].value.to_f, :min_height => dimensions_node.at("height").attributes['min'].value.to_f, :max_height => dimensions_node.at("height").attributes['max'].value.to_f, :min_width => dimensions_node.at("width").attributes['min'].value.to_f, :max_width => dimensions_node.at("width").attributes['max'].value.to_f } { :service_code => service_code, :service_name => service_name, :options => , :restrictions => restrictions } end |
#parse_services_response(response) ⇒ Object
service discovery
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 160 def parse_services_response(response) doc = Nokogiri.XML(response) doc.remove_namespaces! service_nodes = doc.xpath('services/service') service_nodes.inject({}) do |result, node| service_code = node.at("service-code").text service_name = node.at("service-name").text service_link = node.at("link").attributes['href'].value service_link_media_type = node.at("link").attributes['media-type'].value result[service_code] = { :name => service_name, :link => service_link, :link_media_type => service_link_media_type } result end end |
#parse_shipment_receipt_response(response) ⇒ Object
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 512 def parse_shipment_receipt_response(response) doc = Nokogiri.XML(response) doc.remove_namespaces! root = doc.at('non-contract-shipment-receipt') cc_details_node = root.at('cc-receipt-details') service_standard_node = root.at('service-standard') receipt = { :final_shipping_point => root.at("final-shipping-point").text, :shipping_point_name => root.at("shipping-point-name").text, :service_code => root.at("service-code").text, :rated_weight => root.at("rated-weight").text.to_f, :base_amount => root.at("base-amount").text.to_f, :pre_tax_amount => root.at("pre-tax-amount").text.to_f, :gst_amount => root.at("gst-amount").text.to_f, :pst_amount => root.at("pst-amount").text.to_f, :hst_amount => root.at("hst-amount").text.to_f, :charge_amount => cc_details_node.at("charge-amount").text.to_f, :currency => cc_details_node.at("currency").text, :expected_transit_days => service_standard_node.at("expected-transit-time").text.to_i, :expected_delivery_date => service_standard_node.at("expected-delivery-date").text } option_nodes = root.xpath('priced-options/priced-option') receipt[:priced_options] = if option_nodes.length > 0 option_nodes.inject({}) do |result, node| result[node.at("option-code").text] = node.at("option-price").text.to_f result end else {} end receipt end |
#parse_shipment_response(response) ⇒ Object
473 474 475 476 477 478 479 480 481 482 483 484 485 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 473 def parse_shipment_response(response) doc = Nokogiri.XML(response) doc.remove_namespaces! raise ActiveShipping::ResponseError, "No Shipping" unless doc.at('non-contract-shipment-info') = { :shipping_id => doc.root.at('shipment-id').text, :tracking_number => doc.root.at('tracking-pin').text, :details_url => doc.root.at_xpath("links/link[@rel='details']")['href'], :label_url => doc.root.at_xpath("links/link[@rel='label']")['href'], :receipt_url => doc.root.at_xpath("links/link[@rel='receipt']")['href'], } CPPWSShippingResponse.new(true, "", {}, ) end |
#parse_tracking_response(response) ⇒ Object
tracking
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 282 def parse_tracking_response(response) doc = Nokogiri.XML(response) doc.remove_namespaces! raise ActiveShipping::ResponseError, "No Tracking" unless doc.at('tracking-detail') events = doc.root.xpath('significant-events/occurrence') shipment_events = build_tracking_events(events) change_date = doc.root.at('changed-expected-date').text expected_date = doc.root.at('expected-delivery-date').text dest_postal_code = doc.root.at('destination-postal-id').text destination = Location.new(:postal_code => dest_postal_code) origin = Location.new(origin_hash_for(doc.root)) = { :carrier => @@name, :service_name => doc.root.at('service-name').text, :expected_date => expected_date.blank? ? nil : Date.parse(expected_date), :changed_date => change_date.blank? ? nil : Date.parse(change_date), :change_reason => doc.root.at('changed-expected-delivery-reason').text.strip, :destination_postal_code => doc.root.at('destination-postal-id').text, :shipment_events => shipment_events, :tracking_number => doc.root.at('pin').text, :origin => origin, :destination => destination, :customer_number => doc.root.at('mailed-by-customer-number').text } CPPWSTrackingResponse.new(true, "", {}, ) end |
#references_node(xml, options) ⇒ Object
417 418 419 420 421 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 417 def references_node(xml, ) # custom values # xml.public_send('references') do # end end |
#register_merchant(options = {}) ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 113 def register_merchant( = {}) url = endpoint + "ot/token" response = ssl_post(url, nil, headers({}, REGISTER_MIMETYPE, REGISTER_MIMETYPE).merge("Content-Length" => "0")) parse_register_token_response(response) rescue ActiveUtils::ResponseError, ActiveShipping::ResponseError => e error_response(e.response.body, CPPWSRegisterResponse) end |
#requirements ⇒ Object
61 62 63 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 61 def requirements [:api_key, :secret] end |
#retrieve_merchant_details(options = {}) ⇒ Object
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 121 def retrieve_merchant_details( = {}) raise MissingTokenIdError unless token_id = [:token_id] url = endpoint + "ot/token/#{token_id}" response = ssl_get(url, headers({}, REGISTER_MIMETYPE, REGISTER_MIMETYPE)) parse_merchant_details_response(response) rescue ActiveUtils::ResponseError, ActiveShipping::ResponseError => e error_response(e.response.body, CPPWSMerchantDetailsResponse) rescue Exception => e raise ResponseError.new(e.) end |
#retrieve_shipment(shipping_id, options = {}) ⇒ Object
98 99 100 101 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 98 def retrieve_shipment(shipping_id, = {}) response = ssl_post(shipment_url(shipping_id, ), nil, headers(, SHIPMENT_MIMETYPE, SHIPMENT_MIMETYPE)) parse_shipment_response(response) end |
#retrieve_shipping_label(shipping_response, options = {}) ⇒ Object
108 109 110 111 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 108 def retrieve_shipping_label(shipping_response, = {}) raise MissingShippingNumberError unless shipping_response && shipping_response.shipping_id ssl_get(shipping_response.label_url, headers(, "application/pdf")) end |
#shipment_customs_node(xml, destination, line_items, options) ⇒ Object
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 423 def shipment_customs_node(xml, destination, line_items, ) return unless destination.country_code != 'CA' xml.public_send('customs') do currency = [:currency] || "CAD" xml.public_send('currency', currency) xml.public_send('conversion-from-cad', [:conversion_from_cad].to_s) if currency != 'CAD' && [:conversion_from_cad] xml.public_send('reason-for-export', 'SOG') # SOG - Sale of Goods xml.public_send('other-reason', [:customs_other_reason]) if [:customs_reason_for_export] && [:customs_other_reason] xml.public_send('additional-customs-info', [:customs_addition_info]) if [:customs_addition_info] xml.public_send('sku-list') do line_items.each do |line_item| kg = '%#2.3f' % [sanitize_weight_kg(line_item.kg)] xml.public_send('item') do xml.public_send('hs-tariff-code', line_item.hs_code) if line_item.hs_code && !line_item.hs_code.empty? xml.public_send('sku', line_item.sku) if line_item.sku && !line_item.sku.empty? xml.public_send('customs-description', line_item.name.slice(0, 44)) xml.public_send('unit-weight', kg) xml.public_send('customs-value-per-unit', '%.2f' % sanitize_price_from_cents(line_item.value)) xml.public_send('customs-number-of-units', line_item.quantity) xml.public_send('country-of-origin', line_item.[:country_of_origin]) if line_item. && line_item.[:country_of_origin] && !line_item.[:country_of_origin].empty? xml.public_send('province-of-origin', line_item.[:province_of_origin]) if line_item. && line_item.[:province_of_origin] && !line_item.[:province_of_origin].empty? end end end end end |
#shipment_destination_node(xml, location, options) ⇒ Object
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 379 def shipment_destination_node(xml, location, ) xml.public_send('destination') do xml.public_send('name', location.name) xml.public_send('company', location.company) if location.company.present? xml.public_send('client-voice-number', location.phone) xml.public_send('address-details') do xml.public_send('address-line-1', location.address1) xml.public_send('address-line-2', location.address2_and_3) unless location.address2_and_3.blank? xml.public_send('city', location.city) xml.public_send('prov-state', location.province) unless location.province.blank? xml.public_send('country-code', location.country_code) xml.public_send('postal-zip-code', location.postal_code) end end end |
#shipment_notification_node(xml, options) ⇒ Object
399 400 401 402 403 404 405 406 407 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 399 def shipment_notification_node(xml, ) return unless [:notification_email] xml.public_send('notification') do xml.public_send('email', [:notification_email]) xml.public_send('on-shipment', true) xml.public_send('on-exception', true) xml.public_send('on-delivery', true) end end |
#shipment_options_node(xml, options) ⇒ Object
395 396 397 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 395 def (xml, ) (xml, SHIPPING_OPTIONS, ) end |
#shipment_parcel_node(xml, package, options = {}) ⇒ Object
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 452 def shipment_parcel_node(xml, package, = {}) weight = sanitize_weight_kg(package.kilograms.to_f) xml.public_send('parcel-characteristics') do xml.public_send('weight', "%#2.3f" % weight) pkg_dim = package.cm if pkg_dim && !pkg_dim.select { |x| x != 0 }.empty? xml.public_send('dimensions') do xml.public_send('length', '%.1f' % ((pkg_dim[2] * 10).round / 10.0)) if pkg_dim.size >= 3 xml.public_send('width', '%.1f' % ((pkg_dim[1] * 10).round / 10.0)) if pkg_dim.size >= 2 xml.public_send('height', '%.1f' % ((pkg_dim[0] * 10).round / 10.0)) if pkg_dim.size >= 1 end xml.public_send('document', false) else xml.public_send('document', true) end xml.public_send('mailing-tube', package.tube?) xml.public_send('unpackaged', package.unpackaged?) end end |
#shipment_preferences_node(xml, options) ⇒ Object
409 410 411 412 413 414 415 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 409 def shipment_preferences_node(xml, ) xml.public_send('preferences') do xml.public_send('show-packing-instructions', [:packing_instructions] || true) xml.public_send('show-postage-rate', [:show_postage_rate] || false) xml.public_send('show-insured-value', true) end end |
#shipment_sender_node(xml, location, options) ⇒ Object
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 363 def shipment_sender_node(xml, location, ) xml.public_send('sender') do xml.public_send('name', location.name) xml.public_send('company', location.company) if location.company.present? xml.public_send('contact-phone', location.phone) xml.public_send('address-details') do xml.public_send('address-line-1', location.address1) xml.public_send('address-line-2', location.address2_and_3) unless location.address2_and_3.blank? xml.public_send('city', location.city) xml.public_send('prov-state', location.province) # xml.public_send('country-code', location.country_code) xml.public_send('postal-zip-code', location.postal_code) end end end |
#shipment_service_code_node(xml, options) ⇒ Object
359 360 361 |
# File 'lib/active_shipping/carriers/canada_post_pws.rb', line 359 def shipment_service_code_node(xml, ) xml.public_send('service-code', [:service]) end |