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_URL

Instance Method Summary collapse

Methods inherited from Base

#add_package, #initialize, #parse_response, #sanitize_response_keys

Constructor Details

This class inherits a constructor from CanadaPost::Request::Base

Instance Method Details

#process_requestObject

Sends post request to CanadaPost API and parse the response, a Rate object is created if the response is successful



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/canada_post/request/rate.rb', line 5

def process_request
  api_response = self.class.post(api_url,
    body: build_xml,
    headers: rate_headers,
    basic_auth: @authorization 
    )
  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