Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added ability to find all tracking numbers with shipment id #161

Open
wants to merge 3 commits into from

2 participants

@alexgirdler

Finds and returns all tracking numbers associated with a shipment ID as well as returning tracking_response for each tracking number.
Due to the double UPS API call in find_all_tracking_info I was unable to create a unit test.

@wvanbergen
Collaborator

When would you use this call? Is this UPS specific?

  • Is it possible to add a remote test, that tests both API calls in succession?
  • This will need to be rebased on the latest master, because the UPS carrier was rewritten using Nokogiri.
@alexgirdler

Sorry I did not see that you commented on this. From what I have seen it is UPS specific. The situation this was used in was as follows:

Order bulk merchandise from vendor.
Vendor provides only partial number of tracking numbers(I believe we were missing 3 unique tracking numbers)
When packages actually arrive there will be extra tracking numbers not included in shipment information that are linked through UPS to main parcel(bulk orders for UPS all share a common parent)

I will rebase and push. As for the remote tests I am not sure if I would be able to I will look into writing one and get back with you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
41 lib/active_shipping/shipping/carriers/ups.rb
@@ -121,6 +121,22 @@ def find_tracking_info(tracking_number, options={})
parse_tracking_response(response, options)
end
+ def find_all_tracking_numbers(shipment_number, options={})
+ options = @options.update(options)
+ access_request = build_access_request
+ shipment_request = build_shipment_request(shipment_number, options)
+ response = commit(:track, save_request(access_request + shipment_request), (options[:test] || false))
+ parse_shipment_response(response, options)
+ end
+
+ def find_all_tracking_info(shipment_number, options={})
+ options = @options.update(options)
+ tracking_info = []
+ find_all_tracking_numbers(shipment_number, options).each do |tracking_number|
+ tracking_info << find_tracking_info(tracking_number, options)
+ end
+ end
+
protected
def upsified_location(location)
@@ -246,6 +262,17 @@ def build_tracking_request(tracking_number, options={})
xml_request.to_s
end
+ def build_shipment_request(shipment_number, options={})
+ xml_request = XmlNode.new('TrackRequest') do |root_node|
+ root_node << XmlNode.new('Request') do |request|
+ request << XmlNode.new('RequestAction', 'Track')
+ request << XmlNode.new('RequestOption', '1')
+ end
+ root_node << XmlNode.new('ShipmentIdentificationNumber', shipment_number.to_s)
+ end
+ xml_request.to_s
+ end
+
def build_location_node(name,location,options={})
# not implemented: * Shipment/Shipper/Name element
# * Shipment/(ShipTo|ShipFrom)/CompanyName element
@@ -424,6 +451,20 @@ def parse_tracking_response(response, options={})
:tracking_number => tracking_number)
end
+ def parse_shipment_response(response, options={})
+ xml = REXML::Document.new(response)
+ success = response_success?(xml)
+ message = response_message(xml)
+
+ if success
+ tracking_numbers = []
+ xml.elements.each('/*/Shipment/Package/') do |package|
+ tracking_numbers << package.get_text('TrackingNumber').to_s
+ end
+ tracking_numbers
+ end
+ end
+
def location_from_address_node(address)
return nil unless address
Location.new(
View
50 test/fixtures/xml/ups/test_get_all_tracking_numbers_response.xml
@@ -0,0 +1,50 @@
+<TrackResponse>
+ <Response>
+ <ResponseStatusCode>1</ResponseStatusCode>
+ <ResponseStatusDescription>Success</ResponseStatusDescription>
+ </Response>
+ <Shipment>
+ <Package>
+ <TrackingNumber>1Z7301510374144406</TrackingNumber>
+ </Package>
+ <Package>
+ <TrackingNumber>1Z7301510373402816</TrackingNumber>
+ </Package>
+ <Package>
+ <TrackingNumber>1Z7301510373836027</TrackingNumber>
+ </Package>
+ <Package>
+ <TrackingNumber>1Z7301510374988031</TrackingNumber>
+ </Package>
+ <Package>
+ <TrackingNumber>1Z7301510374722844</TrackingNumber>
+ </Package>
+ <Package>
+ <TrackingNumber>1Z7301510375824456</TrackingNumber>
+ </Package>
+ <Package>
+ <TrackingNumber>1Z7301510374596866</TrackingNumber>
+ </Package>
+ <Package>
+ <TrackingNumber>1Z7301510373464072</TrackingNumber>
+ </Package>
+ <Package>
+ <TrackingNumber>1Z7301510375570088</TrackingNumber>
+ </Package>
+ <Package>
+ <TrackingNumber>1Z7301510373378899</TrackingNumber>
+ </Package>
+ <Package>
+ <TrackingNumber>1Z7301510373274509</TrackingNumber>
+ </Package>
+ <Package>
+ <TrackingNumber>1Z7301510375160913</TrackingNumber>
+ </Package>
+ <Package>
+ <TrackingNumber>1Z7301510373062121</TrackingNumber>
+ </Package>
+ <Package>
+ <TrackingNumber>1Z7301510373722131</TrackingNumber>
+ </Package>
+ </Shipment>
+</TrackResponse>
View
12 test/remote/ups_test.rb
@@ -27,6 +27,18 @@ def test_tracking_with_another_number
end
end
+ def test_getting_all_tracking_numbers
+ assert_nothing_raised do
+ response = @carrier.find_all_tracking_numbers('1Z7301510374144406')
+ end
+ end
+
+ def test_tracking_all_numbers
+ assert_nothing_raised do
+ response = @carrier.find_all_tracking_info('1Z7301510374144406')
+ end
+ end
+
def test_us_to_uk
response = nil
assert_nothing_raised do
View
8 test/unit/carriers/ups_test.rb
@@ -199,4 +199,12 @@ def test_maximum_weight
assert Package.new((150 * 16) + 0.01, [5,5,5], :units => :imperial).mass > @carrier.maximum_weight
assert Package.new((150 * 16) - 0.01, [5,5,5], :units => :imperial).mass < @carrier.maximum_weight
end
+
+ def test_get_all_tracking_numbers
+ mock_response = xml_fixture('ups/test_get_all_tracking_numbers_response')
+ @carrier.expects(:commit).returns(mock_response)
+ response = @carrier.find_all_tracking_numbers('1Z7301510374144406')
+ assert_equal 14, response.count
+ end
+
end
Something went wrong with that request. Please try again.