active_shipping 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/CHANGELOG +23 -0
- data/MIT-LICENSE +20 -0
- data/README.markdown +173 -0
- data/Rakefile +55 -0
- data/VERSION +1 -0
- data/init.rb +1 -0
- data/lib/active_shipping.rb +50 -0
- data/lib/active_shipping/lib/connection.rb +170 -0
- data/lib/active_shipping/lib/country.rb +319 -0
- data/lib/active_shipping/lib/error.rb +4 -0
- data/lib/active_shipping/lib/post_data.rb +22 -0
- data/lib/active_shipping/lib/posts_data.rb +47 -0
- data/lib/active_shipping/lib/requires_parameters.rb +16 -0
- data/lib/active_shipping/lib/utils.rb +18 -0
- data/lib/active_shipping/lib/validateable.rb +76 -0
- data/lib/active_shipping/shipping/base.rb +15 -0
- data/lib/active_shipping/shipping/carrier.rb +75 -0
- data/lib/active_shipping/shipping/carriers.rb +17 -0
- data/lib/active_shipping/shipping/carriers/bogus_carrier.rb +16 -0
- data/lib/active_shipping/shipping/carriers/fedex.rb +315 -0
- data/lib/active_shipping/shipping/carriers/shipwire.rb +167 -0
- data/lib/active_shipping/shipping/carriers/ups.rb +368 -0
- data/lib/active_shipping/shipping/carriers/usps.rb +496 -0
- data/lib/active_shipping/shipping/location.rb +100 -0
- data/lib/active_shipping/shipping/package.rb +144 -0
- data/lib/active_shipping/shipping/rate_estimate.rb +54 -0
- data/lib/active_shipping/shipping/rate_response.rb +19 -0
- data/lib/active_shipping/shipping/response.rb +49 -0
- data/lib/active_shipping/shipping/shipment_event.rb +14 -0
- data/lib/active_shipping/shipping/tracking_response.rb +22 -0
- data/lib/certs/cacert.pem +7815 -0
- data/lib/vendor/quantified/MIT-LICENSE +22 -0
- data/lib/vendor/quantified/README.markdown +49 -0
- data/lib/vendor/quantified/Rakefile +21 -0
- data/lib/vendor/quantified/init.rb +0 -0
- data/lib/vendor/quantified/lib/quantified.rb +6 -0
- data/lib/vendor/quantified/lib/quantified/attribute.rb +208 -0
- data/lib/vendor/quantified/lib/quantified/length.rb +20 -0
- data/lib/vendor/quantified/lib/quantified/mass.rb +19 -0
- data/lib/vendor/quantified/test/length_test.rb +92 -0
- data/lib/vendor/quantified/test/mass_test.rb +88 -0
- data/lib/vendor/quantified/test/test_helper.rb +2 -0
- data/lib/vendor/test_helper.rb +13 -0
- data/lib/vendor/xml_node/README +36 -0
- data/lib/vendor/xml_node/Rakefile +21 -0
- data/lib/vendor/xml_node/benchmark/bench_generation.rb +32 -0
- data/lib/vendor/xml_node/init.rb +1 -0
- data/lib/vendor/xml_node/lib/xml_node.rb +222 -0
- data/lib/vendor/xml_node/test/test_generating.rb +94 -0
- data/lib/vendor/xml_node/test/test_parsing.rb +43 -0
- data/test/fixtures.yml +13 -0
- data/test/fixtures/xml/fedex/ottawa_to_beverly_hills_rate_request.xml +67 -0
- data/test/fixtures/xml/fedex/ottawa_to_beverly_hills_rate_response.xml +213 -0
- data/test/fixtures/xml/fedex/tracking_request.xml +27 -0
- data/test/fixtures/xml/fedex/tracking_response.xml +153 -0
- data/test/fixtures/xml/shipwire/international_rates_response.xml +17 -0
- data/test/fixtures/xml/shipwire/invalid_credentials_response.xml +4 -0
- data/test/fixtures/xml/shipwire/new_carrier_rate_response.xml +18 -0
- data/test/fixtures/xml/shipwire/no_rates_response.xml +7 -0
- data/test/fixtures/xml/shipwire/rates_response.xml +36 -0
- data/test/fixtures/xml/ups/example_tracking_response.xml +53 -0
- data/test/fixtures/xml/ups/shipment_from_tiger_direct.xml +222 -0
- data/test/fixtures/xml/ups/test_real_home_as_residential_destination_response.xml +1 -0
- data/test/fixtures/xml/usps/beverly_hills_to_ottawa_book_rate_response.xml +85 -0
- data/test/fixtures/xml/usps/beverly_hills_to_ottawa_book_wii_rate_response.xml +168 -0
- data/test/fixtures/xml/usps/beverly_hills_to_ottawa_wii_rate_response.xml +85 -0
- data/test/remote/fedex_test.rb +140 -0
- data/test/remote/shipwire_test.rb +88 -0
- data/test/remote/ups_test.rb +187 -0
- data/test/remote/usps_test.rb +184 -0
- data/test/test_helper.rb +167 -0
- data/test/unit/base_test.rb +18 -0
- data/test/unit/carriers/fedex_test.rb +78 -0
- data/test/unit/carriers/shipwire_test.rb +130 -0
- data/test/unit/carriers/ups_test.rb +81 -0
- data/test/unit/carriers/usps_test.rb +206 -0
- data/test/unit/location_test.rb +46 -0
- data/test/unit/package_test.rb +65 -0
- data/test/unit/response_test.rb +10 -0
- metadata +158 -0
data/.gitignore
ADDED
data/CHANGELOG
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
* Remove ftools for Rails 1.9 compatibility and remove xml logging, as logging is now included in the connection [cody]
|
2
|
+
* Update connection code from ActiveMerchant [cody]
|
3
|
+
* Fix space-ridden USPS usernames when validating credentials [james]
|
4
|
+
* Remove extra slash from USPS URLs [james]
|
5
|
+
* Update Shipwire endpoint hostname [cody]
|
6
|
+
* Add missing ISO countries [Edward Ocampo-Gooding]
|
7
|
+
* Add support for Guernsey to country.rb [cody]
|
8
|
+
* Use :words_connector instead of connector in RequiresParameters [cody]
|
9
|
+
* Fix extra slash in UPS endpoints [cody]
|
10
|
+
* Add name to Shipwire class [cody]
|
11
|
+
* Improve FedEx handling of some error conditions [cody]
|
12
|
+
* Add support for validating credentials to Shipwire [cody]
|
13
|
+
* Add support for ssl_get to PostsData. Update Carriers to use PostsData module. Turn on retry safety for carriers [cody]
|
14
|
+
* Add support for Shipwire Shipping Rate API [cody]
|
15
|
+
* Cleanup package tests [cody]
|
16
|
+
* Remove unused Carrier#setup method [cody]
|
17
|
+
* Don't use Array splat in Regex comparisons in Package [cody]
|
18
|
+
* Default the Location to use the :alpha2 country code format [cody]
|
19
|
+
* Add configurable timeouts from Active Merchant [cody]
|
20
|
+
* Update xml_node.rb from XML Node [cody]
|
21
|
+
* Update requires_parameters from ActiveMerchant [cody]
|
22
|
+
* Sync posts_data.rb with ActiveMerchant [cody]
|
23
|
+
* Don't use credentials fixtures in local tests [cody]
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 James MacAulay
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOa AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SaALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,173 @@
|
|
1
|
+
# Active Shipping
|
2
|
+
|
3
|
+
This library is meant to interface with the web services of various shipping carriers. The goal is to abstract the features that are most frequently used into a pleasant and consistent Ruby API. Active Shipping is an extension of [Active Merchant][], and as such, it borrows heavily from conventions used in the latter.
|
4
|
+
|
5
|
+
We are starting out by only implementing the ability to list available shipping rates for a particular origin, destination, and set of packages. Further development could take advantage of other common features of carriers' web services such as tracking orders and printing labels.
|
6
|
+
|
7
|
+
Active Shipping is currently being used and improved in a production environment for the e-commerce application [Shopify][]. Development is being done by [James MacAulay][] (<james@jadedpixel.com>). Discussion is welcome in the [Active Merchant Google Group][discuss].
|
8
|
+
|
9
|
+
[Active Merchant]:http://www.activemerchant.org
|
10
|
+
[Shopify]:http://www.shopify.com
|
11
|
+
[James MacAulay]:http://jmacaulay.net
|
12
|
+
[discuss]:http://groups.google.com/group/activemerchant
|
13
|
+
|
14
|
+
## Supported Shipping Carriers
|
15
|
+
|
16
|
+
* [UPS](http://www.ups.com)
|
17
|
+
* [USPS](http://www.usps.com)
|
18
|
+
* [FedEx](http://www.fedex.com)
|
19
|
+
* more soon!
|
20
|
+
|
21
|
+
## Prerequisites
|
22
|
+
|
23
|
+
* [active_support](http://github.com/rails/rails/tree/master/activesupport)
|
24
|
+
* [xml_node](http://github.com/tobi/xml_node/) (right now a version of it is actually included in this library, so you don't need to worry about it yet)
|
25
|
+
* [mocha](http://mocha.rubyforge.org/) for the tests
|
26
|
+
|
27
|
+
## Download & Installation
|
28
|
+
|
29
|
+
Currently this library is available on GitHub:
|
30
|
+
|
31
|
+
<http://github.com/Shopify/active_shipping>
|
32
|
+
|
33
|
+
You will need to get [Git][] if you don't have it. Then:
|
34
|
+
|
35
|
+
> git clone git://github.com/Shopify/active_shipping.git
|
36
|
+
|
37
|
+
(That URL is case-sensitive, so watch out.)
|
38
|
+
|
39
|
+
Active Shipping includes an init.rb file. This means that Rails will automatically load it on startup. Check out [git-archive][] for exporting the file tree from your repository to your vendor directory.
|
40
|
+
|
41
|
+
Gem and tarball forthcoming on rubyforge.
|
42
|
+
|
43
|
+
[Git]:http://git.or.cz/
|
44
|
+
[git-archive]:http://www.kernel.org/pub/software/scm/git/docs/git-archive.html
|
45
|
+
|
46
|
+
## Sample Usage
|
47
|
+
|
48
|
+
require 'active_shipping'
|
49
|
+
include ActiveMerchant::Shipping
|
50
|
+
|
51
|
+
# Package up a poster and a Wii for your nephew.
|
52
|
+
packages = [
|
53
|
+
Package.new( 100, # 100 grams
|
54
|
+
[93,10], # 93 cm long, 10 cm diameter
|
55
|
+
:cylinder => true), # cylinders have different volume calculations
|
56
|
+
|
57
|
+
Package.new( (7.5 * 16), # 7.5 lbs, times 16 oz/lb.
|
58
|
+
[15, 10, 4.5], # 15x10x4.5 inches
|
59
|
+
:units => :imperial) # not grams, not centimetres
|
60
|
+
]
|
61
|
+
|
62
|
+
# You live in Beverly Hills, he lives in Ottawa
|
63
|
+
origin = Location.new( :country => 'US',
|
64
|
+
:state => 'CA',
|
65
|
+
:city => 'Beverly Hills',
|
66
|
+
:zip => '90210')
|
67
|
+
|
68
|
+
destination = Location.new( :country => 'CA',
|
69
|
+
:province => 'ON',
|
70
|
+
:city => 'Ottawa',
|
71
|
+
:postal_code => 'K1P 1J1')
|
72
|
+
|
73
|
+
# Find out how much it'll be.
|
74
|
+
ups = UPS.new(:login => 'auntjudy', :password => 'secret', :key => 'xml-access-key')
|
75
|
+
response = ups.find_rates(origin, destination, packages)
|
76
|
+
|
77
|
+
ups_rates = response.rates.sort_by(&:price).collect {|rate| [rate.service_name, rate.price]}
|
78
|
+
# => [["UPS Standard", 3936],
|
79
|
+
# ["UPS Worldwide Expedited", 8682],
|
80
|
+
# ["UPS Saver", 9348],
|
81
|
+
# ["UPS Express", 9702],
|
82
|
+
# ["UPS Worldwide Express Plus", 14502]]
|
83
|
+
|
84
|
+
# Check out USPS for comparison...
|
85
|
+
usps = USPS.new(:login => 'developer-key')
|
86
|
+
response = usps.find_rates(origin, destination, packages)
|
87
|
+
|
88
|
+
usps_rates = response.rates.sort_by(&:price).collect {|rate| [rate.service_name, rate.price]}
|
89
|
+
# => [["USPS Priority Mail International", 4110],
|
90
|
+
# ["USPS Express Mail International (EMS)", 5750],
|
91
|
+
# ["USPS Global Express Guaranteed Non-Document Non-Rectangular", 9400],
|
92
|
+
# ["USPS GXG Envelopes", 9400],
|
93
|
+
# ["USPS Global Express Guaranteed Non-Document Rectangular", 9400],
|
94
|
+
# ["USPS Global Express Guaranteed", 9400]]
|
95
|
+
|
96
|
+
# FedEx
|
97
|
+
fdx = FedEx.new(:login => 'Your 9-digit FedEx Account #', :password => 'Your Meter Number')
|
98
|
+
response = fdx.find_rates(origin, destination, packages, :test => true)
|
99
|
+
response.rates.sort_by(&:price).collect {|rate| [rate.service_name, rate.price]}
|
100
|
+
# => [["FedEx Ground", 977],
|
101
|
+
# ["FedEx Ground Home Delivery", 1388],
|
102
|
+
# ["FedEx Express Saver", 2477],
|
103
|
+
# ["FedEx 2 Day", 2718],
|
104
|
+
# ["FedEx Standard Overnight", 4978],
|
105
|
+
# ["FedEx Priority Overnight", 8636],
|
106
|
+
# ["FedEx First Overnight", 12306]]
|
107
|
+
|
108
|
+
# FedEx Tracking
|
109
|
+
fdx = FedEx.new(:login => '999999999', :password => '7777777')
|
110
|
+
tracking_info = fdx.find_tracking_info('tracking number here', :carrier_code => 'fedex_ground') # Ground package
|
111
|
+
|
112
|
+
tracking_info.shipment_events.each do |event|
|
113
|
+
puts "#{event.name} at #{event.location.city}, #{event.location.state} on #{event.time}. #{event.message}"
|
114
|
+
end
|
115
|
+
# => Package information transmitted to FedEx at NASHVILLE LOCAL, TN on Thu Oct 23 00:00:00 UTC 2008.
|
116
|
+
# Picked up by FedEx at NASHVILLE LOCAL, TN on Thu Oct 23 17:30:00 UTC 2008.
|
117
|
+
# Scanned at FedEx sort facility at NASHVILLE, TN on Thu Oct 23 18:50:00 UTC 2008.
|
118
|
+
# Departed FedEx sort facility at NASHVILLE, TN on Thu Oct 23 22:33:00 UTC 2008.
|
119
|
+
# Arrived at FedEx sort facility at KNOXVILLE, TN on Fri Oct 24 02:45:00 UTC 2008.
|
120
|
+
# Scanned at FedEx sort facility at KNOXVILLE, TN on Fri Oct 24 05:56:00 UTC 2008.
|
121
|
+
# Delivered at Knoxville, TN on Fri Oct 24 16:45:00 UTC 2008. Signed for by: T.BAKER
|
122
|
+
|
123
|
+
tracking_info = fdx.find_tracking_info('tracking number here', :carrier_code => 'fedex_express') # Express package
|
124
|
+
|
125
|
+
tracking_info.shipment_events.each do |event|
|
126
|
+
puts "#{event.name} at #{event.location.city}, #{event.location.state} on #{event.time}. #{event.message}"
|
127
|
+
end
|
128
|
+
# => Picked up by FedEx at NASHVILLE, TN on Wed Dec 03 16:46:00 UTC 2008.
|
129
|
+
# Package status at MISSISSAUGA, ON on Wed Dec 03 18:00:00 UTC 2008.
|
130
|
+
# Left FedEx Origin Location at NASHVILLE, TN on Wed Dec 03 20:27:00 UTC 2008.
|
131
|
+
# Arrived at FedEx Ramp at NASHVILLE, TN on Wed Dec 03 20:43:00 UTC 2008.
|
132
|
+
# Left FedEx Ramp at NASHVILLE, TN on Wed Dec 03 22:30:00 UTC 2008.
|
133
|
+
# Arrived at Sort Facility at INDIANAPOLIS, IN on Thu Dec 04 00:31:00 UTC 2008.
|
134
|
+
# Left FedEx Sort Facility at INDIANAPOLIS, IN on Thu Dec 04 01:14:00 UTC 2008.
|
135
|
+
# Left FedEx Sort Facility at INDIANAPOLIS, IN on Thu Dec 04 04:48:00 UTC 2008.
|
136
|
+
# Arrived at FedEx Ramp at MISSISSAUGA, ON on Thu Dec 04 06:26:00 UTC 2008.
|
137
|
+
# Package status at MISSISSAUGA, ON on Thu Dec 04 07:03:00 UTC 2008.
|
138
|
+
# Left FedEx Ramp at MISSISSAUGA, ON on Thu Dec 04 07:37:00 UTC 2008.
|
139
|
+
# Arrived at FedEx Destination Location at TORONTO, ON on Thu Dec 04 08:42:00 UTC 2008.
|
140
|
+
# On FedEx vehicle for delivery at TORONTO, ON on Thu Dec 04 09:04:00 UTC 2008.
|
141
|
+
# Delivered to Non-FedEx clearance broker at TORONTO, ON on Thu Dec 04 10:15:00 UTC 2008.
|
142
|
+
|
143
|
+
## TODO
|
144
|
+
|
145
|
+
* proper documentation
|
146
|
+
* proper offline testing for carriers in addition to the remote tests
|
147
|
+
* package into a gem
|
148
|
+
* carrier code template generator
|
149
|
+
* more carriers
|
150
|
+
* integrate with ActiveMerchant
|
151
|
+
* support more features for existing carriers
|
152
|
+
* bin-packing algorithm (preferably implemented in ruby)
|
153
|
+
* order tracking
|
154
|
+
* label printing
|
155
|
+
|
156
|
+
## Contributing
|
157
|
+
|
158
|
+
Yes, please! Take a look at the tests and the implementation of the Carrier class to see how the basics work. At some point soon there will be a carrier template generator along the lines of the gateway generator included in Active Merchant, but carrier.rb outlines most of what's necessary. The other main classes that would be good to familiarize yourself with are Location, Package, and Response.
|
159
|
+
|
160
|
+
The nicest way to submit changes would be to set up a GitHub account and fork this project, then initiate a pull request when you want your changes looked at. You can also make a patch (preferably with [git-diff][]) and email to james@jadedpixel.com.
|
161
|
+
|
162
|
+
[git-diff]:http://www.kernel.org/pub/software/scm/git/docs/git-diff.html
|
163
|
+
|
164
|
+
## Contributors
|
165
|
+
|
166
|
+
* James MacAulay (<http://jmacaulay.net>)
|
167
|
+
* Tobias Luetke (<http://blog.leetsoft.com>)
|
168
|
+
* Cody Fauser (<http://codyfauser.com>)
|
169
|
+
* Jimmy Baker (<http://jimmyville.com/>)
|
170
|
+
|
171
|
+
## Legal Mumbo Jumbo
|
172
|
+
|
173
|
+
Unless otherwise noted in specific files, all code in the Active Shipping project is under the copyright and license described in the included MIT-LICENSE file.
|
data/Rakefile
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/testtask'
|
4
|
+
require 'rake/rdoctask'
|
5
|
+
require 'rake/gempackagetask'
|
6
|
+
require 'rake/contrib/rubyforgepublisher'
|
7
|
+
|
8
|
+
begin
|
9
|
+
require 'jeweler'
|
10
|
+
Jeweler::Tasks.new do |gem|
|
11
|
+
gem.name = "active_shipping"
|
12
|
+
gem.summary = %Q{Shipping API extension for Active Merchant.}
|
13
|
+
gem.description = %Q{Shipping API extension for Active Merchant.}
|
14
|
+
gem.email = "jmacaulay@gmail.com"
|
15
|
+
gem.homepage = "http://github.com/Shopify/active_shipping"
|
16
|
+
gem.authors = ["James MacAulay", "Tobias Luetke", "Cody Fauser", "Jimmy Baker"]
|
17
|
+
gem.add_dependency "activesupport"
|
18
|
+
end
|
19
|
+
rescue LoadError
|
20
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
desc "Default Task"
|
25
|
+
task :default => 'test:units'
|
26
|
+
task :test => ['test:units','test:remote']
|
27
|
+
|
28
|
+
# Run the unit tests
|
29
|
+
|
30
|
+
namespace :test do
|
31
|
+
Rake::TestTask.new(:units) do |t|
|
32
|
+
t.pattern = 'test/unit/**/*_test.rb'
|
33
|
+
t.ruby_opts << '-rubygems'
|
34
|
+
t.verbose = true
|
35
|
+
end
|
36
|
+
|
37
|
+
Rake::TestTask.new(:remote) do |t|
|
38
|
+
t.pattern = 'test/remote/*_test.rb'
|
39
|
+
t.ruby_opts << '-rubygems'
|
40
|
+
t.verbose = true
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Genereate the RDoc documentation
|
45
|
+
Rake::RDocTask.new do |rdoc|
|
46
|
+
rdoc.rdoc_dir = 'doc'
|
47
|
+
rdoc.title = "ActiveShipping library"
|
48
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
49
|
+
rdoc.rdoc_files.include('README', 'CHANGELOG')
|
50
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
51
|
+
end
|
52
|
+
|
53
|
+
task :install => [:package] do
|
54
|
+
`gem install pkg/#{PKG_FILE_NAME}.gem`
|
55
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'active_shipping'
|
@@ -0,0 +1,50 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2009 Jaded Pixel
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
# a copy of this software and associated documentation files (the
|
6
|
+
# "Software"), to deal in the Software without restriction, including
|
7
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
# the following 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 OF
|
17
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
#++
|
23
|
+
|
24
|
+
$:.unshift File.dirname(__FILE__)
|
25
|
+
|
26
|
+
require 'rubygems'
|
27
|
+
require 'active_support'
|
28
|
+
|
29
|
+
require 'vendor/xml_node/lib/xml_node'
|
30
|
+
require 'vendor/quantified/lib/quantified'
|
31
|
+
require 'quantified/mass'
|
32
|
+
require 'quantified/length'
|
33
|
+
|
34
|
+
require 'net/https'
|
35
|
+
require 'active_shipping/lib/error'
|
36
|
+
require 'active_shipping/lib/requires_parameters'
|
37
|
+
require 'active_shipping/lib/connection'
|
38
|
+
require 'active_shipping/lib/posts_data'
|
39
|
+
require 'active_shipping/lib/country'
|
40
|
+
|
41
|
+
require 'active_shipping/shipping/base'
|
42
|
+
require 'active_shipping/shipping/response'
|
43
|
+
require 'active_shipping/shipping/rate_response'
|
44
|
+
require 'active_shipping/shipping/tracking_response'
|
45
|
+
require 'active_shipping/shipping/package'
|
46
|
+
require 'active_shipping/shipping/location'
|
47
|
+
require 'active_shipping/shipping/rate_estimate'
|
48
|
+
require 'active_shipping/shipping/shipment_event'
|
49
|
+
require 'active_shipping/shipping/carrier'
|
50
|
+
require 'active_shipping/shipping/carriers'
|
@@ -0,0 +1,170 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'net/http'
|
3
|
+
require 'net/https'
|
4
|
+
require 'benchmark'
|
5
|
+
|
6
|
+
module ActiveMerchant
|
7
|
+
class ConnectionError < ActiveMerchantError # :nodoc:
|
8
|
+
end
|
9
|
+
|
10
|
+
class RetriableConnectionError < ConnectionError # :nodoc:
|
11
|
+
end
|
12
|
+
|
13
|
+
class ResponseError < ActiveMerchantError # :nodoc:
|
14
|
+
attr_reader :response
|
15
|
+
|
16
|
+
def initialize(response, message = nil)
|
17
|
+
@response = response
|
18
|
+
@message = message
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_s
|
22
|
+
"Failed with #{response.code} #{response.message if response.respond_to?(:message)}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class Connection
|
27
|
+
MAX_RETRIES = 3
|
28
|
+
OPEN_TIMEOUT = 60
|
29
|
+
READ_TIMEOUT = 60
|
30
|
+
VERIFY_PEER = true
|
31
|
+
RETRY_SAFE = false
|
32
|
+
RUBY_184_POST_HEADERS = { "Content-Type" => "application/x-www-form-urlencoded" }
|
33
|
+
|
34
|
+
attr_accessor :endpoint
|
35
|
+
attr_accessor :open_timeout
|
36
|
+
attr_accessor :read_timeout
|
37
|
+
attr_accessor :verify_peer
|
38
|
+
attr_accessor :retry_safe
|
39
|
+
attr_accessor :pem
|
40
|
+
attr_accessor :pem_password
|
41
|
+
attr_accessor :wiredump_device
|
42
|
+
attr_accessor :logger
|
43
|
+
attr_accessor :tag
|
44
|
+
|
45
|
+
def initialize(endpoint)
|
46
|
+
@endpoint = endpoint.is_a?(URI) ? endpoint : URI.parse(endpoint)
|
47
|
+
@open_timeout = OPEN_TIMEOUT
|
48
|
+
@read_timeout = READ_TIMEOUT
|
49
|
+
@retry_safe = RETRY_SAFE
|
50
|
+
@verify_peer = VERIFY_PEER
|
51
|
+
end
|
52
|
+
|
53
|
+
def request(method, body, headers = {})
|
54
|
+
retry_exceptions do
|
55
|
+
begin
|
56
|
+
info "#{method.to_s.upcase} #{endpoint}", tag
|
57
|
+
|
58
|
+
result = nil
|
59
|
+
|
60
|
+
realtime = Benchmark.realtime do
|
61
|
+
result = case method
|
62
|
+
when :get
|
63
|
+
raise ArgumentError, "GET requests do not support a request body" if body
|
64
|
+
http.get(endpoint.request_uri, headers)
|
65
|
+
when :post
|
66
|
+
debug body
|
67
|
+
http.post(endpoint.request_uri, body, RUBY_184_POST_HEADERS.merge(headers))
|
68
|
+
else
|
69
|
+
raise ArgumentError, "Unsupported request method #{method.to_s.upcase}"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
info "--> %d %s (%d %.4fs)" % [result.code, result.message, result.body ? result.body.length : 0, realtime], tag
|
74
|
+
response = handle_response(result)
|
75
|
+
debug response
|
76
|
+
response
|
77
|
+
rescue EOFError => e
|
78
|
+
raise ConnectionError, "The remote server dropped the connection"
|
79
|
+
rescue Errno::ECONNRESET => e
|
80
|
+
raise ConnectionError, "The remote server reset the connection"
|
81
|
+
rescue Errno::ECONNREFUSED => e
|
82
|
+
raise RetriableConnectionError, "The remote server refused the connection"
|
83
|
+
rescue Timeout::Error, Errno::ETIMEDOUT => e
|
84
|
+
raise ConnectionError, "The connection to the remote server timed out"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
private
|
90
|
+
def http
|
91
|
+
http = Net::HTTP.new(endpoint.host, endpoint.port)
|
92
|
+
configure_debugging(http)
|
93
|
+
configure_timeouts(http)
|
94
|
+
configure_ssl(http)
|
95
|
+
configure_cert(http)
|
96
|
+
http
|
97
|
+
end
|
98
|
+
|
99
|
+
def configure_debugging(http)
|
100
|
+
http.set_debug_output(wiredump_device)
|
101
|
+
end
|
102
|
+
|
103
|
+
def configure_timeouts(http)
|
104
|
+
http.open_timeout = open_timeout
|
105
|
+
http.read_timeout = read_timeout
|
106
|
+
end
|
107
|
+
|
108
|
+
def configure_ssl(http)
|
109
|
+
return unless endpoint.scheme == "https"
|
110
|
+
|
111
|
+
http.use_ssl = true
|
112
|
+
|
113
|
+
if verify_peer
|
114
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
115
|
+
http.ca_file = File.dirname(__FILE__) + '/../../certs/cacert.pem'
|
116
|
+
else
|
117
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
def configure_cert(http)
|
122
|
+
return if pem.blank?
|
123
|
+
|
124
|
+
http.cert = OpenSSL::X509::Certificate.new(pem)
|
125
|
+
|
126
|
+
if pem_password
|
127
|
+
http.key = OpenSSL::PKey::RSA.new(pem, pem_password)
|
128
|
+
else
|
129
|
+
http.key = OpenSSL::PKey::RSA.new(pem)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def retry_exceptions
|
134
|
+
retries = MAX_RETRIES
|
135
|
+
begin
|
136
|
+
yield
|
137
|
+
rescue RetriableConnectionError => e
|
138
|
+
retries -= 1
|
139
|
+
retry unless retries.zero?
|
140
|
+
raise ConnectionError, e.message
|
141
|
+
rescue ConnectionError
|
142
|
+
retries -= 1
|
143
|
+
retry if retry_safe && !retries.zero?
|
144
|
+
raise
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
def handle_response(response)
|
149
|
+
case response.code.to_i
|
150
|
+
when 200...300
|
151
|
+
response.body
|
152
|
+
else
|
153
|
+
raise ResponseError.new(response)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
def debug(message, tag = nil)
|
158
|
+
log(:debug, message, tag)
|
159
|
+
end
|
160
|
+
|
161
|
+
def info(message, tag = nil)
|
162
|
+
log(:info, message, tag)
|
163
|
+
end
|
164
|
+
|
165
|
+
def log(level, message, tag)
|
166
|
+
message = "[#{tag}] #{message}" if tag
|
167
|
+
logger.send(level, message) if logger
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|