Class: Fedex::Request::ServiceAvailability

Inherits:
Base
  • Object
show all
Defined in:
lib/fedex/request/service_availability.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

Constructor Details

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


6
7
8
9
10
11
12
13
14
# File 'lib/fedex/request/service_availability.rb', line 6

def initialize(credentials, options={})
  requires!(options, :origin, :destination, :ship_date, :carrier_code)

  @credentials  = credentials
  @origin       = options[:origin]
  @destination  = options[:destination]
  @ship_date    = options[:ship_date]
  @carrier_code = options[:carrier_code]
end

Instance Method Details

#add_destination(xml) ⇒ Object


48
49
50
51
52
53
# File 'lib/fedex/request/service_availability.rb', line 48

def add_destination(xml)
  xml.Destination{
    xml.PostalCode  @destination[:postal_code]
    xml.CountryCode @destination[:country_code]
  }
end

#add_origin(xml) ⇒ Object


41
42
43
44
45
46
# File 'lib/fedex/request/service_availability.rb', line 41

def add_origin(xml)
  xml.Origin{
    xml.PostalCode  @origin[:postal_code]
    xml.CountryCode @origin[:country_code]
  }
end

#add_other_details(xml) ⇒ Object


55
56
57
58
# File 'lib/fedex/request/service_availability.rb', line 55

def add_other_details(xml)
  xml.ShipDate @ship_date
  xml.CarrierCode @carrier_code
end

#build_xmlObject


27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fedex/request/service_availability.rb', line 27

def build_xml
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.ServiceAvailabilityRequest(:xmlns => "http://fedex.com/ws/packagemovementinformationservice/v#{service[:version]}"){
      add_web_authentication_detail(xml)
      add_client_detail(xml)
      add_version(xml)
      add_origin(xml)
      add_destination(xml)
      add_other_details(xml)
    }
  end
  builder.doc.root.to_xml
end

#failure_response(api_response, response) ⇒ Object

Callback used after a failed shipment response.

Raises:


61
62
63
64
65
66
67
68
# File 'lib/fedex/request/service_availability.rb', line 61

def failure_response(api_response, response)
  error_message = if response[:service_availability_reply]
    [response[:service_availability_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

#process_requestObject


16
17
18
19
20
21
22
23
24
25
# File 'lib/fedex/request/service_availability.rb', line 16

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)
    success_response(api_response, response)
  else
    failure_response(api_response, response)
  end
end

#serviceObject


75
76
77
# File 'lib/fedex/request/service_availability.rb', line 75

def service
  { :id => 'pmis', :version => Fedex::SERVICE_AVAILABILITY_API_VERSION }
end

#success?(response) ⇒ Boolean

Successful request


80
81
82
83
# File 'lib/fedex/request/service_availability.rb', line 80

def success?(response)
  response[:service_availability_reply] &&
    %w{SUCCESS WARNING NOTE}.include?(response[:service_availability_reply][:highest_severity])
end

#success_response(api_response, response) ⇒ Object

Callback used after a successful shipment response.


71
72
73
# File 'lib/fedex/request/service_availability.rb', line 71

def success_response(api_response, response)
  @response_details = response[:service_availability_reply]
end