From 82e068c76df2bfc22cef8fa3b80745ab90caf049 Mon Sep 17 00:00:00 2001 From: Jurgen Heysen Date: Tue, 7 Dec 2021 17:51:47 -0300 Subject: [PATCH] UPS events time zone (#4) * fix: Set correct timezone for shipping events * fix: Better management of the offset * fix: Improved code * chore: bump version --- lib/active_shipping/carriers/ups.rb | 7 +- lib/active_shipping/version.rb | 2 +- test/fixtures/xml/ups/time_with_zone.xml | 379 +++++++++++++++++++++++ test/unit/carriers/ups_test.rb | 8 + test/unit/carriers/usps_test.rb | 4 +- 5 files changed, 394 insertions(+), 6 deletions(-) create mode 100644 test/fixtures/xml/ups/time_with_zone.xml diff --git a/lib/active_shipping/carriers/ups.rb b/lib/active_shipping/carriers/ups.rb index bb50088ea..386972b17 100644 --- a/lib/active_shipping/carriers/ups.rb +++ b/lib/active_shipping/carriers/ups.rb @@ -892,7 +892,7 @@ def parse_tracking_response(response, options = {}) shipment_events = activities.map do |activity| description = activity.at('Status/StatusType/Description').try(:text) type_code = activity.at('Status/StatusType/Code').try(:text) - zoneless_time = parse_ups_datetime(:time => activity.at('Time'), :date => activity.at('Date')) + zoneless_time = parse_ups_datetime(:time => activity.at('Time'), :date => activity.at('Date'), :offset => activity.at('GMTOffset')) location = location_from_address_node(activity.at('ActivityLocation/Address')) ShipmentEvent.new(description, zoneless_time, location, description, type_code) end @@ -1107,15 +1107,16 @@ def location_from_address_node(address) end def parse_ups_datetime(options = {}) - time, date = options[:time].try(:text), options[:date].text + time, date, offset = options[:time].try(:text), options[:date].text, options[:offset]&.text if time.nil? hour, minute, second = 0 else hour, minute, second = time.scan(/\d{2}/) end + offset ||= '+00:00' year, month, day = date[0..3], date[4..5], date[6..7] - Time.utc(year, month, day, hour, minute, second) + Time.new(year, month, day, hour, minute, second, offset).utc end def response_success?(document) diff --git a/lib/active_shipping/version.rb b/lib/active_shipping/version.rb index d0c1adc24..704994d1f 100644 --- a/lib/active_shipping/version.rb +++ b/lib/active_shipping/version.rb @@ -1,3 +1,3 @@ module ActiveShipping - VERSION = "2.3.1" + VERSION = "2.3.2" end diff --git a/test/fixtures/xml/ups/time_with_zone.xml b/test/fixtures/xml/ups/time_with_zone.xml new file mode 100644 index 000000000..c5cf6c776 --- /dev/null +++ b/test/fixtures/xml/ups/time_with_zone.xml @@ -0,0 +1,379 @@ + + + + 1 + Success + + + + 0543R3 +
+ 1234 TEST DR + NORTHAMPTON + PA + 12345 + US +
+
+ +
+ FORT WORTH + TX + 1234 + US +
+
+ + + LBS + + 18.00 + + + 013 + UPS Next Day Air Saver + + + 01 + CPP11568/20146778 + + 1Z0543R31355546175 + 20211201 + 20211202 + + 1Z0543R31355546175 + Y + 20211202 + + +
+ FORT WORTH + TX + 1234 + US +
+ ML + Front Door +
+ + + D + Delivered + + + FS + + + 20211202 + + 2021-12-03 + 01:37:41 + -06:00 +
+ + +
+ Fort Worth + TX + US +
+ Front Door +
+ + + I + Out For Delivery Today + + + OT + + + 20211202 + + 2021-12-02 + 15:56:03 + -06:00 +
+ + +
+ Fort Worth + TX + US +
+ Front Door +
+ + + I + Processing at UPS Facility + + + YP + + + 20211202 + + 2021-12-02 + 13:09:32 + -06:00 +
+ + +
+ Fort Worth + TX + US +
+ Front Door +
+ + + I + Arrived at Facility + + + AR + + + 20211202 + + 2021-12-02 + 12:50:00 + -06:00 +
+ + +
+ DFW Airport + TX + US +
+ Front Door +
+ + + I + Departed from Facility + + + DP + + + 20211202 + + 2021-12-02 + 12:20:00 + -06:00 +
+ + +
+ DFW Airport + TX + US +
+ Front Door +
+ + + I + Arrived at Facility + + + AR + + + 20211202 + + 2021-12-02 + 11:12:00 + -06:00 +
+ + +
+ Rockford + IL + US +
+ Front Door +
+ + + I + Departed from Facility + + + DP + + + 20211202 + + 2021-12-02 + 09:11:00 + -06:00 +
+ + +
+ Rockford + IL + US +
+ Front Door +
+ + + I + Arrived at Facility + + + AR + + + 20211201 + + 2021-12-02 + 05:11:00 + -06:00 +
+ + +
+ Newark + NJ + US +
+ Front Door +
+ + + I + Departed from Facility + + + DP + + + 20211201 + + 2021-12-02 + 02:47:00 + -05:00 +
+ + +
+ Newark + NJ + US +
+ Front Door +
+ + + I + Arrived at Facility + + + AR + + + 20211201 + + 2021-12-02 + 02:08:00 + -05:00 +
+ + +
+ Easton + PA + US +
+ Front Door +
+ + + I + Departed from Facility + + + DP + + + 20211201 + + 2021-12-02 + 00:59:00 + -05:00 +
+ + +
+ Easton + PA + US +
+ Front Door +
+ + + I + Origin Scan + + + OR + + + 20211201 + + 2021-12-01 + 23:16:19 + -05:00 +
+ + +
+ US +
+ Front Door +
+ + + M + Shipper created a label, UPS has not received the package yet. + + + MP + + + 20211201 + + 2021-12-01 + 21:00:39 + -05:00 +
+ + + LBS + + 18.00 + + + 01 + CPP11568/20146778 + + + 01 + CPP11568 + + + 01 + CPP + +
+
+
\ No newline at end of file diff --git a/test/unit/carriers/ups_test.rb b/test/unit/carriers/ups_test.rb index e29de4eb1..5eaceb184 100644 --- a/test/unit/carriers/ups_test.rb +++ b/test/unit/carriers/ups_test.rb @@ -228,6 +228,14 @@ def test_tracking_info_for_status_time_absent assert_nil result.status_time end + def test_time_with_zone + tracking_response = xml_fixture('ups/time_with_zone') + @carrier.expects(:commit).returns(tracking_response) + result = @carrier.find_tracking_info('1Z0543R31355546175').shipment_events[-1].time + expected_time = Time.utc(2021, 12, 03, 01, 37, 41) + assert_equal result, expected_time + end + def test_location_from_address_node_kosovo_kv address = Nokogiri::XML::DocumentFragment.parse(xml_fixture('ups/location_node_kosovo_kv')) diff --git a/test/unit/carriers/usps_test.rb b/test/unit/carriers/usps_test.rb index 6855aa8cf..437cb0bf1 100644 --- a/test/unit/carriers/usps_test.rb +++ b/test/unit/carriers/usps_test.rb @@ -384,7 +384,7 @@ def test_package_valid_for_max_dimensions end def test_strip_9_digit_zip_codes - request = URI.decode(@carrier.send(:build_us_rate_request, package_fixtures[:book], "90210-1234", "123456789")) + request = CGI.unescape(@carrier.send(:build_us_rate_request, package_fixtures[:book], "90210-1234", "123456789")) assert !(request =~ /\>90210-1234\90210\123456789\90210-1234/, request assert_match /\90210/, request