Class: Trackerific::UPS

Inherits:
Service show all
Includes:
HTTParty
Defined in:
lib/trackerific/services/ups.rb

Overview

Provides package tracking support for UPS.

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Service

#initialize

Methods included from OptionsHelper

#validate_options

Constructor Details

This class inherits a constructor from Trackerific::Service

Class Method Details

+ (Array, Regexp) package_id_matchers

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 UPS package IDs

Returns:

  • (Array, Regexp)

    the regular expression



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

def package_id_matchers
  [ /^.Z/, /^[HK].{10}$/ ]
end

+ (Array) required_options

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.

The required options for tracking a UPS package

Returns:

  • (Array)

    the required options for tracking a UPS package



27
28
29
# File 'lib/trackerific/services/ups.rb', line 27

def required_options
  [:key, :user_id, :password]
end

Instance Method Details

- (Trackerific::Details) track_package(package_id)

Tracks a UPS package

Examples:

Track a package

ups = Trackerific::UPS.new key: 'api key', user_id: 'user', password: 'secret'
details = ups.track_package("1Z12345E0291980793")

Parameters:

  • package_id (String)

    the package identifier

Returns:

Raises:

  • (Trackerific::Error)

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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/trackerific/services/ups.rb', line 40

def track_package(package_id)
  super
  # connect to UPS via HTTParty
  http_response = self.class.post('/Track', :body => build_xml_request)
  # throw any HTTP errors
  http_response.error! unless http_response.code == 200
  # Check the response for errors, return a Trackerific::Error, or parse
  # the response from UPS and return a Trackerific::Details
  case http_response['TrackResponse']['Response']['ResponseStatusCode']
    when "0" then raise Trackerific::Error, parse_error_response(http_response)
    when "1" then return parse_success_response(http_response)
    else raise Trackerific::Error, "Invalid response code returned from server."
  end
end