class Fedex::Request::Rate

Public Instance Methods

process_request() click to toggle source

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

# 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

Private Instance Methods

add_requested_shipment(xml) click to toggle source

Add information for shipments

# File lib/fedex/request/rate.rb, line 34
def add_requested_shipment(xml)
  xml.RequestedShipment{
    xml.DropoffType @shipping_options[:drop_off_type] ||= "REGULAR_PICKUP"
    xml.ServiceType service_type if service_type
    xml.PackagingType @shipping_options[:packaging_type] ||= "YOUR_PACKAGING"
    add_shipper(xml)
    add_recipient(xml)
    add_shipping_charges_payment(xml)
    add_customs_clearance(xml) if @customs_clearance_detail
    xml.RateRequestTypes "ACCOUNT"
    add_packages(xml)
  }
end
add_transit_time(xml) click to toggle source

Add transite time options

# File lib/fedex/request/rate.rb, line 49
def add_transit_time(xml)
  xml.ReturnTransitAndCommit true
end
build_xml() click to toggle source

Build xml Fedex Web Service request

# File lib/fedex/request/rate.rb, line 54
def build_xml
  ns = "http://fedex.com/ws/rate/v#{service[:version]}"
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.RateRequest(:xmlns => ns){
      add_web_authentication_detail(xml)
      add_client_detail(xml)
      add_version(xml)
      add_transit_time(xml)
      add_requested_shipment(xml)
    }
  end
  builder.doc.root.to_xml
end
service() click to toggle source
# File lib/fedex/request/rate.rb, line 68
def service
  { :id => 'crs', :version => Fedex::API_VERSION }
end
success?(response) click to toggle source

Successful request

# File lib/fedex/request/rate.rb, line 73
def success?(response)
  response[:rate_reply] &&
    %w{SUCCESS WARNING NOTE}.include?(response[:rate_reply][:highest_severity])
end