Class: Trackerific::Services::USPS
- Includes:
- HTTParty
- Defined in:
- lib/trackerific/services/usps.rb
Overview
Provides package tracking support for USPS.
Class Method Summary collapse
-
.package_id_matchers ⇒ Array, Regexp
private
An Array of Regexp that matches valid USPS package IDs.
Instance Method Summary collapse
-
#city_state_lookup(zipcode) ⇒ Hash
Gets the city/state of a zipcode - requires access to USPS address APIs.
-
#initialize(options = {}) ⇒ USPS
constructor
A new instance of USPS.
-
#track(id) ⇒ Trackerific::Details
Tracks a USPS package.
Methods inherited from Base
Constructor Details
#initialize(options = {}) ⇒ USPS
Returns a new instance of USPS.
18 19 20 |
# File 'lib/trackerific/services/usps.rb', line 18 def initialize(={}) = end |
Class Method Details
.package_id_matchers ⇒ Array, 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 USPS package IDs
25 26 27 |
# File 'lib/trackerific/services/usps.rb', line 25 def self.package_id_matchers [ /^E\D{1}\d{9}\D{2}$|^9\d{15,21}$/ ] end |
Instance Method Details
#city_state_lookup(zipcode) ⇒ Hash
Gets the city/state of a zipcode - requires access to USPS address APIs
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/trackerific/services/usps.rb', line 76 def city_state_lookup(zipcode) response = self.class.get( env == 'production' ? "/ShippingAPI.dll" : "/ShippingAPITest.dll", query: { :API => 'CityStateLookup', :XML => build_city_state_xml_request(zipcode) }.to_query ) # raise any errors error = check_response_for_errors(response, :CityStateLookup) raise error unless error.nil? # return the city, state, and zip response = response['CityStateLookupResponse']['ZipCode'] { :city => response['City'], :state => response['State'], :zip => response['Zip5'] } end |
#track(id) ⇒ Trackerific::Details
Tracks a USPS package
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/trackerific/services/usps.rb', line 37 def track(id) @package_id = id # connect to the USPS shipping API via HTTParty response = self.class.get( env == 'production' ? "/ShippingAPI.dll" : "/ShippingAPITest.dll", query: { :API => 'TrackV2', :XML => build_tracking_xml_request }.to_query ) # raise any errors error = check_response_for_errors(response, :TrackV2) raise error unless error.nil? # get the tracking information from the response tracking_info = response['TrackResponse']['TrackInfo'] events = [] # parse the tracking events out of the USPS tracking info tracking_info['TrackDetail'].each do |d| events << Trackerific::Event.new( date_of_event(d), description_of_event(d), location_of_event(d) ) end unless tracking_info['TrackDetail'].nil? # return the details Trackerific::Details.new( tracking_info['ID'], tracking_info['TrackSummary'], events ) end |