Class: Fedex::Request::Rate

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

Constant Summary

Constants inherited from Base

Base::CARRIER_CODES, Base::CLEARANCE_BROKERAGE_TYPE, Base::DROP_OFF_TYPES, Base::PACKAGING_TYPES, Base::PAYMENT_TYPE, Base::PRODUCTION_URL, Base::RECIPIENT_CUSTOM_ID_TYPE, Base::SERVICE_TYPES, Base::TEST_URL

Instance Attribute Summary

Attributes inherited from Base

#debug

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

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

Instance Method Details

#process_requestObject

Sends post request to Fedex web service and parse the response, a Rate object is created if the response is successful



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

def process_request
  api_response = self.class.post(api_url, :body => build_xml)
  puts api_response if @debug
  response = parse_response(api_response)
  if success?(response)
    rate_reply_details = response[:rate_reply][:rate_reply_details] || []
    rate_reply_details = [rate_reply_details] if rate_reply_details.is_a?(Hash)

    rate_reply_details.map do |rate_reply|
      rate_details = [rate_reply[:rated_shipment_details]].flatten.first[:shipment_rate_detail]
      rate_details.merge!(service_type: rate_reply[:service_type])
      rate_details.merge!(transit_time: rate_reply[:transit_time])
      Fedex::Rate.new(rate_details)
    end
  else
    error_message = if response[:rate_reply]
      [response[:rate_reply][:notifications]].flatten.first[:message]
    else
      "#{api_response["Fault"]["detail"]["fault"]["reason"]}\n--#{api_response["Fault"]["detail"]["fault"]["details"]["ValidationFailureDetail"]["message"].join("\n--")}"
    end rescue $1
    raise RateError, error_message
  end
end