Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add package details (and a sweet Google Maps URL generator).

  • Loading branch information...
commit 56919e57b083ed343e71fc38595278d3dc2f8baa 1 parent 9eb027d
@mojombo authored
View
4 lib/omniship/ups/track.rb
@@ -1,3 +1,7 @@
+require 'omniship/ups/track/activity'
+require 'omniship/ups/track/activity_location'
+require 'omniship/ups/track/address'
+require 'omniship/ups/track/package'
require 'omniship/ups/track/shipment'
require 'omniship/ups/track_request'
View
39 lib/omniship/ups/track/activity.rb
@@ -0,0 +1,39 @@
+module OmniShip
+ module UPS
+ module Track
+ class Activity
+ # The Handsoap XML element representing the root response node.
+ attr_accessor :root
+
+ # Initialize a new Activity.
+ #
+ # root - The root Package XML node.
+ #
+ # Returns the newly initialized Activity.
+ def initialize(root)
+ @root = root
+ end
+
+ # The location of this activity event.
+ #
+ # Returns the OmniShip::UPS::Track::ActivityLocation.
+ def location
+ node = @root.xpath('./ns:ActivityLocation')
+ ActivityLocation.new(node)
+ end
+
+ # Returns a Hash representation of this object.
+ def to_hash
+ {
+ "Location" => location.to_hash
+ }
+ end
+
+ # Returns the String details of this object.
+ def inspect
+ "#<OmniShip::UPS::Track::Activity location=#{location.address}>"
+ end
+ end
+ end
+ end
+end
View
39 lib/omniship/ups/track/activity_location.rb
@@ -0,0 +1,39 @@
+module OmniShip
+ module UPS
+ module Track
+ class ActivityLocation
+ # The Handsoap XML element representing the root response node.
+ attr_accessor :root
+
+ # Initialize a new ActivityLocation.
+ #
+ # root - The root Package XML node.
+ #
+ # Returns the newly initialized ActivityLocation.
+ def initialize(root)
+ @root = root
+ end
+
+ # The address of this activity location.
+ #
+ # Returns the OmniShip::UPS::Track::Address.
+ def address
+ node = @root.xpath('./ns:Address')
+ Address.new(node)
+ end
+
+ # Returns a Hash representation of this object.
+ def to_hash
+ {
+ "Address" => address.to_hash
+ }
+ end
+
+ # Returns the String details of this object.
+ def inspect
+ "#<OmniShip::UPS::Track::ActivityLocation address=#{address.city}>"
+ end
+ end
+ end
+ end
+end
View
59 lib/omniship/ups/track/address.rb
@@ -0,0 +1,59 @@
+module OmniShip
+ module UPS
+ module Track
+ class Address
+ # The Handsoap XML element representing the root response node.
+ attr_accessor :root
+
+ # Initialize a new Address.
+ #
+ # root - The root Package XML node.
+ #
+ # Returns the newly initialized Address.
+ def initialize(root)
+ @root = root
+ end
+
+ # Returns the String city or nil if none was set.
+ def city
+ @root.xpath('./ns:City/text()').to_s
+ end
+
+ # Returns the String state or province code or nil if none was set.
+ def state_province_code
+ @root.xpath('./ns:StateProvinceCode/text()').to_s
+ end
+
+ # Returns the String country code or nil if none was set.
+ def country_code
+ @root.xpath('./ns:CountryCode/text()').to_s
+ end
+
+ # This address as a single line comma delimited string.
+ #
+ # Returns the String address.
+ def to_s
+ parts = []
+ parts << city if city
+ parts << state_province_code if state_province_code
+ parts << country_code if country_code
+ parts.join(', ')
+ end
+
+ # Returns a Hash representation of this object.
+ def to_hash
+ {
+ "City" => city,
+ "StateProvinceCode" => state_province_code,
+ "CountryCode" => country_code
+ }
+ end
+
+ # Returns the String details of this object.
+ def inspect
+ "#<OmniShip::UPS::Track::Address #{to_s}>"
+ end
+ end
+ end
+ end
+end
View
80 lib/omniship/ups/track/package.rb
@@ -0,0 +1,80 @@
+module OmniShip
+ module UPS
+ module Track
+ class Package
+ # The Handsoap XML element representing the root response node.
+ attr_accessor :root
+
+ # Initialize a new Package.
+ #
+ # root - The root Package XML node.
+ #
+ # Returns the newly initialized Package.
+ def initialize(root)
+ @root = root
+ end
+
+ # Returns the String tracking number.
+ def tracking_number
+ @root.xpath('./ns:TrackingNumber/text()').to_s
+ end
+
+ # The activity of the package in reverse chronological order. Each
+ # element represents a stop on the package's journey.
+ #
+ # Returns an array of OmniShip::UPS::Track::Activity objects.
+ def activity
+ @root.xpath('./ns:Activity').map do |act|
+ Activity.new(act)
+ end
+ end
+
+ # Generate a URL to a Google map showing the package's activity.
+ #
+ # Returns the String URL.
+ def activity_map_url
+ url = "http://maps.google.com/maps/api/staticmap?"
+ parts = []
+
+ stops =
+ activity.select do |act|
+ act.location.address.city
+ end.map do |act|
+ act.location.address.to_s
+ end.uniq.reverse
+
+ path_parts = []
+ path_parts << "path=color:0x0000ff"
+ path_parts << "weight:5"
+ stops.each { |addy| path_parts << addy }
+ parts << path_parts.join('|')
+
+ origin = stops.shift
+ last = stops.pop
+
+ parts << "markers=color:red|size:mid|#{origin}" if origin
+ parts << "markers=color:green|#{last}"
+
+ parts << 'size=512x512'
+ parts << 'maptype=roadmap'
+ parts << 'sensor=false'
+ url += parts.join('&')
+ url
+ end
+
+ # Returns a Hash representation of this object.
+ def to_hash
+ {
+ "Activity" => activity.map { |x| x.to_hash },
+ "ActivityMapURL" => activity_map_url
+ }
+ end
+
+ # Returns the String details of this object.
+ def inspect
+ "#<OmniShip::UPS::Track::Package tracking_number=#{tracking_number}>"
+ end
+ end
+ end
+ end
+end
View
17 lib/omniship/ups/track/shipment.rb
@@ -14,6 +14,15 @@ def initialize(root)
@root = root
end
+ # The list of packages in this shipment.
+ #
+ # Returns an array of Package objects.
+ def packages
+ @root.xpath('./ns:Shipment/ns:Package').map do |package|
+ Package.new(package)
+ end
+ end
+
# The scheduled delivery date. If a specific time of day is available
# then it will be set, otherwise the time will be set to noon. If no
# delivery date is available, the result will be nil.
@@ -52,9 +61,15 @@ def scheduled_delivery_time
# Returns a Hash representation of this object.
def to_hash
{
- "ScheduledDelivery" => scheduled_delivery.to_i
+ "ScheduledDelivery" => scheduled_delivery.to_i,
+ "Packages" => packages.map { |x| x.to_hash }
}
end
+
+ # Returns the String details of this object.
+ def inspect
+ "#<OmniShip::UPS::Track::Shipment packages=#{packages.size}>"
+ end
end
end
end
View
11 test/test_ups_track.rb
@@ -3,12 +3,19 @@
class TestUPSTrack < Test::Unit::TestCase
context "stuff" do
setup do
-
+
end
should "work" do
tr = OmniShip::UPS.track('1z7900970327439305')
- p tr.shipment.scheduled_delivery
+
+ tr.shipment.packages.first.activity.each do |act|
+ p act.location.address.to_s
+ end
+
+ puts tr.shipment.packages.first.activity_map_url
+
+ p tr.to_json
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.