benhutton-active_shipping 0.9.13
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +38 -0
- data/MIT-LICENSE +20 -0
- data/README.markdown +142 -0
- data/lib/active_merchant/common.rb +14 -0
- data/lib/active_merchant/common/connection.rb +177 -0
- data/lib/active_merchant/common/country.rb +328 -0
- data/lib/active_merchant/common/error.rb +26 -0
- data/lib/active_merchant/common/post_data.rb +24 -0
- data/lib/active_merchant/common/posts_data.rb +63 -0
- data/lib/active_merchant/common/requires_parameters.rb +16 -0
- data/lib/active_merchant/common/utils.rb +22 -0
- data/lib/active_merchant/common/validateable.rb +76 -0
- data/lib/active_shipping.rb +49 -0
- data/lib/active_shipping/shipping/base.rb +13 -0
- data/lib/active_shipping/shipping/carrier.rb +70 -0
- data/lib/active_shipping/shipping/carriers.rb +20 -0
- data/lib/active_shipping/shipping/carriers/bogus_carrier.rb +16 -0
- data/lib/active_shipping/shipping/carriers/canada_post.rb +268 -0
- data/lib/active_shipping/shipping/carriers/fedex.rb +331 -0
- data/lib/active_shipping/shipping/carriers/kunaki.rb +165 -0
- data/lib/active_shipping/shipping/carriers/new_zealand_post.rb +139 -0
- data/lib/active_shipping/shipping/carriers/shipwire.rb +172 -0
- data/lib/active_shipping/shipping/carriers/ups.rb +390 -0
- data/lib/active_shipping/shipping/carriers/usps.rb +441 -0
- data/lib/active_shipping/shipping/location.rb +109 -0
- data/lib/active_shipping/shipping/package.rb +147 -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 +46 -0
- data/lib/active_shipping/shipping/shipment_event.rb +14 -0
- data/lib/active_shipping/shipping/tracking_response.rb +22 -0
- data/lib/active_shipping/version.rb +3 -0
- data/lib/certs/cacert.pem +7815 -0
- data/lib/certs/eParcel.dtd +111 -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 +8 -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
- metadata +125 -0
data/CHANGELOG
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
2011/04/21:
|
2
|
+
* USPS updated to use new APIs [james]
|
3
|
+
* new :gift boolean option for Package [james]
|
4
|
+
* Location's :address_type can be "po_box" [james]
|
5
|
+
|
6
|
+
Earlier:
|
7
|
+
* New Zealand Post [AbleTech]
|
8
|
+
* Include address name for rate requests to Shipwire if provided [dennis]
|
9
|
+
* Add support for address name to Location [dennis]
|
10
|
+
* Add fix for updated USPS API to strip any encoded html and trailing asterisks from rate names [dennis]
|
11
|
+
* Add carrier CanadaPost [william]
|
12
|
+
* Update FedEx rates and added ability to auto-generate rate name from code that gets returned by FedEx [dennis]
|
13
|
+
* Assume test_helper is in load path when running tests [cody]
|
14
|
+
* Add support Kunaki rating service [cody]
|
15
|
+
* Require active_support instead of activesupport to avoid deprecation warning in Rails 2.3.5 [cody]
|
16
|
+
* Remove ftools for Rails 1.9 compatibility and remove xml logging, as logging is now included in the connection [cody]
|
17
|
+
* Update connection code from ActiveMerchant [cody]
|
18
|
+
* Fix space-ridden USPS usernames when validating credentials [james]
|
19
|
+
* Remove extra slash from USPS URLs [james]
|
20
|
+
* Update Shipwire endpoint hostname [cody]
|
21
|
+
* Add missing ISO countries [Edward Ocampo-Gooding]
|
22
|
+
* Add support for Guernsey to country.rb [cody]
|
23
|
+
* Use :words_connector instead of connector in RequiresParameters [cody]
|
24
|
+
* Fix extra slash in UPS endpoints [cody]
|
25
|
+
* Add name to Shipwire class [cody]
|
26
|
+
* Improve FedEx handling of some error conditions [cody]
|
27
|
+
* Add support for validating credentials to Shipwire [cody]
|
28
|
+
* Add support for ssl_get to PostsData. Update Carriers to use PostsData module. Turn on retry safety for carriers [cody]
|
29
|
+
* Add support for Shipwire Shipping Rate API [cody]
|
30
|
+
* Cleanup package tests [cody]
|
31
|
+
* Remove unused Carrier#setup method [cody]
|
32
|
+
* Don't use Array splat in Regex comparisons in Package [cody]
|
33
|
+
* Default the Location to use the :alpha2 country code format [cody]
|
34
|
+
* Add configurable timeouts from Active Merchant [cody]
|
35
|
+
* Update xml_node.rb from XML Node [cody]
|
36
|
+
* Update requires_parameters from ActiveMerchant [cody]
|
37
|
+
* Sync posts_data.rb with ActiveMerchant [cody]
|
38
|
+
* 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,142 @@
|
|
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
|
+
* [Canada Post](http://www.canadapost.ca)
|
20
|
+
* [New Zealand Post](http://www.nzpost.co.nz)
|
21
|
+
* more soon!
|
22
|
+
|
23
|
+
## Prerequisites
|
24
|
+
|
25
|
+
* [active_support](http://github.com/rails/rails/tree/master/activesupport)
|
26
|
+
* [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)
|
27
|
+
* [mocha](http://mocha.rubyforge.org/) for the tests
|
28
|
+
|
29
|
+
## Download & Installation
|
30
|
+
|
31
|
+
Currently this library is available on GitHub:
|
32
|
+
|
33
|
+
<http://github.com/Shopify/active_shipping>
|
34
|
+
|
35
|
+
You will need to get [Git][] if you don't have it. Then:
|
36
|
+
|
37
|
+
> git clone git://github.com/Shopify/active_shipping.git
|
38
|
+
|
39
|
+
(That URL is case-sensitive, so watch out.)
|
40
|
+
|
41
|
+
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.
|
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
|
+
### Compare rates from different carriers
|
49
|
+
|
50
|
+
require 'active_shipping'
|
51
|
+
include ActiveMerchant::Shipping
|
52
|
+
|
53
|
+
# Package up a poster and a Wii for your nephew.
|
54
|
+
packages = [
|
55
|
+
Package.new( 100, # 100 grams
|
56
|
+
[93,10], # 93 cm long, 10 cm diameter
|
57
|
+
:cylinder => true), # cylinders have different volume calculations
|
58
|
+
|
59
|
+
Package.new( (7.5 * 16), # 7.5 lbs, times 16 oz/lb.
|
60
|
+
[15, 10, 4.5], # 15x10x4.5 inches
|
61
|
+
:units => :imperial) # not grams, not centimetres
|
62
|
+
]
|
63
|
+
|
64
|
+
# You live in Beverly Hills, he lives in Ottawa
|
65
|
+
origin = Location.new( :country => 'US',
|
66
|
+
:state => 'CA',
|
67
|
+
:city => 'Beverly Hills',
|
68
|
+
:zip => '90210')
|
69
|
+
|
70
|
+
destination = Location.new( :country => 'CA',
|
71
|
+
:province => 'ON',
|
72
|
+
:city => 'Ottawa',
|
73
|
+
:postal_code => 'K1P 1J1')
|
74
|
+
|
75
|
+
# Find out how much it'll be.
|
76
|
+
ups = UPS.new(:login => 'auntjudy', :password => 'secret', :key => 'xml-access-key')
|
77
|
+
response = ups.find_rates(origin, destination, packages)
|
78
|
+
|
79
|
+
ups_rates = response.rates.sort_by(&:price).collect {|rate| [rate.service_name, rate.price]}
|
80
|
+
# => [["UPS Standard", 3936],
|
81
|
+
# ["UPS Worldwide Expedited", 8682],
|
82
|
+
# ["UPS Saver", 9348],
|
83
|
+
# ["UPS Express", 9702],
|
84
|
+
# ["UPS Worldwide Express Plus", 14502]]
|
85
|
+
|
86
|
+
# Check out USPS for comparison...
|
87
|
+
usps = USPS.new(:login => 'developer-key')
|
88
|
+
response = usps.find_rates(origin, destination, packages)
|
89
|
+
|
90
|
+
usps_rates = response.rates.sort_by(&:price).collect {|rate| [rate.service_name, rate.price]}
|
91
|
+
# => [["USPS Priority Mail International", 4110],
|
92
|
+
# ["USPS Express Mail International (EMS)", 5750],
|
93
|
+
# ["USPS Global Express Guaranteed Non-Document Non-Rectangular", 9400],
|
94
|
+
# ["USPS GXG Envelopes", 9400],
|
95
|
+
# ["USPS Global Express Guaranteed Non-Document Rectangular", 9400],
|
96
|
+
# ["USPS Global Express Guaranteed", 9400]]
|
97
|
+
|
98
|
+
### Track a FedEx package
|
99
|
+
|
100
|
+
fedex = FedEx.new(:login => '999999999', :password => '7777777')
|
101
|
+
tracking_info = fedex.find_tracking_info('tracking-number', :carrier_code => 'fedex_ground') # Ground package
|
102
|
+
|
103
|
+
tracking_info.shipment_events.each do |event|
|
104
|
+
puts "#{event.name} at #{event.location.city}, #{event.location.state} on #{event.time}. #{event.message}"
|
105
|
+
end
|
106
|
+
# => Package information transmitted to FedEx at NASHVILLE LOCAL, TN on Thu Oct 23 00:00:00 UTC 2008.
|
107
|
+
# Picked up by FedEx at NASHVILLE LOCAL, TN on Thu Oct 23 17:30:00 UTC 2008.
|
108
|
+
# Scanned at FedEx sort facility at NASHVILLE, TN on Thu Oct 23 18:50:00 UTC 2008.
|
109
|
+
# Departed FedEx sort facility at NASHVILLE, TN on Thu Oct 23 22:33:00 UTC 2008.
|
110
|
+
# Arrived at FedEx sort facility at KNOXVILLE, TN on Fri Oct 24 02:45:00 UTC 2008.
|
111
|
+
# Scanned at FedEx sort facility at KNOXVILLE, TN on Fri Oct 24 05:56:00 UTC 2008.
|
112
|
+
# Delivered at Knoxville, TN on Fri Oct 24 16:45:00 UTC 2008. Signed for by: T.BAKER
|
113
|
+
|
114
|
+
## TODO
|
115
|
+
|
116
|
+
* proper documentation
|
117
|
+
* carrier code template generator
|
118
|
+
* more carriers
|
119
|
+
* support more features for existing carriers
|
120
|
+
* bin-packing algorithm (preferably implemented in ruby)
|
121
|
+
* order tracking
|
122
|
+
* label printing
|
123
|
+
|
124
|
+
## Contributing
|
125
|
+
|
126
|
+
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.
|
127
|
+
|
128
|
+
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.
|
129
|
+
|
130
|
+
[git-diff]:http://www.kernel.org/pub/software/scm/git/docs/git-diff.html
|
131
|
+
|
132
|
+
## Contributors
|
133
|
+
|
134
|
+
* James MacAulay (<http://jmacaulay.net>)
|
135
|
+
* Tobias Luetke (<http://blog.leetsoft.com>)
|
136
|
+
* Cody Fauser (<http://codyfauser.com>)
|
137
|
+
* Jimmy Baker (<http://jimmyville.com/>)
|
138
|
+
* William Lang (<http://williamlang.net/>)
|
139
|
+
|
140
|
+
## Legal Mumbo Jumbo
|
141
|
+
|
142
|
+
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.
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module ActiveMerchant
|
2
|
+
autoload :Connection, 'active_merchant/common/connection'
|
3
|
+
autoload :Country, 'active_merchant/common/country'
|
4
|
+
autoload :ActiveMerchantError, 'active_merchant/common/error'
|
5
|
+
autoload :ConnectionError, 'active_merchant/common/error'
|
6
|
+
autoload :RetriableConnectionError, 'active_merchant/common/error'
|
7
|
+
autoload :ResponseError, 'active_merchant/common/error'
|
8
|
+
autoload :ClientCertificateError, 'active_merchant/common/error'
|
9
|
+
autoload :PostData, 'active_merchant/common/post_data'
|
10
|
+
autoload :PostsData, 'active_merchant/common/posts_data'
|
11
|
+
autoload :RequiresParameters, 'active_merchant/common/requires_parameters'
|
12
|
+
autoload :Utils, 'active_merchant/common/utils'
|
13
|
+
autoload :Validateable, 'active_merchant/common/validateable'
|
14
|
+
end
|
@@ -0,0 +1,177 @@
|
|
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
|
+
attr_accessor :ignore_http_status
|
45
|
+
|
46
|
+
def initialize(endpoint)
|
47
|
+
@endpoint = endpoint.is_a?(URI) ? endpoint : URI.parse(endpoint)
|
48
|
+
@open_timeout = OPEN_TIMEOUT
|
49
|
+
@read_timeout = READ_TIMEOUT
|
50
|
+
@retry_safe = RETRY_SAFE
|
51
|
+
@verify_peer = VERIFY_PEER
|
52
|
+
@ignore_http_status = false
|
53
|
+
end
|
54
|
+
|
55
|
+
def request(method, body, headers = {})
|
56
|
+
retry_exceptions do
|
57
|
+
begin
|
58
|
+
info "#{method.to_s.upcase} #{endpoint}", tag
|
59
|
+
|
60
|
+
result = nil
|
61
|
+
|
62
|
+
realtime = Benchmark.realtime do
|
63
|
+
result = case method
|
64
|
+
when :get
|
65
|
+
raise ArgumentError, "GET requests do not support a request body" if body
|
66
|
+
http.get(endpoint.request_uri, headers)
|
67
|
+
when :post
|
68
|
+
debug body
|
69
|
+
http.post(endpoint.request_uri, body, RUBY_184_POST_HEADERS.merge(headers))
|
70
|
+
else
|
71
|
+
raise ArgumentError, "Unsupported request method #{method.to_s.upcase}"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
info "--> %d %s (%d %.4fs)" % [result.code, result.message, result.body ? result.body.length : 0, realtime], tag
|
76
|
+
debug result.body
|
77
|
+
result
|
78
|
+
rescue EOFError => e
|
79
|
+
raise ConnectionError, "The remote server dropped the connection"
|
80
|
+
rescue Errno::ECONNRESET => e
|
81
|
+
raise ConnectionError, "The remote server reset the connection"
|
82
|
+
rescue Errno::ECONNREFUSED => e
|
83
|
+
raise RetriableConnectionError, "The remote server refused the connection"
|
84
|
+
rescue OpenSSL::X509::CertificateError => e
|
85
|
+
raise ClientCertificateError, "The remote server did not accept the provided SSL certificate"
|
86
|
+
rescue Timeout::Error, Errno::ETIMEDOUT => e
|
87
|
+
raise ConnectionError, "The connection to the remote server timed out"
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
private
|
93
|
+
def http
|
94
|
+
http = Net::HTTP.new(endpoint.host, endpoint.port)
|
95
|
+
configure_debugging(http)
|
96
|
+
configure_timeouts(http)
|
97
|
+
configure_ssl(http)
|
98
|
+
configure_cert(http)
|
99
|
+
http
|
100
|
+
end
|
101
|
+
|
102
|
+
def configure_debugging(http)
|
103
|
+
http.set_debug_output(wiredump_device)
|
104
|
+
end
|
105
|
+
|
106
|
+
def configure_timeouts(http)
|
107
|
+
http.open_timeout = open_timeout
|
108
|
+
http.read_timeout = read_timeout
|
109
|
+
end
|
110
|
+
|
111
|
+
def configure_ssl(http)
|
112
|
+
return unless endpoint.scheme == "https"
|
113
|
+
|
114
|
+
http.use_ssl = true
|
115
|
+
|
116
|
+
if verify_peer
|
117
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
118
|
+
http.ca_file = File.dirname(__FILE__) + '/../../certs/cacert.pem'
|
119
|
+
else
|
120
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def configure_cert(http)
|
125
|
+
return if pem.blank?
|
126
|
+
|
127
|
+
http.cert = OpenSSL::X509::Certificate.new(pem)
|
128
|
+
|
129
|
+
if pem_password
|
130
|
+
http.key = OpenSSL::PKey::RSA.new(pem, pem_password)
|
131
|
+
else
|
132
|
+
http.key = OpenSSL::PKey::RSA.new(pem)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def retry_exceptions
|
137
|
+
retries = MAX_RETRIES
|
138
|
+
begin
|
139
|
+
yield
|
140
|
+
rescue RetriableConnectionError => e
|
141
|
+
retries -= 1
|
142
|
+
retry unless retries.zero?
|
143
|
+
raise ConnectionError, e.message
|
144
|
+
rescue ConnectionError
|
145
|
+
retries -= 1
|
146
|
+
retry if retry_safe && !retries.zero?
|
147
|
+
raise
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
def handle_response(response)
|
152
|
+
if @ignore_http_status then
|
153
|
+
return response.body
|
154
|
+
else
|
155
|
+
case response.code.to_i
|
156
|
+
when 200...300
|
157
|
+
response.body
|
158
|
+
else
|
159
|
+
raise ResponseError.new(response)
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
def debug(message, tag = nil)
|
165
|
+
log(:debug, message, tag)
|
166
|
+
end
|
167
|
+
|
168
|
+
def info(message, tag = nil)
|
169
|
+
log(:info, message, tag)
|
170
|
+
end
|
171
|
+
|
172
|
+
def log(level, message, tag)
|
173
|
+
message = "[#{tag}] #{message}" if tag
|
174
|
+
logger.send(level, message) if logger
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
@@ -0,0 +1,328 @@
|
|
1
|
+
#!ruby19
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
module ActiveMerchant #:nodoc:
|
5
|
+
class InvalidCountryCodeError < StandardError
|
6
|
+
end
|
7
|
+
|
8
|
+
class CountryCodeFormatError < StandardError
|
9
|
+
end
|
10
|
+
|
11
|
+
class CountryCode
|
12
|
+
attr_reader :value, :format
|
13
|
+
def initialize(value)
|
14
|
+
@value = value.to_s.upcase
|
15
|
+
detect_format
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_s
|
19
|
+
value
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def detect_format
|
25
|
+
case @value
|
26
|
+
when /^[[:alpha:]]{2}$/
|
27
|
+
@format = :alpha2
|
28
|
+
when /^[[:alpha:]]{3}$/
|
29
|
+
@format = :alpha3
|
30
|
+
when /^[[:digit:]]{3}$/
|
31
|
+
@format = :numeric
|
32
|
+
else
|
33
|
+
raise CountryCodeFormatError, "The country code is not formatted correctly #{@value}"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class Country
|
39
|
+
include RequiresParameters
|
40
|
+
attr_reader :name
|
41
|
+
|
42
|
+
def initialize(options = {})
|
43
|
+
requires!(options, :name, :alpha2, :alpha3, :numeric)
|
44
|
+
@name = options.delete(:name)
|
45
|
+
@codes = options.collect{|k,v| CountryCode.new(v)}
|
46
|
+
end
|
47
|
+
|
48
|
+
def code(format)
|
49
|
+
@codes.detect{|c| c.format == format}
|
50
|
+
end
|
51
|
+
|
52
|
+
def ==(other)
|
53
|
+
(@name == other.name)
|
54
|
+
end
|
55
|
+
alias eql? ==
|
56
|
+
|
57
|
+
def hash
|
58
|
+
@name.hash
|
59
|
+
end
|
60
|
+
|
61
|
+
def to_s
|
62
|
+
@name
|
63
|
+
end
|
64
|
+
|
65
|
+
COUNTRIES = [
|
66
|
+
{ :alpha2 => 'AF', :name => 'Afghanistan', :alpha3 => 'AFG', :numeric => '004' },
|
67
|
+
{ :alpha2 => 'AL', :name => 'Albania', :alpha3 => 'ALB', :numeric => '008' },
|
68
|
+
{ :alpha2 => 'DZ', :name => 'Algeria', :alpha3 => 'DZA', :numeric => '012' },
|
69
|
+
{ :alpha2 => 'AS', :name => 'American Samoa', :alpha3 => 'ASM', :numeric => '016' },
|
70
|
+
{ :alpha2 => 'AD', :name => 'Andorra', :alpha3 => 'AND', :numeric => '020' },
|
71
|
+
{ :alpha2 => 'AO', :name => 'Angola', :alpha3 => 'AGO', :numeric => '024' },
|
72
|
+
{ :alpha2 => 'AI', :name => 'Anguilla', :alpha3 => 'AIA', :numeric => '660' },
|
73
|
+
{ :alpha2 => 'AG', :name => 'Antigua and Barbuda', :alpha3 => 'ATG', :numeric => '028' },
|
74
|
+
{ :alpha2 => 'AR', :name => 'Argentina', :alpha3 => 'ARG', :numeric => '032' },
|
75
|
+
{ :alpha2 => 'AM', :name => 'Armenia', :alpha3 => 'ARM', :numeric => '051' },
|
76
|
+
{ :alpha2 => 'AW', :name => 'Aruba', :alpha3 => 'ABW', :numeric => '533' },
|
77
|
+
{ :alpha2 => 'AU', :name => 'Australia', :alpha3 => 'AUS', :numeric => '036' },
|
78
|
+
{ :alpha2 => 'AT', :name => 'Austria', :alpha3 => 'AUT', :numeric => '040' },
|
79
|
+
{ :alpha2 => 'AZ', :name => 'Azerbaijan', :alpha3 => 'AZE', :numeric => '031' },
|
80
|
+
{ :alpha2 => 'BS', :name => 'Bahamas', :alpha3 => 'BHS', :numeric => '044' },
|
81
|
+
{ :alpha2 => 'BH', :name => 'Bahrain', :alpha3 => 'BHR', :numeric => '048' },
|
82
|
+
{ :alpha2 => 'BD', :name => 'Bangladesh', :alpha3 => 'BGD', :numeric => '050' },
|
83
|
+
{ :alpha2 => 'BB', :name => 'Barbados', :alpha3 => 'BRB', :numeric => '052' },
|
84
|
+
{ :alpha2 => 'BY', :name => 'Belarus', :alpha3 => 'BLR', :numeric => '112' },
|
85
|
+
{ :alpha2 => 'BE', :name => 'Belgium', :alpha3 => 'BEL', :numeric => '056' },
|
86
|
+
{ :alpha2 => 'BZ', :name => 'Belize', :alpha3 => 'BLZ', :numeric => '084' },
|
87
|
+
{ :alpha2 => 'BJ', :name => 'Benin', :alpha3 => 'BEN', :numeric => '204' },
|
88
|
+
{ :alpha2 => 'BM', :name => 'Bermuda', :alpha3 => 'BMU', :numeric => '060' },
|
89
|
+
{ :alpha2 => 'BT', :name => 'Bhutan', :alpha3 => 'BTN', :numeric => '064' },
|
90
|
+
{ :alpha2 => 'BO', :name => 'Bolivia', :alpha3 => 'BOL', :numeric => '068' },
|
91
|
+
{ :alpha2 => 'BA', :name => 'Bosnia and Herzegovina', :alpha3 => 'BIH', :numeric => '070' },
|
92
|
+
{ :alpha2 => 'BW', :name => 'Botswana', :alpha3 => 'BWA', :numeric => '072' },
|
93
|
+
{ :alpha2 => 'BV', :name => 'Bouvet Island', :alpha3 => 'BVD', :numeric => '074' },
|
94
|
+
{ :alpha2 => 'BR', :name => 'Brazil', :alpha3 => 'BRA', :numeric => '076' },
|
95
|
+
{ :alpha2 => 'IO', :name => 'British Indian Ocean Territory', :alpha3 => 'IOT', :numeric => '086' },
|
96
|
+
{ :alpha2 => 'BN', :name => 'Brunei Darussalam', :alpha3 => 'BRN', :numeric => '096' },
|
97
|
+
{ :alpha2 => 'BG', :name => 'Bulgaria', :alpha3 => 'BGR', :numeric => '100' },
|
98
|
+
{ :alpha2 => 'BF', :name => 'Burkina Faso', :alpha3 => 'BFA', :numeric => '854' },
|
99
|
+
{ :alpha2 => 'BI', :name => 'Burundi', :alpha3 => 'BDI', :numeric => '108' },
|
100
|
+
{ :alpha2 => 'KH', :name => 'Cambodia', :alpha3 => 'KHM', :numeric => '116' },
|
101
|
+
{ :alpha2 => 'CM', :name => 'Cameroon', :alpha3 => 'CMR', :numeric => '120' },
|
102
|
+
{ :alpha2 => 'CA', :name => 'Canada', :alpha3 => 'CAN', :numeric => '124' },
|
103
|
+
{ :alpha2 => 'CV', :name => 'Cape Verde', :alpha3 => 'CPV', :numeric => '132' },
|
104
|
+
{ :alpha2 => 'KY', :name => 'Cayman Islands', :alpha3 => 'CYM', :numeric => '136' },
|
105
|
+
{ :alpha2 => 'CF', :name => 'Central African Republic', :alpha3 => 'CAF', :numeric => '140' },
|
106
|
+
{ :alpha2 => 'TD', :name => 'Chad', :alpha3 => 'TCD', :numeric => '148' },
|
107
|
+
{ :alpha2 => 'CL', :name => 'Chile', :alpha3 => 'CHL', :numeric => '152' },
|
108
|
+
{ :alpha2 => 'CN', :name => 'China', :alpha3 => 'CHN', :numeric => '156' },
|
109
|
+
{ :alpha2 => 'CX', :name => 'Christmas Island', :alpha3 => 'CXR', :numeric => '162' },
|
110
|
+
{ :alpha2 => 'CC', :name => 'Cocos (Keeling) Islands', :alpha3 => 'CCK', :numeric => '166' },
|
111
|
+
{ :alpha2 => 'CO', :name => 'Colombia', :alpha3 => 'COL', :numeric => '170' },
|
112
|
+
{ :alpha2 => 'KM', :name => 'Comoros', :alpha3 => 'COM', :numeric => '174' },
|
113
|
+
{ :alpha2 => 'CG', :name => 'Congo', :alpha3 => 'COG', :numeric => '178' },
|
114
|
+
{ :alpha2 => 'CD', :name => 'Congo, the Democratic Republic of the', :alpha3 => 'COD', :numeric => '180' },
|
115
|
+
{ :alpha2 => 'CK', :name => 'Cook Islands', :alpha3 => 'COK', :numeric => '184' },
|
116
|
+
{ :alpha2 => 'CR', :name => 'Costa Rica', :alpha3 => 'CRI', :numeric => '188' },
|
117
|
+
{ :alpha2 => 'CI', :name => 'Cote D\'Ivoire', :alpha3 => 'CIV', :numeric => '384' },
|
118
|
+
{ :alpha2 => 'HR', :name => 'Croatia', :alpha3 => 'HRV', :numeric => '191' },
|
119
|
+
{ :alpha2 => 'CU', :name => 'Cuba', :alpha3 => 'CUB', :numeric => '192' },
|
120
|
+
{ :alpha2 => 'CY', :name => 'Cyprus', :alpha3 => 'CYP', :numeric => '196' },
|
121
|
+
{ :alpha2 => 'CZ', :name => 'Czech Republic', :alpha3 => 'CZE', :numeric => '203' },
|
122
|
+
{ :alpha2 => 'DK', :name => 'Denmark', :alpha3 => 'DNK', :numeric => '208' },
|
123
|
+
{ :alpha2 => 'DJ', :name => 'Djibouti', :alpha3 => 'DJI', :numeric => '262' },
|
124
|
+
{ :alpha2 => 'DM', :name => 'Dominica', :alpha3 => 'DMA', :numeric => '212' },
|
125
|
+
{ :alpha2 => 'DO', :name => 'Dominican Republic', :alpha3 => 'DOM', :numeric => '214' },
|
126
|
+
{ :alpha2 => 'EC', :name => 'Ecuador', :alpha3 => 'ECU', :numeric => '218' },
|
127
|
+
{ :alpha2 => 'EG', :name => 'Egypt', :alpha3 => 'EGY', :numeric => '818' },
|
128
|
+
{ :alpha2 => 'SV', :name => 'El Salvador', :alpha3 => 'SLV', :numeric => '222' },
|
129
|
+
{ :alpha2 => 'GQ', :name => 'Equatorial Guinea', :alpha3 => 'GNQ', :numeric => '226' },
|
130
|
+
{ :alpha2 => 'ER', :name => 'Eritrea', :alpha3 => 'ERI', :numeric => '232' },
|
131
|
+
{ :alpha2 => 'EE', :name => 'Estonia', :alpha3 => 'EST', :numeric => '233' },
|
132
|
+
{ :alpha2 => 'ET', :name => 'Ethiopia', :alpha3 => 'ETH', :numeric => '231' },
|
133
|
+
{ :alpha2 => 'FK', :name => 'Falkland Islands (Malvinas)', :alpha3 => 'FLK', :numeric => '238' },
|
134
|
+
{ :alpha2 => 'FO', :name => 'Faroe Islands', :alpha3 => 'FRO', :numeric => '234' },
|
135
|
+
{ :alpha2 => 'FJ', :name => 'Fiji', :alpha3 => 'FJI', :numeric => '242' },
|
136
|
+
{ :alpha2 => 'FI', :name => 'Finland', :alpha3 => 'FIN', :numeric => '246' },
|
137
|
+
{ :alpha2 => 'FR', :name => 'France', :alpha3 => 'FRA', :numeric => '250' },
|
138
|
+
{ :alpha2 => 'GF', :name => 'French Guiana', :alpha3 => 'GUF', :numeric => '254' },
|
139
|
+
{ :alpha2 => 'PF', :name => 'French Polynesia', :alpha3 => 'PYF', :numeric => '258' },
|
140
|
+
{ :alpha2 => 'TF', :name => 'French Southern Territories', :alpha3 => 'ATF', :numeric => '260' },
|
141
|
+
{ :alpha2 => 'GA', :name => 'Gabon', :alpha3 => 'GAB', :numeric => '266' },
|
142
|
+
{ :alpha2 => 'GM', :name => 'Gambia', :alpha3 => 'GMB', :numeric => '270' },
|
143
|
+
{ :alpha2 => 'GE', :name => 'Georgia', :alpha3 => 'GEO', :numeric => '268' },
|
144
|
+
{ :alpha2 => 'DE', :name => 'Germany', :alpha3 => 'DEU', :numeric => '276' },
|
145
|
+
{ :alpha2 => 'GH', :name => 'Ghana', :alpha3 => 'GHA', :numeric => '288' },
|
146
|
+
{ :alpha2 => 'GI', :name => 'Gibraltar', :alpha3 => 'GIB', :numeric => '292' },
|
147
|
+
{ :alpha2 => 'GR', :name => 'Greece', :alpha3 => 'GRC', :numeric => '300' },
|
148
|
+
{ :alpha2 => 'GL', :name => 'Greenland', :alpha3 => 'GRL', :numeric => '304' },
|
149
|
+
{ :alpha2 => 'GD', :name => 'Grenada', :alpha3 => 'GRD', :numeric => '308' },
|
150
|
+
{ :alpha2 => 'GP', :name => 'Guadeloupe', :alpha3 => 'GLP', :numeric => '312' },
|
151
|
+
{ :alpha2 => 'GU', :name => 'Guam', :alpha3 => 'GUM', :numeric => '316' },
|
152
|
+
{ :alpha2 => 'GT', :name => 'Guatemala', :alpha3 => 'GTM', :numeric => '320' },
|
153
|
+
{ :alpha2 => 'GG', :name => 'Guernsey', :alpha3 => 'GGY', :numeric => '831' },
|
154
|
+
{ :alpha2 => 'GN', :name => 'Guinea', :alpha3 => 'GIN', :numeric => '324' },
|
155
|
+
{ :alpha2 => 'GW', :name => 'Guinea-Bissau', :alpha3 => 'GNB', :numeric => '624' },
|
156
|
+
{ :alpha2 => 'GY', :name => 'Guyana', :alpha3 => 'GUY', :numeric => '328' },
|
157
|
+
{ :alpha2 => 'HT', :name => 'Haiti', :alpha3 => 'HTI', :numeric => '332' },
|
158
|
+
{ :alpha2 => 'HM', :name => 'Heard Island And Mcdonald Islands', :alpha3 => 'HMD', :numeric => '334' },
|
159
|
+
{ :alpha2 => 'VA', :name => 'Holy See (Vatican City State)', :alpha3 => 'VAT', :numeric => '336' },
|
160
|
+
{ :alpha2 => 'HN', :name => 'Honduras', :alpha3 => 'HND', :numeric => '340' },
|
161
|
+
{ :alpha2 => 'HK', :name => 'Hong Kong', :alpha3 => 'HKG', :numeric => '344' },
|
162
|
+
{ :alpha2 => 'HU', :name => 'Hungary', :alpha3 => 'HUN', :numeric => '348' },
|
163
|
+
{ :alpha2 => 'IS', :name => 'Iceland', :alpha3 => 'ISL', :numeric => '352' },
|
164
|
+
{ :alpha2 => 'IN', :name => 'India', :alpha3 => 'IND', :numeric => '356' },
|
165
|
+
{ :alpha2 => 'ID', :name => 'Indonesia', :alpha3 => 'IDN', :numeric => '360' },
|
166
|
+
{ :alpha2 => 'IR', :name => 'Iran, Islamic Republic of', :alpha3 => 'IRN', :numeric => '364' },
|
167
|
+
{ :alpha2 => 'IQ', :name => 'Iraq', :alpha3 => 'IRQ', :numeric => '368' },
|
168
|
+
{ :alpha2 => 'IE', :name => 'Ireland', :alpha3 => 'IRL', :numeric => '372' },
|
169
|
+
{ :alpha2 => 'IM', :name => 'Isle Of Man', :alpha3 => 'IMN', :numeric => '833' },
|
170
|
+
{ :alpha2 => 'IL', :name => 'Israel', :alpha3 => 'ISR', :numeric => '376' },
|
171
|
+
{ :alpha2 => 'IT', :name => 'Italy', :alpha3 => 'ITA', :numeric => '380' },
|
172
|
+
{ :alpha2 => 'JM', :name => 'Jamaica', :alpha3 => 'JAM', :numeric => '388' },
|
173
|
+
{ :alpha2 => 'JP', :name => 'Japan', :alpha3 => 'JPN', :numeric => '392' },
|
174
|
+
{ :alpha2 => 'JE', :name => 'Jersey', :alpha3 => 'JEY', :numeric => '832' },
|
175
|
+
{ :alpha2 => 'JO', :name => 'Jordan', :alpha3 => 'JOR', :numeric => '400' },
|
176
|
+
{ :alpha2 => 'KZ', :name => 'Kazakhstan', :alpha3 => 'KAZ', :numeric => '398' },
|
177
|
+
{ :alpha2 => 'KE', :name => 'Kenya', :alpha3 => 'KEN', :numeric => '404' },
|
178
|
+
{ :alpha2 => 'KI', :name => 'Kiribati', :alpha3 => 'KIR', :numeric => '296' },
|
179
|
+
{ :alpha2 => 'KP', :name => 'Korea, Democratic People\'s Republic of', :alpha3 => 'PRK', :numeric => '408' },
|
180
|
+
{ :alpha2 => 'KR', :name => 'Korea, Republic of', :alpha3 => 'KOR', :numeric => '410' },
|
181
|
+
{ :alpha2 => 'KW', :name => 'Kuwait', :alpha3 => 'KWT', :numeric => '414' },
|
182
|
+
{ :alpha2 => 'KG', :name => 'Kyrgyzstan', :alpha3 => 'KGZ', :numeric => '417' },
|
183
|
+
{ :alpha2 => 'LA', :name => 'Lao People\'s Democratic Republic', :alpha3 => 'LAO', :numeric => '418' },
|
184
|
+
{ :alpha2 => 'LV', :name => 'Latvia', :alpha3 => 'LVA', :numeric => '428' },
|
185
|
+
{ :alpha2 => 'LB', :name => 'Lebanon', :alpha3 => 'LBN', :numeric => '422' },
|
186
|
+
{ :alpha2 => 'LS', :name => 'Lesotho', :alpha3 => 'LSO', :numeric => '426' },
|
187
|
+
{ :alpha2 => 'LR', :name => 'Liberia', :alpha3 => 'LBR', :numeric => '430' },
|
188
|
+
{ :alpha2 => 'LY', :name => 'Libyan Arab Jamahiriya', :alpha3 => 'LBY', :numeric => '434' },
|
189
|
+
{ :alpha2 => 'LI', :name => 'Liechtenstein', :alpha3 => 'LIE', :numeric => '438' },
|
190
|
+
{ :alpha2 => 'LT', :name => 'Lithuania', :alpha3 => 'LTU', :numeric => '440' },
|
191
|
+
{ :alpha2 => 'LU', :name => 'Luxembourg', :alpha3 => 'LUX', :numeric => '442' },
|
192
|
+
{ :alpha2 => 'MO', :name => 'Macao', :alpha3 => 'MAC', :numeric => '446' },
|
193
|
+
{ :alpha2 => 'MK', :name => 'Macedonia, the Former Yugoslav Republic of', :alpha3 => 'MKD', :numeric => '807' },
|
194
|
+
{ :alpha2 => 'MG', :name => 'Madagascar', :alpha3 => 'MDG', :numeric => '450' },
|
195
|
+
{ :alpha2 => 'MW', :name => 'Malawi', :alpha3 => 'MWI', :numeric => '454' },
|
196
|
+
{ :alpha2 => 'MY', :name => 'Malaysia', :alpha3 => 'MYS', :numeric => '458' },
|
197
|
+
{ :alpha2 => 'MV', :name => 'Maldives', :alpha3 => 'MDV', :numeric => '462' },
|
198
|
+
{ :alpha2 => 'ML', :name => 'Mali', :alpha3 => 'MLI', :numeric => '466' },
|
199
|
+
{ :alpha2 => 'MT', :name => 'Malta', :alpha3 => 'MLT', :numeric => '470' },
|
200
|
+
{ :alpha2 => 'MH', :name => 'Marshall Islands', :alpha3 => 'MHL', :numeric => '584' },
|
201
|
+
{ :alpha2 => 'MQ', :name => 'Martinique', :alpha3 => 'MTQ', :numeric => '474' },
|
202
|
+
{ :alpha2 => 'MR', :name => 'Mauritania', :alpha3 => 'MRT', :numeric => '478' },
|
203
|
+
{ :alpha2 => 'MU', :name => 'Mauritius', :alpha3 => 'MUS', :numeric => '480' },
|
204
|
+
{ :alpha2 => 'YT', :name => 'Mayotte', :alpha3 => 'MYT', :numeric => '175' },
|
205
|
+
{ :alpha2 => 'MX', :name => 'Mexico', :alpha3 => 'MEX', :numeric => '484' },
|
206
|
+
{ :alpha2 => 'FM', :name => 'Micronesia, Federated States of', :alpha3 => 'FSM', :numeric => '583' },
|
207
|
+
{ :alpha2 => 'MD', :name => 'Moldova, Republic of', :alpha3 => 'MDA', :numeric => '498' },
|
208
|
+
{ :alpha2 => 'MC', :name => 'Monaco', :alpha3 => 'MCO', :numeric => '492' },
|
209
|
+
{ :alpha2 => 'MN', :name => 'Mongolia', :alpha3 => 'MNG', :numeric => '496' },
|
210
|
+
{ :alpha2 => 'ME', :name => 'Montenegro', :alpha3 => 'MNE', :numeric => '499' },
|
211
|
+
{ :alpha2 => 'MS', :name => 'Montserrat', :alpha3 => 'MSR', :numeric => '500' },
|
212
|
+
{ :alpha2 => 'MA', :name => 'Morocco', :alpha3 => 'MAR', :numeric => '504' },
|
213
|
+
{ :alpha2 => 'MZ', :name => 'Mozambique', :alpha3 => 'MOZ', :numeric => '508' },
|
214
|
+
{ :alpha2 => 'MM', :name => 'Myanmar', :alpha3 => 'MMR', :numeric => '104' },
|
215
|
+
{ :alpha2 => 'NA', :name => 'Namibia', :alpha3 => 'NAM', :numeric => '516' },
|
216
|
+
{ :alpha2 => 'NR', :name => 'Nauru', :alpha3 => 'NRU', :numeric => '520' },
|
217
|
+
{ :alpha2 => 'NP', :name => 'Nepal', :alpha3 => 'NPL', :numeric => '524' },
|
218
|
+
{ :alpha2 => 'NL', :name => 'Netherlands', :alpha3 => 'NLD', :numeric => '528' },
|
219
|
+
{ :alpha2 => 'AN', :name => 'Netherlands Antilles', :alpha3 => 'ANT', :numeric => '530' },
|
220
|
+
{ :alpha2 => 'NC', :name => 'New Caledonia', :alpha3 => 'NCL', :numeric => '540' },
|
221
|
+
{ :alpha2 => 'NZ', :name => 'New Zealand', :alpha3 => 'NZL', :numeric => '554' },
|
222
|
+
{ :alpha2 => 'NI', :name => 'Nicaragua', :alpha3 => 'NIC', :numeric => '558' },
|
223
|
+
{ :alpha2 => 'NE', :name => 'Niger', :alpha3 => 'NER', :numeric => '562' },
|
224
|
+
{ :alpha2 => 'NG', :name => 'Nigeria', :alpha3 => 'NGA', :numeric => '566' },
|
225
|
+
{ :alpha2 => 'NU', :name => 'Niue', :alpha3 => 'NIU', :numeric => '570' },
|
226
|
+
{ :alpha2 => 'NF', :name => 'Norfolk Island', :alpha3 => 'NFK', :numeric => '574' },
|
227
|
+
{ :alpha2 => 'MP', :name => 'Northern Mariana Islands', :alpha3 => 'MNP', :numeric => '580' },
|
228
|
+
{ :alpha2 => 'NO', :name => 'Norway', :alpha3 => 'NOR', :numeric => '578' },
|
229
|
+
{ :alpha2 => 'OM', :name => 'Oman', :alpha3 => 'OMN', :numeric => '512' },
|
230
|
+
{ :alpha2 => 'PK', :name => 'Pakistan', :alpha3 => 'PAK', :numeric => '586' },
|
231
|
+
{ :alpha2 => 'PW', :name => 'Palau', :alpha3 => 'PLW', :numeric => '585' },
|
232
|
+
{ :alpha2 => 'PS', :name => 'Palestinian Territory, Occupied', :alpha3 => 'PSE', :numeric => '275' },
|
233
|
+
{ :alpha2 => 'PA', :name => 'Panama', :alpha3 => 'PAN', :numeric => '591' },
|
234
|
+
{ :alpha2 => 'PG', :name => 'Papua New Guinea', :alpha3 => 'PNG', :numeric => '598' },
|
235
|
+
{ :alpha2 => 'PY', :name => 'Paraguay', :alpha3 => 'PRY', :numeric => '600' },
|
236
|
+
{ :alpha2 => 'PE', :name => 'Peru', :alpha3 => 'PER', :numeric => '604' },
|
237
|
+
{ :alpha2 => 'PH', :name => 'Philippines', :alpha3 => 'PHL', :numeric => '608' },
|
238
|
+
{ :alpha2 => 'PN', :name => 'Pitcairn', :alpha3 => 'PCN', :numeric => '612' },
|
239
|
+
{ :alpha2 => 'PL', :name => 'Poland', :alpha3 => 'POL', :numeric => '616' },
|
240
|
+
{ :alpha2 => 'PT', :name => 'Portugal', :alpha3 => 'PRT', :numeric => '620' },
|
241
|
+
{ :alpha2 => 'PR', :name => 'Puerto Rico', :alpha3 => 'PRI', :numeric => '630' },
|
242
|
+
{ :alpha2 => 'QA', :name => 'Qatar', :alpha3 => 'QAT', :numeric => '634' },
|
243
|
+
{ :alpha2 => 'RE', :name => 'Reunion', :alpha3 => 'REU', :numeric => '638' },
|
244
|
+
{ :alpha2 => 'RO', :name => 'Romania', :alpha3 => 'ROM', :numeric => '642' },
|
245
|
+
{ :alpha2 => 'RU', :name => 'Russian Federation', :alpha3 => 'RUS', :numeric => '643' },
|
246
|
+
{ :alpha2 => 'RW', :name => 'Rwanda', :alpha3 => 'RWA', :numeric => '646' },
|
247
|
+
{ :alpha2 => 'BL', :name => 'Saint Barthélemy', :alpha3 => 'BLM', :numeric => '652' },
|
248
|
+
{ :alpha2 => 'SH', :name => 'Saint Helena', :alpha3 => 'SHN', :numeric => '654' },
|
249
|
+
{ :alpha2 => 'KN', :name => 'Saint Kitts and Nevis', :alpha3 => 'KNA', :numeric => '659' },
|
250
|
+
{ :alpha2 => 'LC', :name => 'Saint Lucia', :alpha3 => 'LCA', :numeric => '662' },
|
251
|
+
{ :alpha2 => 'MF', :name => 'Saint Martin (French part)', :alpha3 => 'MAF', :numeric => '663' },
|
252
|
+
{ :alpha2 => 'PM', :name => 'Saint Pierre and Miquelon', :alpha3 => 'SPM', :numeric => '666' },
|
253
|
+
{ :alpha2 => 'VC', :name => 'Saint Vincent and the Grenadines', :alpha3 => 'VCT', :numeric => '670' },
|
254
|
+
{ :alpha2 => 'WS', :name => 'Samoa', :alpha3 => 'WSM', :numeric => '882' },
|
255
|
+
{ :alpha2 => 'SM', :name => 'San Marino', :alpha3 => 'SMR', :numeric => '674' },
|
256
|
+
{ :alpha2 => 'ST', :name => 'Sao Tome and Principe', :alpha3 => 'STP', :numeric => '678' },
|
257
|
+
{ :alpha2 => 'SA', :name => 'Saudi Arabia', :alpha3 => 'SAU', :numeric => '682' },
|
258
|
+
{ :alpha2 => 'SN', :name => 'Senegal', :alpha3 => 'SEN', :numeric => '686' },
|
259
|
+
{ :alpha2 => 'RS', :name => 'Serbia', :alpha3 => 'SRB', :numeric => '688' },
|
260
|
+
{ :alpha2 => 'SC', :name => 'Seychelles', :alpha3 => 'SYC', :numeric => '690' },
|
261
|
+
{ :alpha2 => 'SL', :name => 'Sierra Leone', :alpha3 => 'SLE', :numeric => '694' },
|
262
|
+
{ :alpha2 => 'SG', :name => 'Singapore', :alpha3 => 'SGP', :numeric => '702' },
|
263
|
+
{ :alpha2 => 'SK', :name => 'Slovakia', :alpha3 => 'SVK', :numeric => '703' },
|
264
|
+
{ :alpha2 => 'SI', :name => 'Slovenia', :alpha3 => 'SVN', :numeric => '705' },
|
265
|
+
{ :alpha2 => 'SB', :name => 'Solomon Islands', :alpha3 => 'SLB', :numeric => '090' },
|
266
|
+
{ :alpha2 => 'SO', :name => 'Somalia', :alpha3 => 'SOM', :numeric => '706' },
|
267
|
+
{ :alpha2 => 'ZA', :name => 'South Africa', :alpha3 => 'ZAF', :numeric => '710' },
|
268
|
+
{ :alpha2 => 'GS', :name => 'South Georgia and the South Sandwich Islands', :alpha3 => 'SGS', :numeric => '239' },
|
269
|
+
{ :alpha2 => 'ES', :name => 'Spain', :alpha3 => 'ESP', :numeric => '724' },
|
270
|
+
{ :alpha2 => 'LK', :name => 'Sri Lanka', :alpha3 => 'LKA', :numeric => '144' },
|
271
|
+
{ :alpha2 => 'SD', :name => 'Sudan', :alpha3 => 'SDN', :numeric => '736' },
|
272
|
+
{ :alpha2 => 'SR', :name => 'Suriname', :alpha3 => 'SUR', :numeric => '740' },
|
273
|
+
{ :alpha2 => 'SJ', :name => 'Svalbard and Jan Mayen', :alpha3 => 'SJM', :numeric => '744' },
|
274
|
+
{ :alpha2 => 'SZ', :name => 'Swaziland', :alpha3 => 'SWZ', :numeric => '748' },
|
275
|
+
{ :alpha2 => 'SE', :name => 'Sweden', :alpha3 => 'SWE', :numeric => '752' },
|
276
|
+
{ :alpha2 => 'CH', :name => 'Switzerland', :alpha3 => 'CHE', :numeric => '756' },
|
277
|
+
{ :alpha2 => 'SY', :name => 'Syrian Arab Republic', :alpha3 => 'SYR', :numeric => '760' },
|
278
|
+
{ :alpha2 => 'TW', :name => 'Taiwan, Province of China', :alpha3 => 'TWN', :numeric => '158' },
|
279
|
+
{ :alpha2 => 'TJ', :name => 'Tajikistan', :alpha3 => 'TJK', :numeric => '762' },
|
280
|
+
{ :alpha2 => 'TZ', :name => 'Tanzania, United Republic of', :alpha3 => 'TZA', :numeric => '834' },
|
281
|
+
{ :alpha2 => 'TH', :name => 'Thailand', :alpha3 => 'THA', :numeric => '764' },
|
282
|
+
{ :alpha2 => 'TL', :name => 'Timor Leste', :alpha3 => 'TLS', :numeric => '626' },
|
283
|
+
{ :alpha2 => 'TG', :name => 'Togo', :alpha3 => 'TGO', :numeric => '768' },
|
284
|
+
{ :alpha2 => 'TK', :name => 'Tokelau', :alpha3 => 'TKL', :numeric => '772' },
|
285
|
+
{ :alpha2 => 'TO', :name => 'Tonga', :alpha3 => 'TON', :numeric => '776' },
|
286
|
+
{ :alpha2 => 'TT', :name => 'Trinidad and Tobago', :alpha3 => 'TTO', :numeric => '780' },
|
287
|
+
{ :alpha2 => 'TN', :name => 'Tunisia', :alpha3 => 'TUN', :numeric => '788' },
|
288
|
+
{ :alpha2 => 'TR', :name => 'Turkey', :alpha3 => 'TUR', :numeric => '792' },
|
289
|
+
{ :alpha2 => 'TM', :name => 'Turkmenistan', :alpha3 => 'TKM', :numeric => '795' },
|
290
|
+
{ :alpha2 => 'TC', :name => 'Turks and Caicos Islands', :alpha3 => 'TCA', :numeric => '796' },
|
291
|
+
{ :alpha2 => 'TV', :name => 'Tuvalu', :alpha3 => 'TUV', :numeric => '798' },
|
292
|
+
{ :alpha2 => 'UG', :name => 'Uganda', :alpha3 => 'UGA', :numeric => '800' },
|
293
|
+
{ :alpha2 => 'UA', :name => 'Ukraine', :alpha3 => 'UKR', :numeric => '804' },
|
294
|
+
{ :alpha2 => 'AE', :name => 'United Arab Emirates', :alpha3 => 'ARE', :numeric => '784' },
|
295
|
+
{ :alpha2 => 'GB', :name => 'United Kingdom', :alpha3 => 'GBR', :numeric => '826' },
|
296
|
+
{ :alpha2 => 'US', :name => 'United States', :alpha3 => 'USA', :numeric => '840' },
|
297
|
+
{ :alpha2 => 'UM', :name => 'United States Minor Outlying Islands', :alpha3 => 'UMI', :numeric => '581' },
|
298
|
+
{ :alpha2 => 'UY', :name => 'Uruguay', :alpha3 => 'URY', :numeric => '858' },
|
299
|
+
{ :alpha2 => 'UZ', :name => 'Uzbekistan', :alpha3 => 'UZB', :numeric => '860' },
|
300
|
+
{ :alpha2 => 'VU', :name => 'Vanuatu', :alpha3 => 'VUT', :numeric => '548' },
|
301
|
+
{ :alpha2 => 'VE', :name => 'Venezuela', :alpha3 => 'VEN', :numeric => '862' },
|
302
|
+
{ :alpha2 => 'VN', :name => 'Viet Nam', :alpha3 => 'VNM', :numeric => '704' },
|
303
|
+
{ :alpha2 => 'VG', :name => 'Virgin Islands, British', :alpha3 => 'VGB', :numeric => '092' },
|
304
|
+
{ :alpha2 => 'VI', :name => 'Virgin Islands, U.S.', :alpha3 => 'VIR', :numeric => '850' },
|
305
|
+
{ :alpha2 => 'WF', :name => 'Wallis and Futuna', :alpha3 => 'WLF', :numeric => '876' },
|
306
|
+
{ :alpha2 => 'EH', :name => 'Western Sahara', :alpha3 => 'ESH', :numeric => '732' },
|
307
|
+
{ :alpha2 => 'YE', :name => 'Yemen', :alpha3 => 'YEM', :numeric => '887' },
|
308
|
+
{ :alpha2 => 'ZM', :name => 'Zambia', :alpha3 => 'ZMB', :numeric => '894' },
|
309
|
+
{ :alpha2 => 'ZW', :name => 'Zimbabwe', :alpha3 => 'ZWE', :numeric => '716' },
|
310
|
+
{ :alpha2 => 'AX', :name => 'Åland Islands', :alpha3 => 'ALA', :numeric => '248' }
|
311
|
+
]
|
312
|
+
|
313
|
+
def self.find(name)
|
314
|
+
raise InvalidCountryCodeError, "Cannot lookup country for an empty name" if name.blank?
|
315
|
+
|
316
|
+
case name.length
|
317
|
+
when 2, 3
|
318
|
+
upcase_name = name.upcase
|
319
|
+
country_code = CountryCode.new(name)
|
320
|
+
country = COUNTRIES.detect{|c| c[country_code.format] == upcase_name }
|
321
|
+
else
|
322
|
+
country = COUNTRIES.detect{|c| c[:name] == name }
|
323
|
+
end
|
324
|
+
raise InvalidCountryCodeError, "No country could be found for the country #{name}" if country.nil?
|
325
|
+
Country.new(country.dup)
|
326
|
+
end
|
327
|
+
end
|
328
|
+
end
|