trackerific 0.6.2 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.rspec +1 -2
- data/Gemfile +3 -22
- data/Gemfile.lock +33 -107
- data/README.rdoc +35 -61
- data/Rakefile +10 -36
- data/lib/trackerific/configuration.rb +3 -46
- data/lib/trackerific/details.rb +2 -71
- data/lib/trackerific/event.rb +5 -49
- data/lib/trackerific/services/base.rb +45 -0
- data/lib/trackerific/services/fedex.rb +75 -85
- data/lib/trackerific/services/mock_service.rb +43 -54
- data/lib/trackerific/services/ups.rb +94 -103
- data/lib/trackerific/services/usps.rb +182 -186
- data/lib/trackerific/services.rb +41 -0
- data/lib/trackerific/version.rb +3 -0
- data/lib/trackerific.rb +30 -73
- data/spec/fixtures/{fedex_error_response.xml → fedex/error.xml} +0 -0
- data/spec/fixtures/{fedex_success_response.xml → fedex/success.xml} +0 -0
- data/spec/fixtures/{ups_error_response.xml → ups/error.xml} +0 -0
- data/spec/fixtures/{ups_success_response.xml → ups/success.xml} +0 -0
- data/spec/fixtures/{usps_city_state_lookup_response.xml → usps/city_state_lookup.xml} +0 -0
- data/spec/fixtures/{usps_error_response.xml → usps/error.xml} +0 -0
- data/spec/fixtures/{usps_success_response.xml → usps/success.xml} +0 -0
- data/spec/lib/trackerific/configuration_spec.rb +8 -39
- data/spec/lib/trackerific/details_spec.rb +13 -78
- data/spec/lib/trackerific/error_spec.rb +1 -5
- data/spec/lib/trackerific/event_spec.rb +14 -43
- data/spec/lib/trackerific/services/base_spec.rb +13 -0
- data/spec/lib/trackerific/services/fedex_spec.rb +59 -62
- data/spec/lib/trackerific/services/mock_service_spec.rb +33 -46
- data/spec/lib/trackerific/services/ups_spec.rb +46 -66
- data/spec/lib/trackerific/services/usps_spec.rb +80 -94
- data/spec/lib/trackerific/services_spec.rb +37 -0
- data/spec/lib/trackerific/version_spec.rb +5 -0
- data/spec/lib/trackerific_spec.rb +15 -53
- data/spec/spec_helper.rb +2 -9
- data/spec/support/fixtures.rb +4 -18
- data/spec/support/test_services.rb +23 -0
- data/trackerific.gemspec +24 -102
- metadata +151 -185
- data/VERSION +0 -1
- data/changelog +0 -9
- data/examples/custom_service_spec.rb +0 -44
- data/lib/helpers/options_helper.rb +0 -23
- data/lib/trackerific/service.rb +0 -100
- data/spec/lib/helpers/options_helper_spec.rb +0 -82
- data/spec/lib/trackerific/service_spec.rb +0 -44
- data/spec/support/trackerific.rb +0 -21
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8805756f860d3296a7f8c74ccc73fa761306f5eb
|
4
|
+
data.tar.gz: 6983a95593bd7d48fcc090e6e6cf586829549647
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: aa16b48e0a34e396275a02d77109dc8d582ff2c219a7de922dd2d67c4dfe6541abca0892741d2a631976b53867c8ecc6acff9abb18199bc3155c2e79e4a6013b
|
7
|
+
data.tar.gz: 935b59bd04fd1286160f3fc823a4c3ebdd79819e42c820381cbcaa31f2b990c554f42bc658bbe400e8104ca645b07f87ec90a6cc5c5472ca7216af5bdbc424c3
|
data/.gitignore
ADDED
data/.rspec
CHANGED
@@ -1,2 +1 @@
|
|
1
|
-
--
|
2
|
-
--format d
|
1
|
+
--color
|
data/Gemfile
CHANGED
@@ -1,23 +1,4 @@
|
|
1
|
-
source
|
1
|
+
source 'https://rubygems.org'
|
2
2
|
|
3
|
-
gem
|
4
|
-
|
5
|
-
gem "httparty", ">= 0.7.7"
|
6
|
-
gem "builder", ">= 2.1.2"
|
7
|
-
|
8
|
-
group :development do
|
9
|
-
gem "bundler", ">= 1.0.13"
|
10
|
-
gem "jeweler", ">= 1.5.2"
|
11
|
-
gem 'yardstick', ">= 0.4.0"
|
12
|
-
end
|
13
|
-
|
14
|
-
group :development, :test do
|
15
|
-
gem "rspec-rails", ">= 2.6.1"
|
16
|
-
gem "rspec_multi_matchers", ">= 1.1.0"
|
17
|
-
gem "ruby-debug19", ">= 0.11.6", :require => "ruby-debug"
|
18
|
-
end
|
19
|
-
|
20
|
-
group :test do
|
21
|
-
gem "fakeweb", ">= 1.3.0"
|
22
|
-
gem 'simplecov', '>= 0.4.0', :require => false
|
23
|
-
end
|
3
|
+
# Specify your gem's dependencies in trackerific.gemspec
|
4
|
+
gemspec
|
data/Gemfile.lock
CHANGED
@@ -1,77 +1,32 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
trackerific (0.7.0)
|
5
|
+
activesupport
|
6
|
+
builder (~> 3.2.2)
|
7
|
+
httparty (~> 0.12.0)
|
8
|
+
|
1
9
|
GEM
|
2
|
-
remote:
|
10
|
+
remote: https://rubygems.org/
|
3
11
|
specs:
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
erubis (~> 2.6.6)
|
13
|
-
i18n (~> 0.5.0)
|
14
|
-
rack (~> 1.2.1)
|
15
|
-
rack-mount (~> 0.6.14)
|
16
|
-
rack-test (~> 0.5.7)
|
17
|
-
tzinfo (~> 0.3.23)
|
18
|
-
activemodel (3.0.8)
|
19
|
-
activesupport (= 3.0.8)
|
20
|
-
builder (~> 2.1.2)
|
21
|
-
i18n (~> 0.5.0)
|
22
|
-
activerecord (3.0.8)
|
23
|
-
activemodel (= 3.0.8)
|
24
|
-
activesupport (= 3.0.8)
|
25
|
-
arel (~> 2.0.10)
|
26
|
-
tzinfo (~> 0.3.23)
|
27
|
-
activeresource (3.0.8)
|
28
|
-
activemodel (= 3.0.8)
|
29
|
-
activesupport (= 3.0.8)
|
30
|
-
activesupport (3.0.8)
|
31
|
-
archive-tar-minitar (0.5.2)
|
32
|
-
arel (2.0.10)
|
33
|
-
builder (2.1.2)
|
34
|
-
columnize (0.3.3)
|
35
|
-
crack (0.1.8)
|
12
|
+
activesupport (4.0.0)
|
13
|
+
i18n (~> 0.6, >= 0.6.4)
|
14
|
+
minitest (~> 4.2)
|
15
|
+
multi_json (~> 1.3)
|
16
|
+
thread_safe (~> 0.1)
|
17
|
+
tzinfo (~> 0.3.37)
|
18
|
+
atomic (1.1.14)
|
19
|
+
builder (3.2.2)
|
36
20
|
diff-lcs (1.1.2)
|
37
|
-
erubis (2.6.6)
|
38
|
-
abstract (>= 1.0.0)
|
39
21
|
fakeweb (1.3.0)
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
i18n (0.5
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
linecache19 (0.5.12)
|
49
|
-
ruby_core_source (>= 0.1.4)
|
50
|
-
mail (2.2.19)
|
51
|
-
activesupport (>= 2.3.6)
|
52
|
-
i18n (>= 0.4.0)
|
53
|
-
mime-types (~> 1.16)
|
54
|
-
treetop (~> 1.4.8)
|
55
|
-
mime-types (1.16)
|
56
|
-
polyglot (0.3.1)
|
57
|
-
rack (1.2.3)
|
58
|
-
rack-mount (0.6.14)
|
59
|
-
rack (>= 1.0.0)
|
60
|
-
rack-test (0.5.7)
|
61
|
-
rack (>= 1.0)
|
62
|
-
rails (3.0.8)
|
63
|
-
actionmailer (= 3.0.8)
|
64
|
-
actionpack (= 3.0.8)
|
65
|
-
activerecord (= 3.0.8)
|
66
|
-
activeresource (= 3.0.8)
|
67
|
-
activesupport (= 3.0.8)
|
68
|
-
bundler (~> 1.0)
|
69
|
-
railties (= 3.0.8)
|
70
|
-
railties (3.0.8)
|
71
|
-
actionpack (= 3.0.8)
|
72
|
-
activesupport (= 3.0.8)
|
73
|
-
rake (>= 0.8.7)
|
74
|
-
thor (~> 0.14.4)
|
22
|
+
httparty (0.12.0)
|
23
|
+
json (~> 1.8)
|
24
|
+
multi_xml (>= 0.5.2)
|
25
|
+
i18n (0.6.5)
|
26
|
+
json (1.8.0)
|
27
|
+
minitest (4.7.5)
|
28
|
+
multi_json (1.8.1)
|
29
|
+
multi_xml (0.5.5)
|
75
30
|
rake (0.9.2)
|
76
31
|
rspec (2.6.0)
|
77
32
|
rspec-core (~> 2.6.0)
|
@@ -81,45 +36,16 @@ GEM
|
|
81
36
|
rspec-expectations (2.6.0)
|
82
37
|
diff-lcs (~> 1.1.2)
|
83
38
|
rspec-mocks (2.6.0)
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
railties (~> 3.0)
|
88
|
-
rspec (~> 2.6.0)
|
89
|
-
rspec_multi_matchers (1.1.0)
|
90
|
-
ruby-debug-base19 (0.11.25)
|
91
|
-
columnize (>= 0.3.1)
|
92
|
-
linecache19 (>= 0.5.11)
|
93
|
-
ruby_core_source (>= 0.1.4)
|
94
|
-
ruby-debug19 (0.11.6)
|
95
|
-
columnize (>= 0.3.1)
|
96
|
-
linecache19 (>= 0.5.11)
|
97
|
-
ruby-debug-base19 (>= 0.11.19)
|
98
|
-
ruby_core_source (0.1.5)
|
99
|
-
archive-tar-minitar (>= 0.5.2)
|
100
|
-
simplecov (0.4.2)
|
101
|
-
simplecov-html (~> 0.4.4)
|
102
|
-
simplecov-html (0.4.5)
|
103
|
-
thor (0.14.6)
|
104
|
-
treetop (1.4.9)
|
105
|
-
polyglot (>= 0.3.1)
|
106
|
-
tzinfo (0.3.28)
|
107
|
-
yard (0.7.1)
|
108
|
-
yardstick (0.4.0)
|
109
|
-
yard (~> 0.7.0)
|
39
|
+
thread_safe (0.1.3)
|
40
|
+
atomic
|
41
|
+
tzinfo (0.3.38)
|
110
42
|
|
111
43
|
PLATFORMS
|
112
44
|
ruby
|
113
45
|
|
114
46
|
DEPENDENCIES
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
rails (>= 3.0.0)
|
121
|
-
rspec-rails (>= 2.6.1)
|
122
|
-
rspec_multi_matchers (>= 1.1.0)
|
123
|
-
ruby-debug19 (>= 0.11.6)
|
124
|
-
simplecov (>= 0.4.0)
|
125
|
-
yardstick (>= 0.4.0)
|
47
|
+
bundler (>= 1.3.5)
|
48
|
+
fakeweb (~> 1.3.0)
|
49
|
+
rake
|
50
|
+
rspec (~> 2.6.0)
|
51
|
+
trackerific!
|
data/README.rdoc
CHANGED
@@ -5,30 +5,22 @@ To use this gem, add this line to your Gemfile
|
|
5
5
|
and then run
|
6
6
|
bundle install
|
7
7
|
|
8
|
-
== Usage
|
9
|
-
|
10
|
-
If you would like to see a functional example of this gem in action, check out the
|
11
|
-
sample Rails app here: {package-tracker}[https://github.com/travishaynes/package-tracker]
|
12
|
-
|
13
8
|
=== Configuration
|
14
9
|
|
15
|
-
|
16
|
-
configure your credentials for each service.
|
10
|
+
You will need to configure your credentials for each service:
|
17
11
|
|
18
|
-
# config/initializers/trackerific.rb
|
19
|
-
require 'trackerific'
|
20
12
|
Trackerific.configure do |config|
|
21
13
|
config.fedex :account => 'account',
|
22
14
|
:meter => '123456789'
|
23
|
-
|
15
|
+
|
24
16
|
config.ups :key => 'key',
|
25
17
|
:user_id => 'userid',
|
26
18
|
:password => 'secret'
|
27
|
-
|
19
|
+
|
28
20
|
config.usps :user_id => 'userid',
|
29
21
|
:use_city_state_lookup => true
|
30
22
|
end
|
31
|
-
|
23
|
+
|
32
24
|
For USPS packages, the option :use_city_state_lookup defaults to false, and will
|
33
25
|
only work if you have access to USPS's CityStateLookup API. If you can enable
|
34
26
|
it, this feature will provide the location for USPS package events.
|
@@ -37,36 +29,36 @@ it, this feature will provide the location for USPS package events.
|
|
37
29
|
|
38
30
|
Once you configured the services, tracking a package is as easy as
|
39
31
|
|
40
|
-
|
41
|
-
details = track_package "package id"
|
32
|
+
details = Trackerific.track("package id")
|
42
33
|
|
43
34
|
=== Finding a Tracking Service Provider
|
44
35
|
|
45
|
-
If you do not know the tracking service provider of a package id
|
46
|
-
|
47
|
-
use the tracking_service helper method.
|
36
|
+
If you do not know the tracking service provider of a package id you can use
|
37
|
+
Trackerific::Services.find_by_tracking_id:
|
48
38
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
tracking_service "unknown package id" # => nil
|
39
|
+
Trackerific::Services.find_by_tracking_id("183689015000001") # => Trackerific::Services::FedEx
|
40
|
+
Trackerific::Services.find_by_tracking_id("1Z12345E0291980793") # => Trackerific::Services::UPS
|
41
|
+
Trackerific::Services.find_by_tracking_id("EJ958083578US") # => Trackerific::Services::USPS
|
42
|
+
Trackerific::Services.find_by_tracking_id("unknown package id") # => nil
|
54
43
|
|
55
44
|
=== Tracking a Package with a Specific Service
|
56
45
|
|
57
46
|
Use this method if you need to specify exactly which service to track a package.
|
58
47
|
|
59
48
|
# Track a FedEx package:
|
60
|
-
|
61
|
-
|
62
|
-
|
49
|
+
details = Trackerific::Services::FedEx.track('183689015000001')
|
50
|
+
|
63
51
|
# Track a USPS package:
|
64
|
-
|
65
|
-
|
66
|
-
|
52
|
+
details = Trackerific::Services::USPS.track('EJ958083578US')
|
53
|
+
|
67
54
|
# Track a UPS package:
|
68
|
-
|
69
|
-
|
55
|
+
details = Trackerific::Services::UPS.track('1Z12345E0291980793')
|
56
|
+
|
57
|
+
Using #track will automatically read the credentials from
|
58
|
+
`Trackerific.configuration`. If you need to assign them manually, use #new:
|
59
|
+
|
60
|
+
fedex = Trackerific::Services::FedEx.new account: "account", meter: "123456789"
|
61
|
+
details = fedex.track('183689015000001')
|
70
62
|
|
71
63
|
=== Tracking Details
|
72
64
|
|
@@ -100,7 +92,7 @@ recent events will always be at the top of the list.
|
|
100
92
|
If you have access to the USPS CityStateLookup API, you can use Trackerific to
|
101
93
|
look up the city and state of a zipcode.
|
102
94
|
|
103
|
-
usps = Trackerific::USPS.new :user_id => 'userid'
|
95
|
+
usps = Trackerific::Services::USPS.new :user_id => 'userid'
|
104
96
|
usps.city_state_lookup "90210" # => { :city => 'BEVERLY HILLS', :state => 'CA', :zip => '90210' }
|
105
97
|
|
106
98
|
=== Exception handling
|
@@ -111,43 +103,25 @@ tracking number to their system, a Trackerific::Error will be raised. Here's an
|
|
111
103
|
example on how to handle Trackerific::Errors:
|
112
104
|
|
113
105
|
begin
|
114
|
-
|
106
|
+
details = Trackerific.track('EJ958083578US')
|
107
|
+
# handle details here
|
115
108
|
rescue Trackerific::Error => e
|
116
109
|
puts e.message
|
117
110
|
end
|
118
111
|
|
119
|
-
|
120
|
-
|
121
|
-
# in app/controllers/application_controller.rb
|
122
|
-
rescue_from Trackerific::Error do |exception|
|
123
|
-
redirect_to root_url, :alert => exception.message
|
124
|
-
end
|
125
|
-
|
126
|
-
== Writing a Custom Service
|
127
|
-
|
128
|
-
See the examples folder for an example of a custom service.
|
129
|
-
|
130
|
-
=== Testing with Trackerific
|
112
|
+
=== Testing
|
131
113
|
|
132
114
|
Trackerific provides a mocked service you can use in your unit tests of your
|
133
|
-
application.
|
134
|
-
environments only, and disabled in production.
|
115
|
+
application. You must require the service manually.
|
135
116
|
|
117
|
+
require 'trackerific/services/mock_service'
|
136
118
|
details = track_package("XXXXXXXXXX") # => returns a populated Trackerific::Details
|
137
|
-
details = track_package("XXXxxxxxxx") # => throws a Trackerific::
|
138
|
-
|
139
|
-
== Contributing to trackerific
|
140
|
-
|
141
|
-
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
142
|
-
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
143
|
-
* Fork the project
|
144
|
-
* Start a feature/bugfix branch
|
145
|
-
* Commit and push until you are happy with your contribution
|
146
|
-
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
147
|
-
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
148
|
-
|
149
|
-
== Copyright
|
119
|
+
details = track_package("XXXxxxxxxx") # => throws a Trackerific::Error exception
|
150
120
|
|
151
|
-
|
152
|
-
further details.
|
121
|
+
== Contributing
|
153
122
|
|
123
|
+
1. Fork it
|
124
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
125
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
126
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
127
|
+
5. Create new Pull Request
|
data/Rakefile
CHANGED
@@ -1,40 +1,14 @@
|
|
1
|
-
require '
|
2
|
-
require '
|
3
|
-
begin
|
4
|
-
Bundler.setup(:default, :development)
|
5
|
-
rescue Bundler::BundlerError => e
|
6
|
-
$stderr.puts e.message
|
7
|
-
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
-
exit e.status_code
|
9
|
-
end
|
10
|
-
require 'rake'
|
11
|
-
|
12
|
-
require 'jeweler'
|
13
|
-
Jeweler::Tasks.new do |gem|
|
14
|
-
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
-
gem.name = "trackerific"
|
16
|
-
gem.homepage = "http://github.com/travishaynes/trackerific"
|
17
|
-
gem.license = "MIT"
|
18
|
-
gem.summary = %Q{Trackerific provides package tracking to Rails.}
|
19
|
-
gem.description = %Q{Package tracking made easy for Rails. Currently supported services include FedEx, UPS, and USPS.}
|
20
|
-
gem.email = "travis.j.haynes@gmail.com"
|
21
|
-
gem.authors = ["Travis Haynes"]
|
22
|
-
gem.rubyforge_project = "trackerific"
|
23
|
-
end
|
24
|
-
Jeweler::RubygemsDotOrgTasks.new
|
25
|
-
|
26
|
-
# measure coverage
|
27
|
-
|
28
|
-
require 'yardstick/rake/measurement'
|
29
|
-
|
30
|
-
Yardstick::Rake::Measurement.new(:yardstick_measure) do |measurement|
|
31
|
-
measurement.output = 'measurement/report.txt'
|
32
|
-
end
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rspec/core/rake_task'
|
33
3
|
|
34
|
-
|
4
|
+
RSpec::Core::RakeTask.new(:spec)
|
35
5
|
|
36
|
-
|
6
|
+
task :default => :spec
|
37
7
|
|
38
|
-
|
39
|
-
|
8
|
+
desc 'Starts an IRB console with Trackerific loaded'
|
9
|
+
task :console do
|
10
|
+
require 'irb'
|
11
|
+
require 'trackerific'
|
12
|
+
ARGV.clear
|
13
|
+
IRB.start
|
40
14
|
end
|
@@ -1,50 +1,7 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
|
1
3
|
module Trackerific
|
2
|
-
# Provides a dynamic configuration
|
3
4
|
class Configuration
|
4
|
-
include
|
5
|
-
# Creates a new instance of Trackerific::Configuration
|
6
|
-
# @api private
|
7
|
-
def initialize
|
8
|
-
@options = {}
|
9
|
-
end
|
10
|
-
# Overriding the method_missing method allows dynamic methods
|
11
|
-
# @return [Hash]
|
12
|
-
# @api private
|
13
|
-
def method_missing(sym, *args, &block)
|
14
|
-
# Get a list of all the services (convert symbols to lower case)
|
15
|
-
services = Trackerific.services.map { |service| service.to_s.downcase.to_sym }
|
16
|
-
# Do not accept any configuration values for anything except services
|
17
|
-
raise NoMethodError unless services.include? sym
|
18
|
-
unless args.empty?
|
19
|
-
# Only accept Hashes
|
20
|
-
raise ArgumentError unless args[0].class == Hash
|
21
|
-
# Validate configuration values against the required parameters for that service
|
22
|
-
service = Trackerific.service_get(sym)
|
23
|
-
validate_options args[0], service.required_parameters, service.valid_options
|
24
|
-
# Store the configuration options
|
25
|
-
@options[sym] = args[0]
|
26
|
-
end
|
27
|
-
# return the configuration options
|
28
|
-
@options[sym]
|
29
|
-
end
|
30
|
-
end
|
31
|
-
class << self
|
32
|
-
# Stores the configuration options for Trackerific
|
33
|
-
# @return [Trackerific::Configuration]
|
34
|
-
# @api private
|
35
|
-
def configuration
|
36
|
-
@configuration ||= Trackerific::Configuration.new
|
37
|
-
end
|
38
|
-
# Configures Trackerific
|
39
|
-
# @return [Trackerific::Configuration]
|
40
|
-
# @example Defining credentials
|
41
|
-
# Trackerific.configure do |config|
|
42
|
-
# config.fedex :meter => '123456789', :account => '123456789'
|
43
|
-
# end
|
44
|
-
# @api public
|
45
|
-
def configure
|
46
|
-
yield configuration
|
47
|
-
configuration
|
48
|
-
end
|
5
|
+
include ActiveSupport::Configurable
|
49
6
|
end
|
50
7
|
end
|
data/lib/trackerific/details.rb
CHANGED
@@ -1,75 +1,6 @@
|
|
1
1
|
module Trackerific
|
2
2
|
# Details returned when tracking a package. Stores the package identifier,
|
3
|
-
# a summary, and the events.
|
4
|
-
class Details
|
5
|
-
include OptionsHelper
|
6
|
-
|
7
|
-
# Provides a new instance of Details
|
8
|
-
# @param [Hash] details The details for this package
|
9
|
-
# @api private
|
10
|
-
def initialize(details = {})
|
11
|
-
required = [:package_id, :summary, :events]
|
12
|
-
valid = required + [:weight, :via]
|
13
|
-
validate_options(details, required, valid)
|
14
|
-
@package_id = details[:package_id]
|
15
|
-
@summary = details[:summary]
|
16
|
-
@events = details[:events]
|
17
|
-
@weight = details[:weight] || nil
|
18
|
-
@via = details[:via] || nil
|
19
|
-
end
|
20
|
-
|
21
|
-
# The package identifier
|
22
|
-
# @example Get the id of a tracked package
|
23
|
-
# details.package_id # => the package identifier
|
24
|
-
# @return [String] the package identifier
|
25
|
-
# @api public
|
26
|
-
def package_id
|
27
|
-
@package_id
|
28
|
-
end
|
29
|
-
|
30
|
-
# Summary of the package's tracking events
|
31
|
-
# @example Get the summary of a tracked package
|
32
|
-
# details.summary # => Summary of the tracking events (i.e. Delivered)
|
33
|
-
# @return [String] a summary of the tracking status
|
34
|
-
# @api public
|
35
|
-
def summary
|
36
|
-
@summary
|
37
|
-
end
|
38
|
-
|
39
|
-
# The events for this package
|
40
|
-
# @example Print all the events for a tracked package
|
41
|
-
# puts details.events
|
42
|
-
# @example Get the date the package was shipped
|
43
|
-
# details.events.last.date # => a DateTime value
|
44
|
-
# @example A bulleted HTML list of the events (most current on top) in haml
|
45
|
-
# %ul
|
46
|
-
# - details.events.each do |event|
|
47
|
-
# %li= event
|
48
|
-
# @return [Array, Trackerific::Event] the tracking events
|
49
|
-
# @api public
|
50
|
-
def events
|
51
|
-
@events
|
52
|
-
end
|
53
|
-
|
54
|
-
# The weight of the package (may not be supported by all services)
|
55
|
-
# @example Get the weight of a package
|
56
|
-
# details.weight[:weight] # => the weight
|
57
|
-
# details.weight[:units] # => the units of measurement for the weight (i.e. "LBS")
|
58
|
-
# @return [Hash] Example: { units: 'LBS', weight: 19.1 }
|
59
|
-
# @api public
|
60
|
-
def weight
|
61
|
-
@weight
|
62
|
-
end
|
63
|
-
|
64
|
-
# Example: UPS 2ND DAY AIR. May not be supported by all services
|
65
|
-
# @example Get how the package was shipped
|
66
|
-
# ups = Trackerific::UPS.new :user_id => "userid"
|
67
|
-
# details = ups.track_package "1Z12345E0291980793"
|
68
|
-
# details.via # => "UPS 2ND DAY AIR"
|
69
|
-
# @return [String] The service used to ship the package
|
70
|
-
# @api public
|
71
|
-
def via
|
72
|
-
@via
|
73
|
-
end
|
3
|
+
# a summary, and the events, etc.
|
4
|
+
class Details < Struct.new(:package_id, :summary, :events, :weight, :via)
|
74
5
|
end
|
75
6
|
end
|
data/lib/trackerific/event.rb
CHANGED
@@ -1,60 +1,16 @@
|
|
1
1
|
module Trackerific
|
2
2
|
# Provides details for a tracking event
|
3
|
-
class Event
|
4
|
-
include OptionsHelper
|
5
|
-
|
6
|
-
# Provides a new instance of Event
|
7
|
-
# @param [Hash] details The details of the event
|
8
|
-
# @api private
|
9
|
-
def initialize(details = {})
|
10
|
-
required_details = [:date, :description, :location]
|
11
|
-
validate_options details, required_details
|
12
|
-
@date = details[:date]
|
13
|
-
@description = details[:description]
|
14
|
-
@location = details[:location]
|
15
|
-
end
|
16
|
-
|
17
|
-
# The date and time of the event
|
18
|
-
# @example Get the date of an event
|
19
|
-
# date = details.events.first.date
|
20
|
-
# @return [DateTime]
|
21
|
-
# @api public
|
22
|
-
def date
|
23
|
-
@date
|
24
|
-
end
|
25
|
-
|
26
|
-
# The event's description
|
27
|
-
# @example Get the description of an event
|
28
|
-
# description = details.events.first.description
|
29
|
-
# @return [String]
|
30
|
-
# @api public
|
31
|
-
def description
|
32
|
-
@description
|
33
|
-
end
|
34
|
-
|
35
|
-
# Where the event took place (usually in City State Zip format)
|
36
|
-
# @example Get the location of an event
|
37
|
-
# location = details.events.first.location
|
38
|
-
# @return [String]
|
39
|
-
# @api public
|
40
|
-
def location
|
41
|
-
@location
|
42
|
-
end
|
43
|
-
|
3
|
+
class Event < Struct.new(:date, :description, :location)
|
44
4
|
# Converts the event into a string
|
45
5
|
# @example Get a human-readable string from an event
|
46
6
|
# event = details.event.to_s
|
47
|
-
# @example A bulleted list of events in haml
|
48
|
-
# %ul
|
49
|
-
# - details.events.each do |event|
|
50
|
-
# %li= event
|
51
7
|
# @return [String] converts the event into a string
|
52
8
|
# @api public
|
53
9
|
def to_s
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
"
|
10
|
+
[ date.strftime('%b %d %I:%M %P'),
|
11
|
+
description,
|
12
|
+
location
|
13
|
+
].join(" ")
|
58
14
|
end
|
59
15
|
end
|
60
16
|
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Trackerific
|
2
|
+
module Services
|
3
|
+
class Base
|
4
|
+
class << self
|
5
|
+
attr_accessor :name
|
6
|
+
|
7
|
+
# Registers the service with Trackerific
|
8
|
+
# @api semipublic
|
9
|
+
def register(name)
|
10
|
+
self.name = name.to_sym
|
11
|
+
Trackerific::Services[self.name] = self
|
12
|
+
end
|
13
|
+
|
14
|
+
def track(id)
|
15
|
+
options = Trackerific.configuration[self.name] || {}
|
16
|
+
new(options).track(id)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Checks if the given package ID can be tracked by this service
|
20
|
+
# @param [String] id The package ID
|
21
|
+
# @return [Boolean] true when this service can track the given ID
|
22
|
+
def can_track?(id)
|
23
|
+
package_id_matchers.each {|m| return true if id =~ m }
|
24
|
+
false
|
25
|
+
end
|
26
|
+
|
27
|
+
# An Array of Regexp that matches valid package ids for the service
|
28
|
+
# @api semipublic
|
29
|
+
def package_id_matchers
|
30
|
+
raise NotImplementedError,
|
31
|
+
"You must implement this method in your service", caller
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# Gets the tracking information for the package from the server
|
37
|
+
# @param [String] id The package identifier
|
38
|
+
# @return [Trackerific::Details] The tracking details
|
39
|
+
# @api semipublic
|
40
|
+
def track(id)
|
41
|
+
raise NotImplementedError,
|
42
|
+
"You must implement this method in your service", caller
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|