Skip to content
This repository has been archived by the owner on Jun 13, 2018. It is now read-only.

Commit

Permalink
Merge pull request #361 from Shopify/better-detect-usps-attempted-del…
Browse files Browse the repository at this point in the history
…ivery

Use multiple attempted delivery codes for USPS tracking
  • Loading branch information
Jonathan Kwok committed Apr 26, 2016
2 parents fbbe5c4 + 21f2c50 commit 18334c2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
5 changes: 3 additions & 2 deletions lib/active_shipping/carriers/usps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ class USPS < Carrier
:package_service => 'PACKAGESERVICE'
}

ATTEMPTED_DELIVERY_CODES = %w(02 53 54 55 56 H0)

# Array of U.S. possessions according to USPS: https://www.usps.com/ship/official-abbreviations.htm
US_POSSESSIONS = %w(AS FM GU MH MP PW PR VI)

Expand Down Expand Up @@ -630,8 +632,7 @@ def parse_tracking_info(response, node)

shipment_events = shipment_events.sort_by(&:time)

# USPS defines a delivery attempt with code 55
attempted_delivery_date = shipment_events.detect{ |shipment_event| shipment_event.type_code=="55" }.try(:time)
attempted_delivery_date = shipment_events.detect{ |shipment_event| ATTEMPTED_DELIVERY_CODES.include?(shipment_event.type_code) }.try(:time)

if last_shipment = shipment_events.last
status = last_shipment.status
Expand Down
8 changes: 4 additions & 4 deletions test/remote/usps_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ def setup
end

def test_tracking_with_attempted_delivery
response = @carrier.find_tracking_info('9405515901606017103876', test: false)
response = @carrier.find_tracking_info('CJ509046330US', test: false)
assert response.success?, response.message
assert_equal 9,response.shipment_events.size
assert_equal 10,response.shipment_events.size
assert_equal 'DELIVERED', response.shipment_events.last.message
assert_equal Time.parse('2015-12-10 14:42:00 UTC'), response.attempted_delivery_date
assert_equal Time.parse('2015-12-24 10:51:00 UTC'), response.actual_delivery_date
assert_equal Time.parse('2016-04-21 13:46:00 UTC'), response.attempted_delivery_date
assert_equal Time.parse('2016-04-25 17:13:00 UTC'), response.actual_delivery_date
end

def test_tracking_with_bad_number
Expand Down

0 comments on commit 18334c2

Please sign in to comment.