Class: FedexShip::Request::Rate

Inherits:
LogsFedex show all
Defined in:
lib/fedex_ship/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 LogsFedex

#delete_ship_serv_log, #pickup_serv_log, #rate_serv_log, #ship_serv_log, #track_serv_log

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from FedexShip::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



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fedex_ship/request/rate.rb', line 8

def process_request
  @build_xml = build_xml
  rate_serv_log('Final XML Request : ' + @build_xml.to_s)
  api_url_srv = api_url + "/rate"
  rate_serv_log('URL for API : ' + api_url_srv.to_s)
  api_response = self.class.post(api_url_srv, :body => @build_xml)
  rate_serv_log('API Response : ' + api_response.to_s)
  puts api_response if @debug
  response = parse_response(api_response)
  if success?(response)
    rate_serv_log('Successfully Done : ' + response.to_s)
    rate_reply_details = response[:envelope][:body][: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[:delivery_timestamp])
      FedexShip::Rate.new(rate_details)
    end
  else
    error_message = if response[:envelope][:body][:rate_reply]
                      [response[:envelope][:body][: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