Class: Trackerific::USPS

Inherits:
Base
  • Object
show all
Includes:
HTTParty
Defined in:
lib/usps.rb

Overview

Provides package tracking support for USPS.

Instance Method Summary (collapse)

Constructor Details

- (USPS) initialize(options = {})

A new instance of USPS



16
17
18
19
# File 'lib/usps.rb', line 16

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

Instance Method Details

- (Array) required_options

The required option for tracking a UPS package is :user_id

Returns:

  • (Array)

    the required options for tracking a UPS package.



24
25
26
# File 'lib/usps.rb', line 24

def required_options
  [:user_id]
end

- (Trackerific::Details) track_package(package_id)

Tracks a USPS package. A Trackerific::Error is raised when a package cannot be tracked.

Returns:

Raises:



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/usps.rb', line 31

def track_package(package_id)
  super
  response = self.class.get('/ShippingAPITest.dll', :query => {:API => 'TrackV2', :XML => build_xml_request}.to_query)
  response.error! unless response.code == 200
  raise Trackerific::Error, response['Error']['Description'] unless response['Error'].nil?
  raise Trackerific::Error, "Tracking information not found in response from server." if response['TrackResponse'].nil?
  tracking_info = response['TrackResponse']['TrackInfo']
  details = []
  tracking_info['TrackDetail'].each do |d|
    # each tracking detail is a string in this format:
    # MM DD HH:MM am/pm DESCRIPTION CITY STATE ZIP.
    # unfortunately, it will not be possible to tell the difference between
    # the location, and the summary. So, for USPS, the location will be in
    # the summary
    d = d.split(" ")
    date = DateTime.parse(d[0..3].join(" "))
    desc = d[4..d.length].join(" ")
    details << Trackerific::Event.new(date, desc, "")
  end
  Trackerific::Details.new(
    tracking_info['ID'],
    tracking_info['TrackSummary'],
    details
  )
end