Class: Trackerific::Services::FedEx

Inherits:
Base
  • Object
show all
Includes:
HTTParty
Defined in:
lib/trackerific/services/fedex.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

can_track?, register, track

Constructor Details

#initialize(options = {}) ⇒ FedEx

Returns a new instance of FedEx.



21
22
23
# File 'lib/trackerific/services/fedex.rb', line 21

def initialize(options={})
  @options = options
end

Class Method Details

.package_id_matchersArray, Regexp

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

An Array of Regexp that matches valid FedEx package IDs

Returns:

  • (Array, Regexp)

    the regular expression



17
18
19
# File 'lib/trackerific/services/fedex.rb', line 17

def self.package_id_matchers
  [ /^[0-9]{15}$/ ]
end

Instance Method Details

#track(package_id) ⇒ Trackerific::Details

Tracks a FedEx package

Examples:

Track a package

fedex = Trackerific::FedEx.new :account => 'account', :meter => 'meter'
details = fedex.track_package("183689015000001")

Parameters:

  • package_id (String)

    the package identifier

Returns:

Raises:

  • (Trackerific::Error)

    raised when the server returns an error (invalid credentials, tracking package, etc.)



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/trackerific/services/fedex.rb', line 33

def track(package_id)
  # request tracking information from FedEx via HTTParty
  http_response = self.class.post "/GatewayDC", body: build_xml_request
  # raise any HTTP errors
  http_response.error! unless http_response.code == 200
  # get the tracking information from the reply
  track_reply = http_response["FDXTrack2Reply"]
  # raise a Trackerific::Error if there is an error in the reply
  raise Trackerific::Error, track_reply["Error"]["Message"] unless track_reply["Error"].nil?
  # get the details from the reply
  details = track_reply["Package"]
  # convert them into Trackerific::Events
  events = []
  details["Event"].each do |e|
    date = Time.parse("#{e["Date"]} #{e["Time"]}")
    desc = e["Description"]
    addr = e["Address"]
    loc = "#{addr["StateOrProvinceCode"]} #{addr["PostalCode"]}"
    events << Trackerific::Event.new(date, desc, loc)
  end
  # Return a Trackerific::Details containing all the events
  Trackerific::Details.new(
    details["TrackingNumber"], details["StatusDescription"], events
  )
end