Class: CanadaPost::Request::Rate

Inherits:
Base
  • Object
show all
Defined in:
lib/canada_post/request/rate.rb

Constant Summary

Constants inherited from Base

Base::OPTION_CODES, Base::PRODUCTION_URL, Base::SERVICE_CODES, Base::TEST_CONTRACT_ID, Base::TEST_URL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#api_url, #client, #parse_response, #process_response, #sanitize_response_keys

Constructor Details

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

Returns a new instance of Rate.



7
8
9
10
11
12
# File 'lib/canada_post/request/rate.rb', line 7

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]
  super(credentials)
end

Instance Attribute Details

#packageObject

Returns the value of attribute package.



5
6
7
# File 'lib/canada_post/request/rate.rb', line 5

def package
  @package
end

#recipientObject

Returns the value of attribute recipient.



5
6
7
# File 'lib/canada_post/request/rate.rb', line 5

def recipient
  @recipient
end

#shipperObject

Returns the value of attribute shipper.



5
6
7
# File 'lib/canada_post/request/rate.rb', line 5

def shipper
  @shipper
end

Instance Method Details

#process_requestObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/canada_post/request/rate.rb', line 14

def process_request
  api_response = client(rate_url, build_xml, rate_headers)
  response = parse_response(api_response)

  if success?(response)
    rate_reply_details = response[:price_quotes][:price_quote] || []
    rate_reply_details = [rate_reply_details] if rate_reply_details.is_a? Hash
    rate_reply_details.map do |rate_reply|
      CanadaPost::Rate.new(rate_reply)
    end
  else
    error_message =
      if response[:messages]
        response[:messages][:message][:description]
      else
        'api_response.response'
      end
    raise RateError, error_message
  end
end