Class: CanadaPost::Request::Base

Inherits:
Object
  • Object
show all
Includes:
Helpers, HTTParty
Defined in:
lib/canada_post/request/base.rb

Direct Known Subclasses

Rate

Constant Summary collapse

TEST_URL =

CanadaPost API Test URL

"https://ct.soa-gw.canadapost.ca"
PRODUCTION_URL =

CanadaPost API Production URL

"https://https://soa-gw.canadapost.ca"
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", "DOM.EP", "DOM.XP", "DOM.XP.CERT", "DOM.PC.CERT", "DOM.PC", "DOM.DT", "DOM.LIB", "USA.EP", "USA.PW.ENV", "USA.PW.PAK", "USA.PW.PARCEL", "USA.SP.AIR", "USA.TP", "USA.TP.LVM", "USA.XP", "INT.XP", "INT.IP.AIR", "INT.IP.SURF", "INT.PW.ENV", "INT.PW.PAK", "INT.PW.PARCEL", "INT.SP.AIR", "INT.SP.SURF", "INT.TP"]

Instance Method Summary collapse

Constructor Details

#initialize(credentials, options = {}) ⇒ Base

Returns a new instance of Base.



57
58
59
60
61
62
63
# File 'lib/canada_post/request/base.rb', line 57

def initialize(credentials, options={})
  requires!(options, :shipper, :recipient, :package)
  @credentials = credentials
  @shipper, @recipient, @package, @service_type = options[:shipper], options[:recipient], options[:package], options[:service_type]
  @authorization = { username: @credentials.username, password: @credentials.password }
  @customer_number = @credentials.customer_number
end

Instance Method Details

#add_package(xml) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/canada_post/request/base.rb', line 77

def add_package(xml)
  xml.send(:"parcel-characteristics") {
    xml.weight @package[:weight][:value]
    if @package[:dimensions]
      xml.dimensions {
        xml.height @package[:dimensions][:height].round(1)
        xml.width @package[:dimensions][:width].round(1)
        xml.length @package[:dimensions][:length].round(1)
      }
    end
  }
end

#api_urlObject



69
70
71
# File 'lib/canada_post/request/base.rb', line 69

def api_url
  @credentials.mode == "production" ? PRODUCTION_URL : TEST_URL
end

#build_xmlObject

Raises:

  • (NotImplementedError)


73
74
75
# File 'lib/canada_post/request/base.rb', line 73

def build_xml
  raise NotImplementedError, "Override #build_xml in subclass"
end

#parse_response(response) ⇒ Object

Parse response, convert keys to underscore symbols



91
92
93
94
# File 'lib/canada_post/request/base.rb', line 91

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_requestObject

Raises:

  • (NotImplementedError)


65
66
67
# File 'lib/canada_post/request/base.rb', line 65

def process_request
  raise NotImplementedError, "Override #process_request in subclass"
end

#sanitize_response_keys(response) ⇒ Object

Recursively sanitizes the response object by cleaning up any hash keys.



97
98
99
100
101
102
103
104
105
# File 'lib/canada_post/request/base.rb', line 97

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