active_shipping 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. data/.gitignore +6 -0
  2. data/CHANGELOG +23 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.markdown +173 -0
  5. data/Rakefile +55 -0
  6. data/VERSION +1 -0
  7. data/init.rb +1 -0
  8. data/lib/active_shipping.rb +50 -0
  9. data/lib/active_shipping/lib/connection.rb +170 -0
  10. data/lib/active_shipping/lib/country.rb +319 -0
  11. data/lib/active_shipping/lib/error.rb +4 -0
  12. data/lib/active_shipping/lib/post_data.rb +22 -0
  13. data/lib/active_shipping/lib/posts_data.rb +47 -0
  14. data/lib/active_shipping/lib/requires_parameters.rb +16 -0
  15. data/lib/active_shipping/lib/utils.rb +18 -0
  16. data/lib/active_shipping/lib/validateable.rb +76 -0
  17. data/lib/active_shipping/shipping/base.rb +15 -0
  18. data/lib/active_shipping/shipping/carrier.rb +75 -0
  19. data/lib/active_shipping/shipping/carriers.rb +17 -0
  20. data/lib/active_shipping/shipping/carriers/bogus_carrier.rb +16 -0
  21. data/lib/active_shipping/shipping/carriers/fedex.rb +315 -0
  22. data/lib/active_shipping/shipping/carriers/shipwire.rb +167 -0
  23. data/lib/active_shipping/shipping/carriers/ups.rb +368 -0
  24. data/lib/active_shipping/shipping/carriers/usps.rb +496 -0
  25. data/lib/active_shipping/shipping/location.rb +100 -0
  26. data/lib/active_shipping/shipping/package.rb +144 -0
  27. data/lib/active_shipping/shipping/rate_estimate.rb +54 -0
  28. data/lib/active_shipping/shipping/rate_response.rb +19 -0
  29. data/lib/active_shipping/shipping/response.rb +49 -0
  30. data/lib/active_shipping/shipping/shipment_event.rb +14 -0
  31. data/lib/active_shipping/shipping/tracking_response.rb +22 -0
  32. data/lib/certs/cacert.pem +7815 -0
  33. data/lib/vendor/quantified/MIT-LICENSE +22 -0
  34. data/lib/vendor/quantified/README.markdown +49 -0
  35. data/lib/vendor/quantified/Rakefile +21 -0
  36. data/lib/vendor/quantified/init.rb +0 -0
  37. data/lib/vendor/quantified/lib/quantified.rb +6 -0
  38. data/lib/vendor/quantified/lib/quantified/attribute.rb +208 -0
  39. data/lib/vendor/quantified/lib/quantified/length.rb +20 -0
  40. data/lib/vendor/quantified/lib/quantified/mass.rb +19 -0
  41. data/lib/vendor/quantified/test/length_test.rb +92 -0
  42. data/lib/vendor/quantified/test/mass_test.rb +88 -0
  43. data/lib/vendor/quantified/test/test_helper.rb +2 -0
  44. data/lib/vendor/test_helper.rb +13 -0
  45. data/lib/vendor/xml_node/README +36 -0
  46. data/lib/vendor/xml_node/Rakefile +21 -0
  47. data/lib/vendor/xml_node/benchmark/bench_generation.rb +32 -0
  48. data/lib/vendor/xml_node/init.rb +1 -0
  49. data/lib/vendor/xml_node/lib/xml_node.rb +222 -0
  50. data/lib/vendor/xml_node/test/test_generating.rb +94 -0
  51. data/lib/vendor/xml_node/test/test_parsing.rb +43 -0
  52. data/test/fixtures.yml +13 -0
  53. data/test/fixtures/xml/fedex/ottawa_to_beverly_hills_rate_request.xml +67 -0
  54. data/test/fixtures/xml/fedex/ottawa_to_beverly_hills_rate_response.xml +213 -0
  55. data/test/fixtures/xml/fedex/tracking_request.xml +27 -0
  56. data/test/fixtures/xml/fedex/tracking_response.xml +153 -0
  57. data/test/fixtures/xml/shipwire/international_rates_response.xml +17 -0
  58. data/test/fixtures/xml/shipwire/invalid_credentials_response.xml +4 -0
  59. data/test/fixtures/xml/shipwire/new_carrier_rate_response.xml +18 -0
  60. data/test/fixtures/xml/shipwire/no_rates_response.xml +7 -0
  61. data/test/fixtures/xml/shipwire/rates_response.xml +36 -0
  62. data/test/fixtures/xml/ups/example_tracking_response.xml +53 -0
  63. data/test/fixtures/xml/ups/shipment_from_tiger_direct.xml +222 -0
  64. data/test/fixtures/xml/ups/test_real_home_as_residential_destination_response.xml +1 -0
  65. data/test/fixtures/xml/usps/beverly_hills_to_ottawa_book_rate_response.xml +85 -0
  66. data/test/fixtures/xml/usps/beverly_hills_to_ottawa_book_wii_rate_response.xml +168 -0
  67. data/test/fixtures/xml/usps/beverly_hills_to_ottawa_wii_rate_response.xml +85 -0
  68. data/test/remote/fedex_test.rb +140 -0
  69. data/test/remote/shipwire_test.rb +88 -0
  70. data/test/remote/ups_test.rb +187 -0
  71. data/test/remote/usps_test.rb +184 -0
  72. data/test/test_helper.rb +167 -0
  73. data/test/unit/base_test.rb +18 -0
  74. data/test/unit/carriers/fedex_test.rb +78 -0
  75. data/test/unit/carriers/shipwire_test.rb +130 -0
  76. data/test/unit/carriers/ups_test.rb +81 -0
  77. data/test/unit/carriers/usps_test.rb +206 -0
  78. data/test/unit/location_test.rb +46 -0
  79. data/test/unit/package_test.rb +65 -0
  80. data/test/unit/response_test.rb +10 -0
  81. metadata +158 -0
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2009 James MacAulay
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,49 @@
1
+ Quantified
2
+ ==========
3
+
4
+ Pretty quantifiable measurements which feel like ActiveSupport::Duration.
5
+
6
+ Access whichever included attributes you want like so:
7
+
8
+ require 'quantified/mass'
9
+ require 'quantified/length'
10
+
11
+ Add methods to Numeric (only plural, must correspond with plural unit names):
12
+
13
+ Mass.numeric_methods :grams, :kilograms, :ounces, :pounds
14
+ Length.numeric_methods :metres, :centimetres, :inches, :feet
15
+
16
+ Then you can do things like this:
17
+
18
+ 1.feet == 12.inches
19
+ # => true
20
+
21
+ 18.inches.to_feet
22
+ # => #<Quantified::Length: 1.5 feet>
23
+
24
+ (2.5).feet.in_millimetres.to_s
25
+ # => "762.0 millimetres"
26
+
27
+
28
+ You can easily define new attributes. Here's length.rb:
29
+
30
+ module Quantified
31
+ class Length < Attribute
32
+ system :metric do
33
+ primitive :metre
34
+
35
+ one :centimetre, :is => Length.new(0.01, :metres)
36
+ one :millimetre, :is => Length.new(0.1, :centimetres)
37
+ one :kilometre, :is => Length.new(1000, :metres)
38
+ end
39
+
40
+ system :imperial do
41
+ primitive :inch
42
+ one :inch, :is => Length.new(2.540, :centimetres)
43
+
44
+ one :foot, :plural => :feet, :is => Length.new(12, :inches)
45
+ one :yard, :is => Length.new(3, :feet)
46
+ one :mile, :is => Length.new(5280, :feet)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,21 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for the calculations plugin.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'Quantified'
19
+ rdoc.options << '--line-numbers --inline-source'
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
File without changes
@@ -0,0 +1,6 @@
1
+ $: << File.dirname(__FILE__)
2
+ require 'rubygems'
3
+ require 'activesupport'
4
+ require 'bigdecimal'
5
+
6
+ require 'quantified/attribute'
@@ -0,0 +1,208 @@
1
+ module Quantified
2
+ class Attribute
3
+ include Comparable
4
+
5
+ attr_reader :amount, :unit
6
+
7
+ def initialize(amount, unit)
8
+ raise ArgumentError, "amount must be a Numeric" unless amount.is_a?(Numeric)
9
+ @amount, @unit = amount, unit.to_sym
10
+ end
11
+
12
+ def to_s
13
+ "#{amount} #{unit}"
14
+ end
15
+
16
+ def inspect
17
+ "#<#{self.class.name}: #{amount} #{unit}>"
18
+ end
19
+
20
+ def ==(other)
21
+ (BigDecimal.new(self.amount.to_s) == BigDecimal.new(other.amount.to_s) && self.unit == other.unit) || BigDecimal.new(self.class.convert(self.amount, self.unit, other.unit).to_s) == BigDecimal.new(other.amount.to_s)
22
+ rescue NoMethodError
23
+ self.amount == other
24
+ end
25
+
26
+ def eql?(other)
27
+ self.class == other.class && BigDecimal.new(self.amount.to_s) == BigDecimal.new(other.amount.to_s) && self.unit == other.unit
28
+ end
29
+
30
+ def <=>(other)
31
+ if self.class == other.class
32
+ self.class.convert(self.amount, self.unit, other.unit) <=> other.amount
33
+ else
34
+ self.amount <=> other
35
+ end
36
+ end
37
+
38
+ def system
39
+ self.class.units_to_systems[unit]
40
+ end
41
+
42
+ def method_missing(meth, *args)
43
+ if args.size == 1 && self.class == (other = args.first).class
44
+ other_amount_in_self_units = self.class.convert(other.amount, other.unit, self.unit)
45
+ self.class.new(amount.send(meth, other_amount_in_self_units), self.unit)
46
+ else
47
+ amount.send(meth, *args)
48
+ end
49
+ end
50
+
51
+ def self.conversion_rate(from, to)
52
+ return nil unless self.conversions[from] and self.conversions[to]
53
+ return self.conversions[from][to] ||=
54
+ (1.0 / self.conversions[to][from] if self.conversions[to][from]) || begin
55
+ shared_conversions = self.conversions[from].keys & self.conversions[to].keys
56
+ if shared_conversions.any?
57
+ primitive = shared_conversions.first
58
+ self.conversions[from][primitive] * (1.0 / self.conversions[to][primitive])
59
+ else
60
+ self.conversions[from].each do |conversion_unit, multiple|
61
+ if self.conversions[to].include?(conversion_unit)
62
+ return multiple * conversion_rate(conversion) * (1.0 / self.conversions[to][conversion_unit])
63
+ end
64
+ end
65
+ from_primitive = (self.conversions[from].keys & self.primitives).first
66
+ to_primitive = (self.conversions[to].keys & self.primitives).first
67
+ if from_primitive_to_primitive_multiple = conversion_rate(from_primitive, to_primitive)
68
+ return self.conversions[from][from_primitive] * from_primitive_to_primitive_multiple * (1.0 / self.conversions[to][to_primitive])
69
+ end
70
+ raise StandardError, "No conversion path from #{from} to #{to}"
71
+ end
72
+ end
73
+ end
74
+
75
+ def self.units(system=nil)
76
+ if system
77
+ self.systems_to_units[system.to_sym].dup
78
+ else
79
+ read_inheritable_attribute(:primitives) | self.conversions.keys
80
+ end
81
+ end
82
+
83
+ def self.primitives
84
+ read_inheritable_attribute(:primitives).dup
85
+ end
86
+
87
+ def self.non_primitives
88
+ self.conversions.keys
89
+ end
90
+
91
+ def self.systems
92
+ self.systems_to_units.keys
93
+ end
94
+
95
+ def self.add_numeric_methods?
96
+ self.add_numeric_methods
97
+ end
98
+
99
+ def self.numeric_methods(*args)
100
+ args.each do |arg|
101
+ add_numeric_method_for(arg.to_sym)
102
+ end
103
+ end
104
+
105
+ protected
106
+
107
+ class_inheritable_accessor :add_numeric_methods, :primitives, :conversions, :current_system, :systems_to_units, :units_to_systems
108
+ self.add_numeric_methods = false
109
+ self.primitives = []
110
+ self.conversions = {}
111
+ self.current_system = nil
112
+ self.systems_to_units = {}
113
+ self.units_to_systems = {}
114
+
115
+ def self.system(system_name, &block)
116
+ old_system = self.current_system
117
+ self.current_system = system_name.to_sym
118
+ yield
119
+ self.current_system = old_system
120
+ end
121
+
122
+ def self.primitive(sym, options={})
123
+ unit_sym = (options[:plural] || sym.to_s.pluralize).to_sym
124
+ self.primitives << unit_sym
125
+ add_to_system(unit_sym)
126
+ add_methods_for(unit_sym, options)
127
+ end
128
+
129
+ def self.add_to_system(unit_sym)
130
+ if self.current_system
131
+ self.units_to_systems[unit_sym] ||= begin
132
+ sys_ary = self.systems_to_units[self.current_system] ||= []
133
+ sys_ary << unit_sym
134
+ self.current_system
135
+ end
136
+ end
137
+ end
138
+
139
+ def self.one(sym, options={})
140
+ unit_sym = (options[:plural] || sym.to_s.pluralize).to_sym
141
+ add_to_system(unit_sym)
142
+ register_unit(unit_sym, options[:is].unit, options[:is].amount)
143
+ add_methods_for(unit_sym, options)
144
+ end
145
+
146
+ def self.register_unit(multiple_unit, other_unit, multiple)
147
+ multiple_unit, other_unit = multiple_unit.to_sym, other_unit.to_sym
148
+ self.conversions[multiple_unit] ||= {}
149
+ self.conversions[other_unit] ||= {}
150
+
151
+ if self.primitives.include?(multiple_unit) || self.primitives.include?(other_unit)
152
+ add_conversion(multiple_unit, other_unit, multiple)
153
+ else
154
+ [multiple_unit, other_unit].each do |this_unit|
155
+ self.conversions[this_unit].each do |this_other_unit, this_multiple|
156
+ if self.primitives.include?(this_other_unit)
157
+ add_conversion(multiple_unit, this_other_unit, multiple * this_multiple)
158
+ end
159
+ end
160
+ end
161
+ end
162
+ end
163
+
164
+ def self.add_conversion(multiple_unit, other_unit, multiple)
165
+ self.conversions[multiple_unit] ||={}
166
+ self.conversions[multiple_unit][other_unit] = multiple
167
+ self.conversions[other_unit] ||= {}
168
+ self.conversions[other_unit][multiple_unit] = (1.0 / multiple)
169
+ end
170
+
171
+ def self.convert(amount, from, to)
172
+ from, to = from.to_sym, to.to_sym
173
+ amount * conversion_rate(from, to)
174
+ end
175
+
176
+ def self.add_methods_for(sym, options={})
177
+ add_conversion_method_for(sym, options)
178
+ add_numeric_method = if options.has_key?(:add_numeric_methods)
179
+ options[:add_numeric_methods]
180
+ else
181
+ self.add_numeric_methods
182
+ end
183
+ add_numeric_method_for(sym.to_s, options) if add_numeric_method
184
+ end
185
+
186
+ def self.add_conversion_method_for(sym, options={})
187
+ unit_name = sym.to_s
188
+ class_eval do
189
+ define_method("to_#{unit_name}") do
190
+ return self if unit_name == self.unit.to_s
191
+ self.class.new(self.class.convert(self.amount, self.unit, unit_name), unit_name)
192
+ end
193
+ alias_method("in_#{unit_name}","to_#{unit_name}")
194
+ end
195
+ end
196
+
197
+ def self.add_numeric_method_for(unit_name, options={})
198
+ unit_name = unit_name.to_sym
199
+ raise ArgumentError, "#{unit_name.inspect} is not a unit in #{self.name}" unless units.include?(unit_name)
200
+ klass = self
201
+ Numeric.class_eval do
202
+ define_method(unit_name) do
203
+ klass.new(self, unit_name.to_sym)
204
+ end
205
+ end
206
+ end
207
+ end
208
+ end
@@ -0,0 +1,20 @@
1
+ module Quantified
2
+ class Length < Attribute
3
+ system :metric do
4
+ primitive :metre
5
+
6
+ one :centimetre, :is => Length.new(0.01, :metres)
7
+ one :millimetre, :is => Length.new(0.1, :centimetres)
8
+ one :kilometre, :is => Length.new(1000, :metres)
9
+ end
10
+
11
+ system :imperial do
12
+ primitive :inch
13
+ one :inch, :is => Length.new(2.540, :centimetres)
14
+
15
+ one :foot, :plural => :feet, :is => Length.new(12, :inches)
16
+ one :yard, :is => Length.new(3, :feet)
17
+ one :mile, :is => Length.new(5280, :feet)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,19 @@
1
+ module Quantified
2
+ class Mass < Attribute
3
+ system :metric do
4
+ primitive :gram
5
+
6
+ one :milligram, :is => Mass.new(0.001, :grams)
7
+ one :kilogram, :is => Mass.new(1000, :grams)
8
+ end
9
+
10
+ system :imperial do
11
+ primitive :ounce
12
+ one :ounce, :is => Mass.new(28.349523125, :grams)
13
+
14
+ one :pound, :is => Mass.new(16, :ounces)
15
+ one :stone, :is => Mass.new(14, :pounds)
16
+ one :short_ton, :is => Mass.new(2000, :pounds)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,92 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+ require 'quantified/length'
3
+
4
+ class LengthTest < Test::Unit::TestCase
5
+ include Quantified
6
+ Length.numeric_methods :metres, :centimetres, :inches, :feet
7
+
8
+ def setup
9
+ @length = Length.new(5, :feet)
10
+ end
11
+
12
+ def test_inspect
13
+ assert_equal "#<Quantified::Length: 5 feet>", @length.inspect
14
+ end
15
+
16
+ def test_to_s
17
+ assert_equal "5 feet", @length.to_s
18
+ end
19
+
20
+ def test_initialize_from_numeric
21
+ assert_equal "5 feet", 5.feet.to_s
22
+ end
23
+
24
+ def test_equalities
25
+ assert_equal 1.feet, (1.0).feet
26
+ # == based on value
27
+ assert_equal 6.feet, Length.new(2, :yards)
28
+ # eql? based on value and unit
29
+ assert !6.feet.eql?(Length.new(2, :yards))
30
+ # equal? based on object identity
31
+ assert !2.feet.equal?(2.feet)
32
+ end
33
+
34
+ def test_convert_yards_to_feet
35
+ assert 6.feet.eql?(Length.new(2, :yards).to_feet)
36
+ end
37
+
38
+ def test_convert_feet_to_yards
39
+ assert Length.new(2, :yards).eql?(6.feet.to_yards)
40
+ end
41
+
42
+ def test_convert_yards_to_millimetres
43
+ assert Length.new(914.4, :millimetres).eql?(Length.new(1, :yards).to_millimetres)
44
+ end
45
+
46
+ def test_convert_millimetres_to_yards
47
+ assert Length.new(1, :yards).eql?(Length.new(914.4, :millimetres).to_yards)
48
+ end
49
+
50
+ def test_convert_metres_to_inches
51
+ assert_equal 1.inches, (0.0254).metres.to_inches
52
+ assert 1.inches.eql?((0.0254).metres.to_inches)
53
+ assert 1.inches.eql?((0.0254).metres.in_inches)
54
+ end
55
+
56
+ def test_comparison_with_numeric
57
+ assert 2.feet > 1
58
+ assert 2.feet == 2
59
+ assert 2.feet <= 2
60
+ assert 2.feet < 3
61
+ end
62
+
63
+ def test_method_missing_to_i
64
+ assert_equal 2, (2.4).feet.to_i
65
+ end
66
+
67
+ def test_method_missing_to_f
68
+ assert_equal 2.4, (2.4).feet.to_f
69
+ end
70
+
71
+ def test_method_missing_minus
72
+ assert_equal 2.feet, 5.feet - 3.feet
73
+ end
74
+
75
+ def test_numeric_methods_not_added_for_some_units
76
+ assert_raises NoMethodError do
77
+ 2.yards
78
+ end
79
+ assert_raises NoMethodError do
80
+ 2.millimetres
81
+ end
82
+ end
83
+
84
+ def test_systems
85
+ assert_equal [:metric, :imperial], Length.systems
86
+ assert_equal [:metres, :centimetres, :millimetres, :kilometres], Length.units(:metric)
87
+ assert_equal [:inches, :feet, :yards, :miles], Length.units(:imperial)
88
+
89
+ assert_equal :metric, 2.centimetres.system
90
+ assert_equal :imperial, 2.feet.system
91
+ end
92
+ end