trackerific 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +9 -9
- data/Gemfile.lock +21 -3
- data/README.rdoc +29 -8
- data/VERSION +1 -1
- data/lib/fedex.rb +43 -45
- data/lib/trackerific.rb +12 -1
- data/lib/ups.rb +7 -16
- data/lib/usps.rb +7 -14
- data/spec/fixtures/fedex_error_response.xml +10 -0
- data/spec/fixtures/fedex_success_response.xml +73 -11
- data/spec/fixtures/usps_error_response.xml +8 -0
- data/spec/fixtures/usps_success_response.xml +9 -0
- data/spec/lib/fedex_spec.rb +42 -1
- data/spec/lib/trackerific_spec.rb +51 -3
- data/spec/lib/usps_spec.rb +42 -1
- data/spec/support/fixtures.rb +1 -3
- data/trackerific.gemspec +11 -13
- metadata +29 -33
- data/lib/soap_client.rb +0 -13
- data/lib/wsdl/fedex/development.wsdl +0 -1395
- data/lib/wsdl/fedex/production.wsdl +0 -1490
- data/spec/lib/soap_client_spec.rb +0 -5
data/Gemfile
CHANGED
@@ -1,21 +1,21 @@
|
|
1
1
|
source "http://rubygems.org"
|
2
2
|
|
3
|
-
gem
|
3
|
+
gem "rails", ">= 3.0.0"
|
4
4
|
|
5
|
-
gem
|
6
|
-
gem
|
7
|
-
gem 'savon', ">= 0.8.6"
|
8
|
-
gem 'curb', ">= 0.7.15"
|
5
|
+
gem "httparty", ">= 0.7.7"
|
6
|
+
gem "builder", ">= 2.1.2"
|
9
7
|
|
10
8
|
group :development do
|
11
|
-
gem
|
12
|
-
gem
|
9
|
+
gem "bundler", ">= 1.0.13"
|
10
|
+
gem "jeweler", ">= 1.5.2"
|
13
11
|
end
|
14
12
|
|
15
13
|
group :development, :test do
|
16
|
-
gem
|
14
|
+
gem "rspec-rails", ">= 2.6.1"
|
15
|
+
gem "savon_spec", ">= 0.1.6"
|
16
|
+
gem "ruby-debug19", :require => "ruby-debug"
|
17
17
|
end
|
18
18
|
|
19
19
|
group :test do
|
20
|
-
gem
|
20
|
+
gem "fakeweb", ">= 1.3.0"
|
21
21
|
end
|
data/Gemfile.lock
CHANGED
@@ -28,10 +28,11 @@ GEM
|
|
28
28
|
activemodel (= 3.0.5)
|
29
29
|
activesupport (= 3.0.5)
|
30
30
|
activesupport (3.0.5)
|
31
|
+
archive-tar-minitar (0.5.2)
|
31
32
|
arel (2.0.9)
|
32
33
|
builder (2.1.2)
|
34
|
+
columnize (0.3.2)
|
33
35
|
crack (0.1.8)
|
34
|
-
curb (0.7.15)
|
35
36
|
diff-lcs (1.1.2)
|
36
37
|
erubis (2.6.6)
|
37
38
|
abstract (>= 1.0.0)
|
@@ -49,12 +50,15 @@ GEM
|
|
49
50
|
bundler (~> 1.0.0)
|
50
51
|
git (>= 1.2.5)
|
51
52
|
rake
|
53
|
+
linecache19 (0.5.12)
|
54
|
+
ruby_core_source (>= 0.1.4)
|
52
55
|
mail (2.2.15)
|
53
56
|
activesupport (>= 2.3.6)
|
54
57
|
i18n (>= 0.4.0)
|
55
58
|
mime-types (~> 1.16)
|
56
59
|
treetop (~> 1.4.8)
|
57
60
|
mime-types (1.16)
|
61
|
+
mocha (0.9.12)
|
58
62
|
ntlm-http (0.1.1)
|
59
63
|
polyglot (0.3.1)
|
60
64
|
rack (1.2.2)
|
@@ -89,11 +93,25 @@ GEM
|
|
89
93
|
activesupport (~> 3.0)
|
90
94
|
railties (~> 3.0)
|
91
95
|
rspec (~> 2.6.0)
|
96
|
+
ruby-debug-base19 (0.11.25)
|
97
|
+
columnize (>= 0.3.1)
|
98
|
+
linecache19 (>= 0.5.11)
|
99
|
+
ruby_core_source (>= 0.1.4)
|
100
|
+
ruby-debug19 (0.11.6)
|
101
|
+
columnize (>= 0.3.1)
|
102
|
+
linecache19 (>= 0.5.11)
|
103
|
+
ruby-debug-base19 (>= 0.11.19)
|
104
|
+
ruby_core_source (0.1.5)
|
105
|
+
archive-tar-minitar (>= 0.5.2)
|
92
106
|
savon (0.8.6)
|
93
107
|
builder (>= 2.1.2)
|
94
108
|
crack (~> 0.1.8)
|
95
109
|
gyoku (>= 0.3.0)
|
96
110
|
httpi (>= 0.7.8)
|
111
|
+
savon_spec (0.1.6)
|
112
|
+
mocha (>= 0.9.8)
|
113
|
+
rspec (>= 2.0.0)
|
114
|
+
savon (>= 0.8.0)
|
97
115
|
thor (0.14.6)
|
98
116
|
treetop (1.4.9)
|
99
117
|
polyglot (>= 0.3.1)
|
@@ -105,10 +123,10 @@ PLATFORMS
|
|
105
123
|
DEPENDENCIES
|
106
124
|
builder (>= 2.1.2)
|
107
125
|
bundler (>= 1.0.13)
|
108
|
-
curb (>= 0.7.15)
|
109
126
|
fakeweb (>= 1.3.0)
|
110
127
|
httparty (>= 0.7.7)
|
111
128
|
jeweler (>= 1.5.2)
|
112
129
|
rails (>= 3.0.0)
|
113
130
|
rspec-rails (>= 2.6.1)
|
114
|
-
|
131
|
+
ruby-debug19
|
132
|
+
savon_spec (>= 0.1.6)
|
data/README.rdoc
CHANGED
@@ -8,8 +8,8 @@ and then run
|
|
8
8
|
== Usage:
|
9
9
|
|
10
10
|
# Track a FedEx package:
|
11
|
-
fedex = Trackerific::FedEx.new :account => '123456789', :meter => '123456789'
|
12
|
-
tracking_info = fedex.track_package('
|
11
|
+
fedex = Trackerific::FedEx.new :account => '123456789', :meter => '123456789'
|
12
|
+
tracking_info = fedex.track_package('183689015000001')
|
13
13
|
|
14
14
|
# Track a USPS package:
|
15
15
|
usps = Trackerific::USPS.new :user_id => '123USERID4567'
|
@@ -21,7 +21,8 @@ and then run
|
|
21
21
|
|
22
22
|
=== Tracking Results
|
23
23
|
|
24
|
-
The tracking results for all three providers are returned in a Hash in this
|
24
|
+
The tracking results for all three providers are returned in a Hash in this
|
25
|
+
format:
|
25
26
|
|
26
27
|
{
|
27
28
|
:package_id => 'the package id being tracked',
|
@@ -43,9 +44,24 @@ The above code would yield something that looks like this:
|
|
43
44
|
* May 30 10:08 am ARRIVAL AT UNIT WILMINGTON DE 19850.
|
44
45
|
* May 29 9:55 am ACCEPT OR PICKUP EDGEWATER NJ 07020.
|
45
46
|
|
47
|
+
=== Finding a Tracking Service Provider
|
48
|
+
|
49
|
+
If you do not know the tracking service provider of a package id, you can use
|
50
|
+
the tracking_service helper method to get a Trackerific class that most likely
|
51
|
+
will be able to track the given package id.
|
52
|
+
|
53
|
+
include Trackerific
|
54
|
+
tracking_service "183689015000001" # => Trackerific::FedEx
|
55
|
+
tracking_service "1Z12345E0291980793" # => Trackerific::UPS
|
56
|
+
tracking_service "EJ958083578US" # => Trackerific::USPS
|
57
|
+
tracking_service "unknown package id" # => nil
|
58
|
+
|
46
59
|
=== Exception handling:
|
47
60
|
|
48
|
-
Exception handling is esssential for tracking packages. If, for example,
|
61
|
+
Exception handling is esssential for tracking packages. If, for example,
|
62
|
+
you enter the wrong number, or the tracking provider has yet to have added the
|
63
|
+
tracking number to their system, a Trackerific::Error will be raised. Here's an
|
64
|
+
example on how to handle Trackerific::Errors:
|
49
65
|
|
50
66
|
begin
|
51
67
|
usps.track_package('EJ958083578US')
|
@@ -55,13 +71,18 @@ Exception handling is esssential for tracking packages. If, for example, you ent
|
|
55
71
|
|
56
72
|
== Contributing to trackerific
|
57
73
|
|
58
|
-
* Check out the latest master to make sure the feature hasn't been implemented
|
59
|
-
|
74
|
+
* Check out the latest master to make sure the feature hasn't been implemented
|
75
|
+
or the bug hasn't been fixed yet
|
76
|
+
* Check out the issue tracker to make sure someone already hasn't requested it
|
77
|
+
and/or contributed it
|
60
78
|
* Fork the project
|
61
79
|
* Start a feature/bugfix branch
|
62
80
|
* Commit and push until you are happy with your contribution
|
63
|
-
* Make sure to add tests for it. This is important so I don't break it in a
|
64
|
-
|
81
|
+
* Make sure to add tests for it. This is important so I don't break it in a
|
82
|
+
future version unintentionally.
|
83
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to
|
84
|
+
have your own version, or is otherwise necessary, that is fine, but please
|
85
|
+
isolate to its own commit so I can cherry-pick around it.
|
65
86
|
|
66
87
|
== Copyright
|
67
88
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/fedex.rb
CHANGED
@@ -1,64 +1,62 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
|
1
3
|
module Trackerific
|
2
|
-
require 'savon'
|
3
4
|
|
4
5
|
class FedEx < Base
|
5
|
-
include
|
6
|
-
|
7
|
-
|
6
|
+
include ::HTTParty
|
7
|
+
format :xml
|
8
|
+
base_uri "https://gateway.fedex.com"
|
8
9
|
|
9
10
|
def required_options
|
10
|
-
[:account, :meter
|
11
|
+
[:account, :meter]
|
11
12
|
end
|
12
13
|
|
13
14
|
def track_package(package_id)
|
14
15
|
super
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
16
|
+
http_response = self.class.post "/GatewayDC", :body => build_xml_request
|
17
|
+
http_response.error! unless http_response.code == 200
|
18
|
+
|
19
|
+
track_reply = http_response["FDXTrack2Reply"]
|
20
|
+
raise Trackerific::Error, track_reply["Error"]["Message"] unless track_reply["Error"].nil?
|
21
|
+
|
22
|
+
details = track_reply["Package"]
|
23
|
+
events = []
|
24
|
+
details["Event"].each do |e|
|
25
|
+
date = Time.parse("#{e["Date"]} #{e["Time"]}").strftime('%b %d %I:%M %P')
|
26
|
+
desc = e["Description"]
|
27
|
+
addr = e["Address"]
|
28
|
+
# Adds event in this format:
|
29
|
+
# MM DD HH:MM am/pm Description City State Zip
|
30
|
+
events << "#{date} #{desc} #{addr["City"]} #{addr["StateOrProvinceCode"]} #{addr["PostalCode"]}"
|
25
31
|
end
|
32
|
+
|
26
33
|
return {
|
27
|
-
:package_id =>
|
28
|
-
:summary =>
|
29
|
-
:details =>
|
34
|
+
:package_id => details["TrackingNumber"],
|
35
|
+
:summary => details["StatusDescription"],
|
36
|
+
:details => events
|
30
37
|
}
|
31
38
|
end
|
32
39
|
|
33
40
|
protected
|
34
41
|
|
35
|
-
def
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
},
|
54
|
-
:PackageIdentifier => {
|
55
|
-
:Value => @package_id,
|
56
|
-
:Type => 'TRACKING_NUMBER_OR_DOORTAG',
|
57
|
-
:order! => [:Value, :Type]
|
58
|
-
},
|
59
|
-
:IncludeDetailedScans => 'true',
|
60
|
-
:order! => [:WebAuthenticationDetail, :ClientDetail, :Version, :PackageIdentifier, :IncludeDetailedScans]
|
61
|
-
}
|
42
|
+
def build_xml_request
|
43
|
+
xml = ""
|
44
|
+
xmlns_api = "http://www.fedex.com/fsmapi"
|
45
|
+
xmlns_xsi = "http://www.w3.org/2001/XMLSchema-instance"
|
46
|
+
xsi_noNSL = "FDXTrack2Request.xsd"
|
47
|
+
builder = ::Builder::XmlMarkup.new(:target => xml)
|
48
|
+
builder.instruct! :xml, :version => "1.0", :encoding => "UTF-8"
|
49
|
+
builder.FDXTrack2Request "xmlns:api"=>xmlns_api, "xmlns:xsi"=>xmlns_xsi, "xsi:noNamespaceSchemaLocation" => xsi_noNSL do |r|
|
50
|
+
r.RequestHeader do |rh|
|
51
|
+
rh.AccountNumber @options[:account]
|
52
|
+
rh.MeterNumber @options[:meter]
|
53
|
+
end
|
54
|
+
r.PackageIdentifier do |pi|
|
55
|
+
pi.Value @package_id
|
56
|
+
end
|
57
|
+
r.DetailScans true
|
58
|
+
end
|
59
|
+
return xml
|
62
60
|
end
|
63
61
|
|
64
62
|
end
|
data/lib/trackerific.rb
CHANGED
@@ -25,8 +25,19 @@ module Trackerific
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
-
require 'soap_client'
|
29
28
|
require 'usps'
|
30
29
|
require 'fedex'
|
31
30
|
require 'ups'
|
31
|
+
|
32
|
+
def tracking_service(package_id)
|
33
|
+
case package_id
|
34
|
+
when /^.Z/, /^[HK].{10}$/ then Trackerific::UPS
|
35
|
+
when /^96.{20}$/ then Trackerific::FedEx
|
36
|
+
else case package_id.length
|
37
|
+
when 13, 20, 22, 30 then Trackerific::USPS
|
38
|
+
when 12, 15, 19 then Trackerific::FedEx
|
39
|
+
else nil
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
32
43
|
end
|
data/lib/ups.rb
CHANGED
@@ -2,10 +2,12 @@ module Trackerific
|
|
2
2
|
require 'httparty'
|
3
3
|
|
4
4
|
class UPS < Base
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
include ::HTTParty
|
6
|
+
format :xml
|
7
|
+
base_uri defined?(Rails) ? case Rails.env
|
8
|
+
when 'test','development' then 'https://wwwcie.ups.com/ups.app/xml'
|
9
|
+
when 'production' then 'https://www.ups.com/ups.app/xml'
|
10
|
+
end : 'https://www.ups.com/ups.app/xml'
|
9
11
|
|
10
12
|
def required_options
|
11
13
|
[:key, :user_id, :password]
|
@@ -13,7 +15,7 @@ module Trackerific
|
|
13
15
|
|
14
16
|
def track_package(package_id)
|
15
17
|
super
|
16
|
-
http_response =
|
18
|
+
http_response = self.class.post('/Track', :body => build_xml_request)
|
17
19
|
http_response.error! unless http_response.code == 200
|
18
20
|
case http_response['TrackResponse']['Response']['ResponseStatusCode']
|
19
21
|
when "0" then raise Trackerific::Error, parse_error_response(http_response)
|
@@ -66,16 +68,5 @@ module Trackerific
|
|
66
68
|
end
|
67
69
|
return xml
|
68
70
|
end
|
69
|
-
|
70
|
-
private
|
71
|
-
|
72
|
-
class HTTP
|
73
|
-
include ::HTTParty
|
74
|
-
format :xml
|
75
|
-
base_uri case Rails.env
|
76
|
-
when 'test','development' then 'https://wwwcie.ups.com/ups.app/xml'
|
77
|
-
when 'production' then 'https://www.ups.com/ups.app/xml'
|
78
|
-
end
|
79
|
-
end
|
80
71
|
end
|
81
72
|
end
|
data/lib/usps.rb
CHANGED
@@ -3,8 +3,12 @@ module Trackerific
|
|
3
3
|
require 'httparty'
|
4
4
|
|
5
5
|
class USPS < Base
|
6
|
-
|
7
|
-
|
6
|
+
include HTTParty
|
7
|
+
format :xml
|
8
|
+
base_uri defined?(Rails) ? case Rails.env
|
9
|
+
when 'test', 'development' then 'http://testing.shippingapis.com'
|
10
|
+
when 'production' then 'https://secure.shippingapis.com'
|
11
|
+
end : 'https://secure.shippingapis.com'
|
8
12
|
|
9
13
|
def initialize(options = {})
|
10
14
|
super
|
@@ -17,7 +21,7 @@ module Trackerific
|
|
17
21
|
|
18
22
|
def track_package(package_id)
|
19
23
|
super
|
20
|
-
response =
|
24
|
+
response = self.class.get('/ShippingAPITest.dll', :query => {:API => 'TrackV2', :XML => build_xml_request}.to_query)
|
21
25
|
response.error! unless response.code == 200
|
22
26
|
raise Trackerific::Error, response['Error']['Description'] unless response['Error'].nil?
|
23
27
|
raise Trackerific::Error, "Tracking information not found in response from server." if response['TrackResponse'].nil?
|
@@ -40,17 +44,6 @@ module Trackerific
|
|
40
44
|
return tracking_request
|
41
45
|
end
|
42
46
|
|
43
|
-
private
|
44
|
-
|
45
|
-
class HTTP
|
46
|
-
include HTTParty
|
47
|
-
format :xml
|
48
|
-
base_uri case Rails.env
|
49
|
-
when 'test', 'development' then 'http://testing.shippingapis.com'
|
50
|
-
when 'production' then 'https://secure.shippingapis.com'
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
47
|
end
|
55
48
|
|
56
49
|
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<FDXTrack2Reply xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
3
|
+
<Error>
|
4
|
+
<Code>6035</Code>
|
5
|
+
<Message>Invalid tracking numbers. Please check the following numbers and resubmit.</Message>
|
6
|
+
<LocalLanguageMessage>Invalid tracking numbers. Please check the following numbers and resubmit.</LocalLanguageMessage>
|
7
|
+
</Error>
|
8
|
+
<DuplicateWaybill>false</DuplicateWaybill>
|
9
|
+
<MoreData>false</MoreData>
|
10
|
+
</FDXTrack2Reply>
|
@@ -1,12 +1,74 @@
|
|
1
1
|
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
-
<
|
3
|
-
<
|
4
|
-
<
|
5
|
-
<
|
6
|
-
</
|
7
|
-
</
|
8
|
-
<
|
9
|
-
<
|
10
|
-
<
|
11
|
-
<
|
12
|
-
|
2
|
+
<FDXTrack2Reply xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
3
|
+
<DuplicateWaybill>false</DuplicateWaybill>
|
4
|
+
<MoreData>false</MoreData>
|
5
|
+
<Package>
|
6
|
+
<TrackingNumber>183689015000001</TrackingNumber>
|
7
|
+
<TrackingNumberUniqueIdentifier>12010~183689015000001</TrackingNumberUniqueIdentifier>
|
8
|
+
<StatusCode>DL</StatusCode>
|
9
|
+
<StatusDescription>Delivered</StatusDescription>
|
10
|
+
<CarrierCode>FDXG</CarrierCode>
|
11
|
+
<OtherIdentifier>
|
12
|
+
<Value>770-886-6395</Value>
|
13
|
+
<Type>CUR</Type>
|
14
|
+
</OtherIdentifier>
|
15
|
+
<Service>FedEx Home Delivery</Service>
|
16
|
+
<Weight>19.8</Weight>
|
17
|
+
<WeightUnits>LBS</WeightUnits>
|
18
|
+
<PackagingDescription>Package</PackagingDescription>
|
19
|
+
<PackageSequenceNumber>1</PackageSequenceNumber>
|
20
|
+
<PackageCount>1</PackageCount>
|
21
|
+
<OriginLocationAddress>
|
22
|
+
<City>ANAHEIM</City>
|
23
|
+
<StateOrProvinceCode>CA</StateOrProvinceCode>
|
24
|
+
<CountryCode>US</CountryCode>
|
25
|
+
</OriginLocationAddress>
|
26
|
+
<ShipDate>2010-06-25</ShipDate>
|
27
|
+
<DestinationAddress>
|
28
|
+
<City>Gainesville</City>
|
29
|
+
<StateOrProvinceCode>GA</StateOrProvinceCode>
|
30
|
+
<CountryCode>US</CountryCode>
|
31
|
+
</DestinationAddress>
|
32
|
+
<DeliveredDate>2010-07-01</DeliveredDate>
|
33
|
+
<DeliveredTime>10:43:51</DeliveredTime>
|
34
|
+
<SignatureProofOfDeliveryAvailable>false</SignatureProofOfDeliveryAvailable>
|
35
|
+
<Event>
|
36
|
+
<Date>2010-07-01</Date>
|
37
|
+
<Time>10:43:51</Time>
|
38
|
+
<Type>DL</Type>
|
39
|
+
<Description>Delivered</Description>
|
40
|
+
<StatusExceptionCode>014</StatusExceptionCode>
|
41
|
+
<StatusExceptionDescription>Left at garage. Signature Service not requested.</StatusExceptionDescription>
|
42
|
+
<Address>
|
43
|
+
<City>Gainesville</City>
|
44
|
+
<StateOrProvinceCode>GA</StateOrProvinceCode>
|
45
|
+
<PostalCode>30506</PostalCode>
|
46
|
+
<CountryCode>US</CountryCode>
|
47
|
+
</Address>
|
48
|
+
</Event>
|
49
|
+
<Event>
|
50
|
+
<Date>2010-07-01</Date>
|
51
|
+
<Time>08:48:00</Time>
|
52
|
+
<Type>OD</Type>
|
53
|
+
<Description>On FedEx vehicle for delivery</Description>
|
54
|
+
<Address>
|
55
|
+
<City>ATHENS</City>
|
56
|
+
<StateOrProvinceCode>GA</StateOrProvinceCode>
|
57
|
+
<PostalCode>30601</PostalCode>
|
58
|
+
<CountryCode>US</CountryCode>
|
59
|
+
</Address>
|
60
|
+
</Event>
|
61
|
+
<Event>
|
62
|
+
<Date>2010-07-01</Date>
|
63
|
+
<Time>05:07:00</Time>
|
64
|
+
<Type>AR</Type>
|
65
|
+
<Description>At local FedEx facility</Description>
|
66
|
+
<Address>
|
67
|
+
<City>ATHENS</City>
|
68
|
+
<StateOrProvinceCode>GA</StateOrProvinceCode>
|
69
|
+
<PostalCode>30601</PostalCode>
|
70
|
+
<CountryCode>US</CountryCode>
|
71
|
+
</Address>
|
72
|
+
</Event>
|
73
|
+
</Package>
|
74
|
+
</FDXTrack2Reply>
|