trackerific 0.4.2 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -7,6 +7,9 @@ and then run
7
7
 
8
8
  == Usage
9
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
+
10
13
  === Configuration
11
14
 
12
15
  To take advantage of Trackerific's automatic service discovery, you will need to
@@ -149,6 +152,14 @@ of the comments of your code. You can use the rake task:
149
152
  rake yardstick_measure
150
153
  which will generate a measurement/report.txt file.
151
154
 
155
+ === Testing with Trackerific
156
+
157
+ Trackerific provides a mocked service you can use in your unit tests of your
158
+ application. The mocked service will be available in development and test
159
+ environments only, and disabled in production.
160
+
161
+ details = track_package("XXXXXXXXXX") # => returns a populated Trackerific::Details
162
+ details = track_package("XXXxxxxxxx") # => throws a Trackerific::Errror exception
152
163
 
153
164
  == Contributing to trackerific
154
165
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.2
1
+ 0.5.0
@@ -3,17 +3,20 @@ module OptionsHelper
3
3
  # Validates a list of options against a list of required options
4
4
  # @param [Array] options list of options to validate
5
5
  # @param [Array] required list of required options
6
+ # @param [Array] valid list of additional valid options
6
7
  # @return true
7
8
  # @raise [ArgumentError] if the options do not pass validation
8
9
  # @api private
9
- def validate_options(options, required)
10
+ def validate_options(options, required = [], valid = [])
11
+ options = {} if options.nil?
12
+ required = {} if required.nil?
10
13
  # make sure all the required options exist
11
14
  required.each do |k|
12
15
  raise ArgumentError.new("Missing required parameter: #{k}") unless options.has_key?(k)
13
16
  end
14
17
  # make sure no invalid options exist
15
18
  options.each do |k, v|
16
- raise ArgumentError.new("Invalid parameter: #{k}") unless required.include?(k)
19
+ raise ArgumentError.new("Invalid parameter: #{k}") unless required.include?(k) || valid.include?(k)
17
20
  end
18
21
  true
19
22
  end
@@ -0,0 +1,54 @@
1
+ require 'date'
2
+
3
+ module Trackerific
4
+
5
+ # Provides a mock service for using in test and development
6
+ class MockService < Trackerific::Service
7
+
8
+ class << self
9
+ # Regular expression matchers for mocked Trackerific service
10
+ # @return [Array, Regexp] the regular expression
11
+ # @api private
12
+ def package_id_matchers
13
+ if defined?(Rails)
14
+ return [ /XXXXXXXXXX/, /XXXxxxxxxx/ ] unless Rails.env.production?
15
+ else
16
+ return [ ] # no matchers in production mode
17
+ end
18
+ end
19
+
20
+ # Returns an Array of required options used when creating a new instance
21
+ # @return [Array] required options
22
+ # @api private
23
+ def required_options
24
+ [ ]
25
+ end
26
+ end
27
+
28
+ # Sets up a mocked package details
29
+ # @param [String] package_id the package identifier
30
+ # @return [Trackerific::Details] the tracking details
31
+ # @raise [Trackerific::Error] raised when the server returns an error
32
+ # @example Track a package
33
+ # service = Trackerific::MockedService.new
34
+ # details = service.track_package("XXXXXXXXXX") # => valid response
35
+ # details = service.track_package("XXXxxxxxxx") # => throws a Trackerific::Error exception
36
+ # @api public
37
+ def track_package(package_id)
38
+ super
39
+ if package_id == "XXXXXXXXXX"
40
+ Trackerific::Details.new(
41
+ package_id,
42
+ "Your package was delivered.",
43
+ [
44
+ Trackerific::Event.new(Date.today, "Package delivered.", "SANTA MARIA, CA"),
45
+ Trackerific::Event.new(Date.today - 1, "Package scanned.", "SANTA BARBARA, CA"),
46
+ Trackerific::Event.new(Date.today - 2, "Package picked up for delivery.", "LOS ANGELES, CA")
47
+ ]
48
+ )
49
+ else
50
+ raise Trackerific::Error, "Package not found."
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,82 @@
1
+ require 'spec_helper'
2
+
3
+ describe OptionsHelper do
4
+ include OptionsHelper
5
+
6
+ describe :validate_options do
7
+
8
+ context "with all required options" do
9
+ before do
10
+ @required = [:hello, :world]
11
+ @options = {:hello => true, :world => true}
12
+ end
13
+ subject { validate_options(@options, @required) }
14
+ it { should be true }
15
+ end
16
+
17
+ context "with missing required options" do
18
+ before do
19
+ @required = [:hello, :world]
20
+ @options = {:hello => true}
21
+ end
22
+ specify { lambda { validate_options(@options, @required) }.should raise_error(ArgumentError) }
23
+ end
24
+
25
+ context "with no required options" do
26
+ before do
27
+ @required = []
28
+ @options = {}
29
+ end
30
+ subject { validate_options(@options, @required) }
31
+ it { should be true }
32
+ end
33
+
34
+ context "when required is nil" do
35
+ before do
36
+ @required = nil
37
+ @options = {}
38
+ end
39
+ subject { validate_options(@options, @required) }
40
+ it { should be true }
41
+ end
42
+
43
+ context "when options is nil" do
44
+ before do
45
+ @required = []
46
+ @options = nil
47
+ end
48
+ subject { validate_options(@options, @required) }
49
+ it { should be true }
50
+ end
51
+
52
+ context "when required and options are nil" do
53
+ before do
54
+ @required = nil
55
+ @options = nil
56
+ end
57
+ subject { validate_options(@options, @required) }
58
+ it { should be true }
59
+ end
60
+
61
+ context "with valid options" do
62
+ before do
63
+ @required = nil
64
+ @options = {:valid => true}
65
+ @valid = [:valid]
66
+ end
67
+ subject { validate_options(@options, @required, @valid) }
68
+ it { should be true }
69
+ end
70
+
71
+ context "with invalid options" do
72
+ before do
73
+ @required = nil
74
+ @options = {:invalid => true}
75
+ @valid = [:valid]
76
+ end
77
+ subject { validate_options(@options, @required, @valid) }
78
+ specify { lambda { validate_options(@options, @required) }.should raise_error(ArgumentError) }
79
+ end
80
+ end
81
+
82
+ end
@@ -10,6 +10,14 @@ end
10
10
 
11
11
  describe Trackerific::Service do
12
12
 
13
+ describe :required_options do
14
+ specify { Trackerific::Service.required_options.should be_kind_of Array }
15
+ end
16
+
17
+ describe :service_name do
18
+ specify { Trackerific::Service.service_name.should be_kind_of String }
19
+ end
20
+
13
21
  context "with a new Trackerific::Service class that has required options" do
14
22
 
15
23
  context "has all the required options" do
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe Trackerific::MockService do
4
+ describe :required_options do
5
+ subject { Trackerific::MockService.required_options }
6
+ it { should be_empty }
7
+ end
8
+
9
+ describe :package_id_matchers do
10
+ subject { Trackerific::MockService.package_id_matchers }
11
+ it("should be an Array of Regexp") { should each { |m| m.should be_a Regexp } }
12
+ end
13
+
14
+ describe :track_package do
15
+ before(:all) do
16
+ @service = Trackerific::MockService.new
17
+ end
18
+
19
+ context "with valid package_id" do
20
+ before { @tracking = @service.track_package("XXXXXXXXXX") }
21
+ subject { @tracking }
22
+ it("should return a Trackerific::Details") { should be_a Trackerific::Details }
23
+
24
+ describe "events.length" do
25
+ subject { @tracking.events.length }
26
+ it { should >= 1}
27
+ end
28
+
29
+ describe :summary do
30
+ subject { @tracking.summary }
31
+ it { should_not be_empty }
32
+ end
33
+ end
34
+
35
+ context "with invalid package_id" do
36
+ specify { lambda { @service.track_package("XXXxxxxxxx") }.should raise_error(Trackerific::Error) }
37
+ end
38
+
39
+ end
40
+
41
+ end
@@ -46,4 +46,15 @@ describe Trackerific do
46
46
 
47
47
  end
48
48
 
49
+ describe :track_package do
50
+
51
+ before do
52
+ @details = track_package "XXXXXXXXXX"
53
+ end
54
+
55
+ subject { @details }
56
+
57
+ it { should be_kind_of Trackerific::Details }
58
+ end
59
+
49
60
  end
data/trackerific.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{trackerific}
8
- s.version = "0.4.2"
8
+ s.version = "0.5.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Travis Haynes"]
12
- s.date = %q{2011-06-16}
12
+ s.date = %q{2011-06-23}
13
13
  s.description = %q{Trackerific provides USPS, FedEx and UPS package tracking to Rails.}
14
14
  s.email = %q{travis.j.haynes@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -56,6 +56,7 @@ Gem::Specification.new do |s|
56
56
  "lib/trackerific/event.rb",
57
57
  "lib/trackerific/service.rb",
58
58
  "lib/trackerific/services/fedex.rb",
59
+ "lib/trackerific/services/mock_service.rb",
59
60
  "lib/trackerific/services/ups.rb",
60
61
  "lib/trackerific/services/usps.rb",
61
62
  "spec/fixtures/fedex_error_response.xml",
@@ -64,12 +65,14 @@ Gem::Specification.new do |s|
64
65
  "spec/fixtures/ups_success_response.xml",
65
66
  "spec/fixtures/usps_error_response.xml",
66
67
  "spec/fixtures/usps_success_response.xml",
68
+ "spec/lib/helpers/options_helper_spec.rb",
67
69
  "spec/lib/trackerific/configuration_spec.rb",
68
70
  "spec/lib/trackerific/details_spec.rb",
69
71
  "spec/lib/trackerific/error_spec.rb",
70
72
  "spec/lib/trackerific/event_spec.rb",
71
73
  "spec/lib/trackerific/service_spec.rb",
72
74
  "spec/lib/trackerific/services/fedex_spec.rb",
75
+ "spec/lib/trackerific/services/mock_service_spec.rb",
73
76
  "spec/lib/trackerific/services/ups_spec.rb",
74
77
  "spec/lib/trackerific/services/usps_spec.rb",
75
78
  "spec/lib/trackerific_spec.rb",
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 4
8
- - 2
9
- version: 0.4.2
7
+ - 5
8
+ - 0
9
+ version: 0.5.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Travis Haynes
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-06-16 00:00:00 -07:00
17
+ date: 2011-06-23 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -201,6 +201,7 @@ files:
201
201
  - lib/trackerific/event.rb
202
202
  - lib/trackerific/service.rb
203
203
  - lib/trackerific/services/fedex.rb
204
+ - lib/trackerific/services/mock_service.rb
204
205
  - lib/trackerific/services/ups.rb
205
206
  - lib/trackerific/services/usps.rb
206
207
  - spec/fixtures/fedex_error_response.xml
@@ -209,12 +210,14 @@ files:
209
210
  - spec/fixtures/ups_success_response.xml
210
211
  - spec/fixtures/usps_error_response.xml
211
212
  - spec/fixtures/usps_success_response.xml
213
+ - spec/lib/helpers/options_helper_spec.rb
212
214
  - spec/lib/trackerific/configuration_spec.rb
213
215
  - spec/lib/trackerific/details_spec.rb
214
216
  - spec/lib/trackerific/error_spec.rb
215
217
  - spec/lib/trackerific/event_spec.rb
216
218
  - spec/lib/trackerific/service_spec.rb
217
219
  - spec/lib/trackerific/services/fedex_spec.rb
220
+ - spec/lib/trackerific/services/mock_service_spec.rb
218
221
  - spec/lib/trackerific/services/ups_spec.rb
219
222
  - spec/lib/trackerific/services/usps_spec.rb
220
223
  - spec/lib/trackerific_spec.rb
@@ -236,7 +239,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
236
239
  requirements:
237
240
  - - ">="
238
241
  - !ruby/object:Gem::Version
239
- hash: -748317933
242
+ hash: 3271480646841924991
240
243
  segments:
241
244
  - 0
242
245
  version: "0"