From 21f2c50d28287985e547d5d06f5d050dc8c17f9e Mon Sep 17 00:00:00 2001 From: Jonathan Kwok Date: Tue, 26 Apr 2016 16:09:39 -0400 Subject: [PATCH] When looking for USPS attempted delivery, consider multiple code possibilities --- lib/active_shipping/carriers/usps.rb | 5 +++-- test/remote/usps_test.rb | 8 ++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/active_shipping/carriers/usps.rb b/lib/active_shipping/carriers/usps.rb index 6b7ffc763..6afd4e2f8 100644 --- a/lib/active_shipping/carriers/usps.rb +++ b/lib/active_shipping/carriers/usps.rb @@ -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) @@ -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 diff --git a/test/remote/usps_test.rb b/test/remote/usps_test.rb index f2c2fce13..4a0e30339 100644 --- a/test/remote/usps_test.rb +++ b/test/remote/usps_test.rb @@ -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