Class: Fedex::Request::TrackingInformation

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

Attributes inherited from Base

#debug

Instance Method Summary collapse

Constructor Details

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


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fedex/request/tracking_information.rb', line 10

def initialize(credentials, options={})
  requires!(options, :package_type, :package_id) unless options.has_key?(:tracking_number)

  @package_id   = options[:package_id]   || options.delete(:tracking_number)
  @package_type = options[:package_type] || "TRACKING_NUMBER_OR_DOORTAG"
  @credentials  = credentials

  # Optional
  @include_detailed_scans = options[:include_detailed_scans] || true
  @uuid                   = options[:uuid]
  @paging_token           = options[:paging_token]

  unless package_type_valid?
    raise "Unknown package type '#{package_type}'"
  end
end

Instance Attribute Details

#package_idObject (readonly)

Returns the value of attribute package_id


8
9
10
# File 'lib/fedex/request/tracking_information.rb', line 8

def package_id
  @package_id
end

#package_typeObject (readonly)

Returns the value of attribute package_type


8
9
10
# File 'lib/fedex/request/tracking_information.rb', line 8

def package_type
  @package_type
end

Instance Method Details

#process_requestObject


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fedex/request/tracking_information.rb', line 27

def process_request
  api_response = self.class.post(api_url, :body => build_xml)
  puts api_response if @debug == true
  response = parse_response(api_response)

  if success?(response)
    options = response[:track_reply][:track_details]

    if response[:track_reply][:duplicate_waybill].downcase == 'true'
      shipments = []
      [options].flatten.map do |details|
        options = {:tracking_number => @package_id, :uuid => details[:tracking_number_unique_identifier]}
        shipments << Request::TrackingInformation.new(@credentials, options).process_request
      end
      shipments.flatten
    else
      [options].flatten.map do |details|
        Fedex::TrackingInformation.new(details)
      end
    end
  else
    error_message = if response[:track_reply]
      response[:track_reply][:notifications][: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