Browse files

Create Location class, and include more location information since th…

…e APIs have it.
  • Loading branch information...
1 parent 624f6f2 commit eeecbe36feac4b1d398a0953d7052aeda1d87a09 @jkeen committed Mar 19, 2013
View
1 lib/trackerific.rb
@@ -4,6 +4,7 @@
require 'trackerific/service'
require 'trackerific/error'
require 'trackerific/details'
+require 'trackerific/location'
require 'trackerific/event'
# require all the Trackerific services
View
15 lib/trackerific/details.rb
@@ -9,10 +9,12 @@ class Details
# @api private
def initialize(details = {})
required = [:package_id, :summary, :events]
- valid = required + [:weight, :via, :estimated_delivery_date]
+ valid = required + [:weight, :via, :estimated_delivery_date, :origin, :destination]
validate_options(details, required, valid)
@package_id = details[:package_id]
@summary = details[:summary]
+ @origin = details[:origin]
+ @destination = details[:destination]
@events = details[:events]
@weight = details[:weight] || nil
@estimated_delivery_date = details[:estimated_delivery_date] || nil
@@ -81,5 +83,16 @@ def estimated_delivery_date
def via
@via
end
+
+ # Location object of origin
+ def origin
+ @origin
+ end
+
+ # Location object of destination
+ def destination
+ @destination
+ end
+
end
end
View
2 lib/trackerific/event.rb
@@ -40,7 +40,7 @@ def description
def location
@location
end
-
+
# Converts the event into a string
# @example Get a human-readable string from an event
# event = details.event.to_s
View
19 lib/trackerific/location.rb
@@ -0,0 +1,19 @@
+module Trackerific
+ class Location
+ def initialize(details = {})
+ @address = details[:address]
+ @city = details[:city]
+ @state = details[:state]
+ @postal_code = details[:postal_code]
+ @country = details[:country]
+
+ self
+ end
+
+ def to_s
+ [@address, @city, @state, @postal_code, @country].compact.join(" ")
+ end
+
+ attr_reader :address, :city, :state, :postal_code, :country
+ end
+end
View
13 lib/trackerific/services/fedex.rb
@@ -25,7 +25,7 @@ def required_parameters
[:account, :meter]
end
end
-
+
# Tracks a FedEx package
# @param [String] package_id the package identifier
# @return [Trackerific::Details] the tracking details
@@ -42,6 +42,7 @@ def track_package(package_id)
http_response.error! unless http_response.code == 200
# get the tracking information from the reply
track_reply = http_response["FDXTrack2Reply"]
+
# raise a Trackerific::Error if there is an error in the reply
raise Trackerific::Error, track_reply["Error"]["Message"] unless track_reply["Error"].nil?
# get the details from the reply
@@ -55,14 +56,16 @@ def track_package(package_id)
events << Trackerific::Event.new(
:date => date,
:description => desc,
- :location => "#{addr["StateOrProvinceCode"]} #{addr["PostalCode"]}"
+ :location => Trackerific::Location.new(:city => addr["City"], :state => addr["StateOrProvinceCode"], :country => addr["CountryCode"], :postal_code => addr["PostalCode"])
)
end
# Return a Trackerific::Details containing all the events
Trackerific::Details.new(
- :package_id => details["TrackingNumber"],
- :summary => details["StatusDescription"],
- :events => events
+ :package_id => details["TrackingNumber"],
+ :origin => Trackerific::Location.new(:city => details["OriginLocationAddress"]["City"], :state => details["OriginLocationAddress"]["StateOrProvinceCode"], :country => details["OriginLocationAddress"]["CountryCode"]),
+ :destination => Trackerific::Location.new(:city => details["DestinationAddress"]["City"], :state => details["DestinationAddress"]["StateOrProvinceCode"], :country => details["DestinationAddress"]["CountryCode"]),
+ :summary => details["StatusDescription"],
+ :events => events
)
end
View
15 lib/trackerific/services/ups.rb
@@ -87,17 +87,24 @@ def parse_success_response(http_response)
date = DateTime.parse("#{date} #{hours}:#{minutes}:#{seconds}")
desc = a['Status']['StatusType']['Description'].titleize
loc = a['ActivityLocation']['Address'].map {|k,v| v}.join(" ")
+ address = a['ActivityLocation']['Address']
+
events << Trackerific::Event.new(
:date => date,
:description => desc,
- :location => loc
+ :location => Trackerific::Location.new(:city => address["City"], :state => address["StateProvinceCode"], :country => address["CountryCode"]),
)
end
+ origin = http_response['TrackResponse']['Shipment']["Shipper"]["Address"]
+ destination = http_response['TrackResponse']['Shipment']["ShipTo"]["Address"]
+
Trackerific::Details.new(
- :package_id => @package_id,
- :summary => summary,
- :events => events,
+ :package_id => @package_id,
+ :summary => summary,
+ :origin => Trackerific::Location.new(:address => origin["AddressLine1"], :city => origin["City"], :state => origin["StateProvinceCode"], :country => origin["CountryCode"]),
+ :destination => Trackerific::Location.new(:address => destination["AddressLine1"], :city => destination["City"], :state => destination["StateProvinceCode"], :country => destination["CountryCode"]),
+ :events => events,
:estimated_delivery_date => deliveryDate
)
end
View
3 lib/trackerific/services/usps.rb
@@ -53,6 +53,7 @@ def track_package(package_id)
# raise any errors
error = check_response_for_errors(response, :TrackV2)
raise error unless error.nil?
+
# get the tracking information from the response
tracking_info = response['TrackResponse']['TrackInfo']
events = []
@@ -148,7 +149,7 @@ def location_of_event(event)
state = l[:state] unless l[:state].nil?
zip = l[:zip] unless l[:zip].nil?
end
- "#{city.titleize}, #{state} #{zip}"
+ Trackerific::Location.new(:city => city, :state => state, :postal_code => zip)
end
# Checks a HTTParty response for USPS, or HTTP errors
View
8 spec/fixtures/ups_success_response.xml
@@ -18,6 +18,14 @@
<CountryCode>US</CountryCode>
</Address>
</Shipper>
+ <ShipTo>
+ <Address>
+ <City>MINNEAPOLIS</City>
+ <StateProvinceCode>MN</StateProvinceCode>
+ <PostalCode>55416</PostalCode>
+ <CountryCode>US</CountryCode>
+ </Address>
+ </ShipTo>
<ShipmentWeight>
<UnitOfMeasurement>
<Code>LBS</Code>
View
2 spec/lib/trackerific/error_spec.rb
@@ -6,4 +6,4 @@
subject { @error }
specify { should be_a StandardError }
-end
+end
View
9 spec/lib/trackerific/location_spec.rb
@@ -0,0 +1,9 @@
+require 'spec_helper'
+
+describe Trackerific::Location do
+
+ before { @location = Trackerific::Location.new(:city => "Austin", :state => "TX", :postal_code => "78745") }
+ subject { @location.to_s }
+ specify { should == "Austin TX 78745" }
+
+end
View
10 spec/lib/trackerific/services/fedex_spec.rb
@@ -49,6 +49,16 @@
subject { @tracking }
it("should return a Trackerific::Details") { should be_a Trackerific::Details }
+ describe "origin" do
+ subject { @tracking.origin }
+ it("should return a Trackerific::Location") { should be_a Trackerific::Location }
+ end
+
+ describe "destination" do
+ subject { @tracking.destination }
+ it("should return a Trackerific::Location") { should be_a Trackerific::Location }
+ end
+
describe "events.length" do
subject { @tracking.events.length }
it { should >= 1}
View
12 spec/lib/trackerific/services/ups_spec.rb
@@ -1,6 +1,6 @@
require 'spec_helper'
require 'fakeweb'
-
+require 'pry'
UPS_TRACK_URL = 'https://wwwcie.ups.com/ups.app/xml/Track'
describe "Trackerific::UPS" do
@@ -49,6 +49,16 @@
subject { @tracking }
it("should return a Trackerific::Details") { should be_a Trackerific::Details }
+
+ describe "origin" do
+ subject { @tracking.origin }
+ it("should be a location object") { should be_a Trackerific::Location }
+ end
+
+ describe "destination" do
+ subject { @tracking.destination }
+ it("should be a location object") { should be_a Trackerific::Location }
+ end
describe "events.length" do
subject { @tracking.events.length }
View
1 trackerific.gemspec
@@ -32,6 +32,7 @@ Gem::Specification.new do |s|
"lib/trackerific/details.rb",
"lib/trackerific/error.rb",
"lib/trackerific/event.rb",
+ "lib/trackerific/location.rb",
"lib/trackerific/service.rb",
"lib/trackerific/services/fedex.rb",
"lib/trackerific/services/mock_service.rb",

0 comments on commit eeecbe3

Please sign in to comment.