active_utils 2.2.3 → 3.0.0.pre1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/.travis.yml +13 -3
- data/Gemfile +1 -1
- data/Gemfile.activesupport32 +4 -0
- data/Gemfile.activesupport40 +4 -0
- data/Gemfile.activesupport41 +4 -0
- data/Gemfile.activesupport42 +4 -0
- data/active_utils.gemspec +2 -1
- data/lib/active_utils/common/connection.rb +3 -169
- data/lib/active_utils/common/country.rb +3 -329
- data/lib/active_utils/common/currency_code.rb +3 -49
- data/lib/active_utils/common/error.rb +3 -28
- data/lib/active_utils/common/network_connection_retries.rb +3 -72
- data/lib/active_utils/common/post_data.rb +3 -23
- data/lib/active_utils/common/posts_data.rb +3 -76
- data/lib/active_utils/common/requires_parameters.rb +3 -15
- data/lib/active_utils/common/utils.rb +3 -19
- data/lib/active_utils/common/validateable.rb +3 -80
- data/lib/active_utils/common/version.rb +4 -0
- data/lib/active_utils/connection.rb +170 -0
- data/lib/active_utils/country.rb +330 -0
- data/lib/active_utils/currency_code.rb +51 -0
- data/lib/active_utils/error.rb +29 -0
- data/lib/active_utils/network_connection_retries.rb +78 -0
- data/lib/active_utils/post_data.rb +24 -0
- data/lib/active_utils/posts_data.rb +78 -0
- data/lib/active_utils/requires_parameters.rb +16 -0
- data/lib/active_utils/utils.rb +20 -0
- data/lib/active_utils/validateable.rb +81 -0
- data/lib/active_utils/version.rb +1 -1
- data/lib/active_utils.rb +21 -16
- data/test/test_helper.rb +15 -4
- data/test/unit/connection_test.rb +15 -15
- data/test/unit/network_connection_retries_test.rb +62 -11
- data/test/unit/post_data_test.rb +6 -6
- data/test/unit/posts_data_test.rb +4 -4
- data/test/unit/utils_test.rb +1 -1
- data/test/unit/validateable_test.rb +1 -1
- data.tar.gz.sig +0 -0
- metadata +35 -6
- metadata.gz.sig +0 -0
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
#!ruby19
|
|
2
|
+
# encoding: utf-8
|
|
3
|
+
|
|
4
|
+
module ActiveUtils #: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 => 'CW', :name => 'Curaçao', :alpha3 => 'CUW', :numeric => '531' },
|
|
121
|
+
{ :alpha2 => 'CY', :name => 'Cyprus', :alpha3 => 'CYP', :numeric => '196' },
|
|
122
|
+
{ :alpha2 => 'CZ', :name => 'Czech Republic', :alpha3 => 'CZE', :numeric => '203' },
|
|
123
|
+
{ :alpha2 => 'DK', :name => 'Denmark', :alpha3 => 'DNK', :numeric => '208' },
|
|
124
|
+
{ :alpha2 => 'DJ', :name => 'Djibouti', :alpha3 => 'DJI', :numeric => '262' },
|
|
125
|
+
{ :alpha2 => 'DM', :name => 'Dominica', :alpha3 => 'DMA', :numeric => '212' },
|
|
126
|
+
{ :alpha2 => 'DO', :name => 'Dominican Republic', :alpha3 => 'DOM', :numeric => '214' },
|
|
127
|
+
{ :alpha2 => 'EC', :name => 'Ecuador', :alpha3 => 'ECU', :numeric => '218' },
|
|
128
|
+
{ :alpha2 => 'EG', :name => 'Egypt', :alpha3 => 'EGY', :numeric => '818' },
|
|
129
|
+
{ :alpha2 => 'SV', :name => 'El Salvador', :alpha3 => 'SLV', :numeric => '222' },
|
|
130
|
+
{ :alpha2 => 'GQ', :name => 'Equatorial Guinea', :alpha3 => 'GNQ', :numeric => '226' },
|
|
131
|
+
{ :alpha2 => 'ER', :name => 'Eritrea', :alpha3 => 'ERI', :numeric => '232' },
|
|
132
|
+
{ :alpha2 => 'EE', :name => 'Estonia', :alpha3 => 'EST', :numeric => '233' },
|
|
133
|
+
{ :alpha2 => 'ET', :name => 'Ethiopia', :alpha3 => 'ETH', :numeric => '231' },
|
|
134
|
+
{ :alpha2 => 'FK', :name => 'Falkland Islands (Malvinas)', :alpha3 => 'FLK', :numeric => '238' },
|
|
135
|
+
{ :alpha2 => 'FO', :name => 'Faroe Islands', :alpha3 => 'FRO', :numeric => '234' },
|
|
136
|
+
{ :alpha2 => 'FJ', :name => 'Fiji', :alpha3 => 'FJI', :numeric => '242' },
|
|
137
|
+
{ :alpha2 => 'FI', :name => 'Finland', :alpha3 => 'FIN', :numeric => '246' },
|
|
138
|
+
{ :alpha2 => 'FR', :name => 'France', :alpha3 => 'FRA', :numeric => '250' },
|
|
139
|
+
{ :alpha2 => 'GF', :name => 'French Guiana', :alpha3 => 'GUF', :numeric => '254' },
|
|
140
|
+
{ :alpha2 => 'PF', :name => 'French Polynesia', :alpha3 => 'PYF', :numeric => '258' },
|
|
141
|
+
{ :alpha2 => 'TF', :name => 'French Southern Territories', :alpha3 => 'ATF', :numeric => '260' },
|
|
142
|
+
{ :alpha2 => 'GA', :name => 'Gabon', :alpha3 => 'GAB', :numeric => '266' },
|
|
143
|
+
{ :alpha2 => 'GM', :name => 'Gambia', :alpha3 => 'GMB', :numeric => '270' },
|
|
144
|
+
{ :alpha2 => 'GE', :name => 'Georgia', :alpha3 => 'GEO', :numeric => '268' },
|
|
145
|
+
{ :alpha2 => 'DE', :name => 'Germany', :alpha3 => 'DEU', :numeric => '276' },
|
|
146
|
+
{ :alpha2 => 'GH', :name => 'Ghana', :alpha3 => 'GHA', :numeric => '288' },
|
|
147
|
+
{ :alpha2 => 'GI', :name => 'Gibraltar', :alpha3 => 'GIB', :numeric => '292' },
|
|
148
|
+
{ :alpha2 => 'GR', :name => 'Greece', :alpha3 => 'GRC', :numeric => '300' },
|
|
149
|
+
{ :alpha2 => 'GL', :name => 'Greenland', :alpha3 => 'GRL', :numeric => '304' },
|
|
150
|
+
{ :alpha2 => 'GD', :name => 'Grenada', :alpha3 => 'GRD', :numeric => '308' },
|
|
151
|
+
{ :alpha2 => 'GP', :name => 'Guadeloupe', :alpha3 => 'GLP', :numeric => '312' },
|
|
152
|
+
{ :alpha2 => 'GU', :name => 'Guam', :alpha3 => 'GUM', :numeric => '316' },
|
|
153
|
+
{ :alpha2 => 'GT', :name => 'Guatemala', :alpha3 => 'GTM', :numeric => '320' },
|
|
154
|
+
{ :alpha2 => 'GG', :name => 'Guernsey', :alpha3 => 'GGY', :numeric => '831' },
|
|
155
|
+
{ :alpha2 => 'GN', :name => 'Guinea', :alpha3 => 'GIN', :numeric => '324' },
|
|
156
|
+
{ :alpha2 => 'GW', :name => 'Guinea-Bissau', :alpha3 => 'GNB', :numeric => '624' },
|
|
157
|
+
{ :alpha2 => 'GY', :name => 'Guyana', :alpha3 => 'GUY', :numeric => '328' },
|
|
158
|
+
{ :alpha2 => 'HT', :name => 'Haiti', :alpha3 => 'HTI', :numeric => '332' },
|
|
159
|
+
{ :alpha2 => 'HM', :name => 'Heard Island And Mcdonald Islands', :alpha3 => 'HMD', :numeric => '334' },
|
|
160
|
+
{ :alpha2 => 'VA', :name => 'Holy See (Vatican City State)', :alpha3 => 'VAT', :numeric => '336' },
|
|
161
|
+
{ :alpha2 => 'HN', :name => 'Honduras', :alpha3 => 'HND', :numeric => '340' },
|
|
162
|
+
{ :alpha2 => 'HK', :name => 'Hong Kong', :alpha3 => 'HKG', :numeric => '344' },
|
|
163
|
+
{ :alpha2 => 'HU', :name => 'Hungary', :alpha3 => 'HUN', :numeric => '348' },
|
|
164
|
+
{ :alpha2 => 'IS', :name => 'Iceland', :alpha3 => 'ISL', :numeric => '352' },
|
|
165
|
+
{ :alpha2 => 'IN', :name => 'India', :alpha3 => 'IND', :numeric => '356' },
|
|
166
|
+
{ :alpha2 => 'ID', :name => 'Indonesia', :alpha3 => 'IDN', :numeric => '360' },
|
|
167
|
+
{ :alpha2 => 'IR', :name => 'Iran, Islamic Republic of', :alpha3 => 'IRN', :numeric => '364' },
|
|
168
|
+
{ :alpha2 => 'IQ', :name => 'Iraq', :alpha3 => 'IRQ', :numeric => '368' },
|
|
169
|
+
{ :alpha2 => 'IE', :name => 'Ireland', :alpha3 => 'IRL', :numeric => '372' },
|
|
170
|
+
{ :alpha2 => 'IM', :name => 'Isle Of Man', :alpha3 => 'IMN', :numeric => '833' },
|
|
171
|
+
{ :alpha2 => 'IL', :name => 'Israel', :alpha3 => 'ISR', :numeric => '376' },
|
|
172
|
+
{ :alpha2 => 'IT', :name => 'Italy', :alpha3 => 'ITA', :numeric => '380' },
|
|
173
|
+
{ :alpha2 => 'JM', :name => 'Jamaica', :alpha3 => 'JAM', :numeric => '388' },
|
|
174
|
+
{ :alpha2 => 'JP', :name => 'Japan', :alpha3 => 'JPN', :numeric => '392' },
|
|
175
|
+
{ :alpha2 => 'JE', :name => 'Jersey', :alpha3 => 'JEY', :numeric => '832' },
|
|
176
|
+
{ :alpha2 => 'JO', :name => 'Jordan', :alpha3 => 'JOR', :numeric => '400' },
|
|
177
|
+
{ :alpha2 => 'KZ', :name => 'Kazakhstan', :alpha3 => 'KAZ', :numeric => '398' },
|
|
178
|
+
{ :alpha2 => 'KE', :name => 'Kenya', :alpha3 => 'KEN', :numeric => '404' },
|
|
179
|
+
{ :alpha2 => 'KI', :name => 'Kiribati', :alpha3 => 'KIR', :numeric => '296' },
|
|
180
|
+
{ :alpha2 => 'KP', :name => 'Korea, Democratic People\'s Republic of', :alpha3 => 'PRK', :numeric => '408' },
|
|
181
|
+
{ :alpha2 => 'KR', :name => 'Korea, Republic of', :alpha3 => 'KOR', :numeric => '410' },
|
|
182
|
+
{ :alpha2 => 'KV', :name => 'Kosovo', :alpha3 => 'KSV', :numeric => '377' },
|
|
183
|
+
{ :alpha2 => 'KW', :name => 'Kuwait', :alpha3 => 'KWT', :numeric => '414' },
|
|
184
|
+
{ :alpha2 => 'KG', :name => 'Kyrgyzstan', :alpha3 => 'KGZ', :numeric => '417' },
|
|
185
|
+
{ :alpha2 => 'LA', :name => 'Lao People\'s Democratic Republic', :alpha3 => 'LAO', :numeric => '418' },
|
|
186
|
+
{ :alpha2 => 'LV', :name => 'Latvia', :alpha3 => 'LVA', :numeric => '428' },
|
|
187
|
+
{ :alpha2 => 'LB', :name => 'Lebanon', :alpha3 => 'LBN', :numeric => '422' },
|
|
188
|
+
{ :alpha2 => 'LS', :name => 'Lesotho', :alpha3 => 'LSO', :numeric => '426' },
|
|
189
|
+
{ :alpha2 => 'LR', :name => 'Liberia', :alpha3 => 'LBR', :numeric => '430' },
|
|
190
|
+
{ :alpha2 => 'LY', :name => 'Libyan Arab Jamahiriya', :alpha3 => 'LBY', :numeric => '434' },
|
|
191
|
+
{ :alpha2 => 'LI', :name => 'Liechtenstein', :alpha3 => 'LIE', :numeric => '438' },
|
|
192
|
+
{ :alpha2 => 'LT', :name => 'Lithuania', :alpha3 => 'LTU', :numeric => '440' },
|
|
193
|
+
{ :alpha2 => 'LU', :name => 'Luxembourg', :alpha3 => 'LUX', :numeric => '442' },
|
|
194
|
+
{ :alpha2 => 'MO', :name => 'Macao', :alpha3 => 'MAC', :numeric => '446' },
|
|
195
|
+
{ :alpha2 => 'MK', :name => 'Macedonia, the Former Yugoslav Republic of', :alpha3 => 'MKD', :numeric => '807' },
|
|
196
|
+
{ :alpha2 => 'MG', :name => 'Madagascar', :alpha3 => 'MDG', :numeric => '450' },
|
|
197
|
+
{ :alpha2 => 'MW', :name => 'Malawi', :alpha3 => 'MWI', :numeric => '454' },
|
|
198
|
+
{ :alpha2 => 'MY', :name => 'Malaysia', :alpha3 => 'MYS', :numeric => '458' },
|
|
199
|
+
{ :alpha2 => 'MV', :name => 'Maldives', :alpha3 => 'MDV', :numeric => '462' },
|
|
200
|
+
{ :alpha2 => 'ML', :name => 'Mali', :alpha3 => 'MLI', :numeric => '466' },
|
|
201
|
+
{ :alpha2 => 'MT', :name => 'Malta', :alpha3 => 'MLT', :numeric => '470' },
|
|
202
|
+
{ :alpha2 => 'MH', :name => 'Marshall Islands', :alpha3 => 'MHL', :numeric => '584' },
|
|
203
|
+
{ :alpha2 => 'MQ', :name => 'Martinique', :alpha3 => 'MTQ', :numeric => '474' },
|
|
204
|
+
{ :alpha2 => 'MR', :name => 'Mauritania', :alpha3 => 'MRT', :numeric => '478' },
|
|
205
|
+
{ :alpha2 => 'MU', :name => 'Mauritius', :alpha3 => 'MUS', :numeric => '480' },
|
|
206
|
+
{ :alpha2 => 'YT', :name => 'Mayotte', :alpha3 => 'MYT', :numeric => '175' },
|
|
207
|
+
{ :alpha2 => 'MX', :name => 'Mexico', :alpha3 => 'MEX', :numeric => '484' },
|
|
208
|
+
{ :alpha2 => 'FM', :name => 'Micronesia, Federated States of', :alpha3 => 'FSM', :numeric => '583' },
|
|
209
|
+
{ :alpha2 => 'MD', :name => 'Moldova, Republic of', :alpha3 => 'MDA', :numeric => '498' },
|
|
210
|
+
{ :alpha2 => 'MC', :name => 'Monaco', :alpha3 => 'MCO', :numeric => '492' },
|
|
211
|
+
{ :alpha2 => 'MN', :name => 'Mongolia', :alpha3 => 'MNG', :numeric => '496' },
|
|
212
|
+
{ :alpha2 => 'ME', :name => 'Montenegro', :alpha3 => 'MNE', :numeric => '499' },
|
|
213
|
+
{ :alpha2 => 'MS', :name => 'Montserrat', :alpha3 => 'MSR', :numeric => '500' },
|
|
214
|
+
{ :alpha2 => 'MA', :name => 'Morocco', :alpha3 => 'MAR', :numeric => '504' },
|
|
215
|
+
{ :alpha2 => 'MZ', :name => 'Mozambique', :alpha3 => 'MOZ', :numeric => '508' },
|
|
216
|
+
{ :alpha2 => 'MM', :name => 'Myanmar', :alpha3 => 'MMR', :numeric => '104' },
|
|
217
|
+
{ :alpha2 => 'NA', :name => 'Namibia', :alpha3 => 'NAM', :numeric => '516' },
|
|
218
|
+
{ :alpha2 => 'NR', :name => 'Nauru', :alpha3 => 'NRU', :numeric => '520' },
|
|
219
|
+
{ :alpha2 => 'NP', :name => 'Nepal', :alpha3 => 'NPL', :numeric => '524' },
|
|
220
|
+
{ :alpha2 => 'NL', :name => 'Netherlands', :alpha3 => 'NLD', :numeric => '528' },
|
|
221
|
+
{ :alpha2 => 'AN', :name => 'Netherlands Antilles', :alpha3 => 'ANT', :numeric => '530' },
|
|
222
|
+
{ :alpha2 => 'NC', :name => 'New Caledonia', :alpha3 => 'NCL', :numeric => '540' },
|
|
223
|
+
{ :alpha2 => 'NZ', :name => 'New Zealand', :alpha3 => 'NZL', :numeric => '554' },
|
|
224
|
+
{ :alpha2 => 'NI', :name => 'Nicaragua', :alpha3 => 'NIC', :numeric => '558' },
|
|
225
|
+
{ :alpha2 => 'NE', :name => 'Niger', :alpha3 => 'NER', :numeric => '562' },
|
|
226
|
+
{ :alpha2 => 'NG', :name => 'Nigeria', :alpha3 => 'NGA', :numeric => '566' },
|
|
227
|
+
{ :alpha2 => 'NU', :name => 'Niue', :alpha3 => 'NIU', :numeric => '570' },
|
|
228
|
+
{ :alpha2 => 'NF', :name => 'Norfolk Island', :alpha3 => 'NFK', :numeric => '574' },
|
|
229
|
+
{ :alpha2 => 'MP', :name => 'Northern Mariana Islands', :alpha3 => 'MNP', :numeric => '580' },
|
|
230
|
+
{ :alpha2 => 'NO', :name => 'Norway', :alpha3 => 'NOR', :numeric => '578' },
|
|
231
|
+
{ :alpha2 => 'OM', :name => 'Oman', :alpha3 => 'OMN', :numeric => '512' },
|
|
232
|
+
{ :alpha2 => 'PK', :name => 'Pakistan', :alpha3 => 'PAK', :numeric => '586' },
|
|
233
|
+
{ :alpha2 => 'PW', :name => 'Palau', :alpha3 => 'PLW', :numeric => '585' },
|
|
234
|
+
{ :alpha2 => 'PS', :name => 'Palestinian Territory, Occupied', :alpha3 => 'PSE', :numeric => '275' },
|
|
235
|
+
{ :alpha2 => 'PA', :name => 'Panama', :alpha3 => 'PAN', :numeric => '591' },
|
|
236
|
+
{ :alpha2 => 'PG', :name => 'Papua New Guinea', :alpha3 => 'PNG', :numeric => '598' },
|
|
237
|
+
{ :alpha2 => 'PY', :name => 'Paraguay', :alpha3 => 'PRY', :numeric => '600' },
|
|
238
|
+
{ :alpha2 => 'PE', :name => 'Peru', :alpha3 => 'PER', :numeric => '604' },
|
|
239
|
+
{ :alpha2 => 'PH', :name => 'Philippines', :alpha3 => 'PHL', :numeric => '608' },
|
|
240
|
+
{ :alpha2 => 'PN', :name => 'Pitcairn', :alpha3 => 'PCN', :numeric => '612' },
|
|
241
|
+
{ :alpha2 => 'PL', :name => 'Poland', :alpha3 => 'POL', :numeric => '616' },
|
|
242
|
+
{ :alpha2 => 'PT', :name => 'Portugal', :alpha3 => 'PRT', :numeric => '620' },
|
|
243
|
+
{ :alpha2 => 'PR', :name => 'Puerto Rico', :alpha3 => 'PRI', :numeric => '630' },
|
|
244
|
+
{ :alpha2 => 'QA', :name => 'Qatar', :alpha3 => 'QAT', :numeric => '634' },
|
|
245
|
+
{ :alpha2 => 'RE', :name => 'Reunion', :alpha3 => 'REU', :numeric => '638' },
|
|
246
|
+
{ :alpha2 => 'RO', :name => 'Romania', :alpha3 => 'ROM', :numeric => '642' },
|
|
247
|
+
{ :alpha2 => 'RU', :name => 'Russian Federation', :alpha3 => 'RUS', :numeric => '643' },
|
|
248
|
+
{ :alpha2 => 'RW', :name => 'Rwanda', :alpha3 => 'RWA', :numeric => '646' },
|
|
249
|
+
{ :alpha2 => 'BL', :name => 'Saint Barthélemy', :alpha3 => 'BLM', :numeric => '652' },
|
|
250
|
+
{ :alpha2 => 'SH', :name => 'Saint Helena', :alpha3 => 'SHN', :numeric => '654' },
|
|
251
|
+
{ :alpha2 => 'KN', :name => 'Saint Kitts and Nevis', :alpha3 => 'KNA', :numeric => '659' },
|
|
252
|
+
{ :alpha2 => 'LC', :name => 'Saint Lucia', :alpha3 => 'LCA', :numeric => '662' },
|
|
253
|
+
{ :alpha2 => 'MF', :name => 'Saint Martin (French part)', :alpha3 => 'MAF', :numeric => '663' },
|
|
254
|
+
{ :alpha2 => 'PM', :name => 'Saint Pierre and Miquelon', :alpha3 => 'SPM', :numeric => '666' },
|
|
255
|
+
{ :alpha2 => 'VC', :name => 'Saint Vincent and the Grenadines', :alpha3 => 'VCT', :numeric => '670' },
|
|
256
|
+
{ :alpha2 => 'WS', :name => 'Samoa', :alpha3 => 'WSM', :numeric => '882' },
|
|
257
|
+
{ :alpha2 => 'SM', :name => 'San Marino', :alpha3 => 'SMR', :numeric => '674' },
|
|
258
|
+
{ :alpha2 => 'ST', :name => 'Sao Tome and Principe', :alpha3 => 'STP', :numeric => '678' },
|
|
259
|
+
{ :alpha2 => 'SA', :name => 'Saudi Arabia', :alpha3 => 'SAU', :numeric => '682' },
|
|
260
|
+
{ :alpha2 => 'SN', :name => 'Senegal', :alpha3 => 'SEN', :numeric => '686' },
|
|
261
|
+
{ :alpha2 => 'RS', :name => 'Serbia', :alpha3 => 'SRB', :numeric => '688' },
|
|
262
|
+
{ :alpha2 => 'SC', :name => 'Seychelles', :alpha3 => 'SYC', :numeric => '690' },
|
|
263
|
+
{ :alpha2 => 'SL', :name => 'Sierra Leone', :alpha3 => 'SLE', :numeric => '694' },
|
|
264
|
+
{ :alpha2 => 'SG', :name => 'Singapore', :alpha3 => 'SGP', :numeric => '702' },
|
|
265
|
+
{ :alpha2 => 'SK', :name => 'Slovakia', :alpha3 => 'SVK', :numeric => '703' },
|
|
266
|
+
{ :alpha2 => 'SI', :name => 'Slovenia', :alpha3 => 'SVN', :numeric => '705' },
|
|
267
|
+
{ :alpha2 => 'SB', :name => 'Solomon Islands', :alpha3 => 'SLB', :numeric => '090' },
|
|
268
|
+
{ :alpha2 => 'SO', :name => 'Somalia', :alpha3 => 'SOM', :numeric => '706' },
|
|
269
|
+
{ :alpha2 => 'ZA', :name => 'South Africa', :alpha3 => 'ZAF', :numeric => '710' },
|
|
270
|
+
{ :alpha2 => 'GS', :name => 'South Georgia and the South Sandwich Islands', :alpha3 => 'SGS', :numeric => '239' },
|
|
271
|
+
{ :alpha2 => 'ES', :name => 'Spain', :alpha3 => 'ESP', :numeric => '724' },
|
|
272
|
+
{ :alpha2 => 'LK', :name => 'Sri Lanka', :alpha3 => 'LKA', :numeric => '144' },
|
|
273
|
+
{ :alpha2 => 'SD', :name => 'Sudan', :alpha3 => 'SDN', :numeric => '736' },
|
|
274
|
+
{ :alpha2 => 'SR', :name => 'Suriname', :alpha3 => 'SUR', :numeric => '740' },
|
|
275
|
+
{ :alpha2 => 'SJ', :name => 'Svalbard and Jan Mayen', :alpha3 => 'SJM', :numeric => '744' },
|
|
276
|
+
{ :alpha2 => 'SZ', :name => 'Swaziland', :alpha3 => 'SWZ', :numeric => '748' },
|
|
277
|
+
{ :alpha2 => 'SE', :name => 'Sweden', :alpha3 => 'SWE', :numeric => '752' },
|
|
278
|
+
{ :alpha2 => 'CH', :name => 'Switzerland', :alpha3 => 'CHE', :numeric => '756' },
|
|
279
|
+
{ :alpha2 => 'SY', :name => 'Syrian Arab Republic', :alpha3 => 'SYR', :numeric => '760' },
|
|
280
|
+
{ :alpha2 => 'TW', :name => 'Taiwan, Province of China', :alpha3 => 'TWN', :numeric => '158' },
|
|
281
|
+
{ :alpha2 => 'TJ', :name => 'Tajikistan', :alpha3 => 'TJK', :numeric => '762' },
|
|
282
|
+
{ :alpha2 => 'TZ', :name => 'Tanzania, United Republic of', :alpha3 => 'TZA', :numeric => '834' },
|
|
283
|
+
{ :alpha2 => 'TH', :name => 'Thailand', :alpha3 => 'THA', :numeric => '764' },
|
|
284
|
+
{ :alpha2 => 'TL', :name => 'Timor Leste', :alpha3 => 'TLS', :numeric => '626' },
|
|
285
|
+
{ :alpha2 => 'TG', :name => 'Togo', :alpha3 => 'TGO', :numeric => '768' },
|
|
286
|
+
{ :alpha2 => 'TK', :name => 'Tokelau', :alpha3 => 'TKL', :numeric => '772' },
|
|
287
|
+
{ :alpha2 => 'TO', :name => 'Tonga', :alpha3 => 'TON', :numeric => '776' },
|
|
288
|
+
{ :alpha2 => 'TT', :name => 'Trinidad and Tobago', :alpha3 => 'TTO', :numeric => '780' },
|
|
289
|
+
{ :alpha2 => 'TN', :name => 'Tunisia', :alpha3 => 'TUN', :numeric => '788' },
|
|
290
|
+
{ :alpha2 => 'TR', :name => 'Turkey', :alpha3 => 'TUR', :numeric => '792' },
|
|
291
|
+
{ :alpha2 => 'TM', :name => 'Turkmenistan', :alpha3 => 'TKM', :numeric => '795' },
|
|
292
|
+
{ :alpha2 => 'TC', :name => 'Turks and Caicos Islands', :alpha3 => 'TCA', :numeric => '796' },
|
|
293
|
+
{ :alpha2 => 'TV', :name => 'Tuvalu', :alpha3 => 'TUV', :numeric => '798' },
|
|
294
|
+
{ :alpha2 => 'UG', :name => 'Uganda', :alpha3 => 'UGA', :numeric => '800' },
|
|
295
|
+
{ :alpha2 => 'UA', :name => 'Ukraine', :alpha3 => 'UKR', :numeric => '804' },
|
|
296
|
+
{ :alpha2 => 'AE', :name => 'United Arab Emirates', :alpha3 => 'ARE', :numeric => '784' },
|
|
297
|
+
{ :alpha2 => 'GB', :name => 'United Kingdom', :alpha3 => 'GBR', :numeric => '826' },
|
|
298
|
+
{ :alpha2 => 'US', :name => 'United States', :alpha3 => 'USA', :numeric => '840' },
|
|
299
|
+
{ :alpha2 => 'UM', :name => 'United States Minor Outlying Islands', :alpha3 => 'UMI', :numeric => '581' },
|
|
300
|
+
{ :alpha2 => 'UY', :name => 'Uruguay', :alpha3 => 'URY', :numeric => '858' },
|
|
301
|
+
{ :alpha2 => 'UZ', :name => 'Uzbekistan', :alpha3 => 'UZB', :numeric => '860' },
|
|
302
|
+
{ :alpha2 => 'VU', :name => 'Vanuatu', :alpha3 => 'VUT', :numeric => '548' },
|
|
303
|
+
{ :alpha2 => 'VE', :name => 'Venezuela', :alpha3 => 'VEN', :numeric => '862' },
|
|
304
|
+
{ :alpha2 => 'VN', :name => 'Viet Nam', :alpha3 => 'VNM', :numeric => '704' },
|
|
305
|
+
{ :alpha2 => 'VG', :name => 'Virgin Islands, British', :alpha3 => 'VGB', :numeric => '092' },
|
|
306
|
+
{ :alpha2 => 'VI', :name => 'Virgin Islands, U.S.', :alpha3 => 'VIR', :numeric => '850' },
|
|
307
|
+
{ :alpha2 => 'WF', :name => 'Wallis and Futuna', :alpha3 => 'WLF', :numeric => '876' },
|
|
308
|
+
{ :alpha2 => 'EH', :name => 'Western Sahara', :alpha3 => 'ESH', :numeric => '732' },
|
|
309
|
+
{ :alpha2 => 'YE', :name => 'Yemen', :alpha3 => 'YEM', :numeric => '887' },
|
|
310
|
+
{ :alpha2 => 'ZM', :name => 'Zambia', :alpha3 => 'ZMB', :numeric => '894' },
|
|
311
|
+
{ :alpha2 => 'ZW', :name => 'Zimbabwe', :alpha3 => 'ZWE', :numeric => '716' },
|
|
312
|
+
{ :alpha2 => 'AX', :name => 'Åland Islands', :alpha3 => 'ALA', :numeric => '248' }
|
|
313
|
+
]
|
|
314
|
+
|
|
315
|
+
def self.find(name)
|
|
316
|
+
raise InvalidCountryCodeError, "Cannot lookup country for an empty name" if name.blank?
|
|
317
|
+
|
|
318
|
+
case name.length
|
|
319
|
+
when 2, 3
|
|
320
|
+
upcase_name = name.upcase
|
|
321
|
+
country_code = CountryCode.new(name)
|
|
322
|
+
country = COUNTRIES.detect{|c| c[country_code.format] == upcase_name }
|
|
323
|
+
else
|
|
324
|
+
country = COUNTRIES.detect{|c| c[:name] == name }
|
|
325
|
+
end
|
|
326
|
+
raise InvalidCountryCodeError, "No country could be found for the country #{name}" if country.nil?
|
|
327
|
+
Country.new(country.dup)
|
|
328
|
+
end
|
|
329
|
+
end
|
|
330
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
module ActiveUtils
|
|
2
|
+
class InvalidCurrencyCodeError < StandardError
|
|
3
|
+
end
|
|
4
|
+
|
|
5
|
+
class CurrencyCode
|
|
6
|
+
ISO_CURRENCIES = [
|
|
7
|
+
"AED", "ALL", "AMD", "ANG", "AOA", "ARS", "AUD", "AWG", "AZN", "BAM", "BBD", "BDT", "BGN", "BHD",
|
|
8
|
+
"BND", "BOB", "BRL", "BSD", "BTN", "BWP", "BYR", "BZD", "CAD", "CHF", "CLP", "CNY", "COP", "CRC",
|
|
9
|
+
"CZK", "DKK", "DOP", "DZD", "EGP", "ETB", "EUR", "FJD", "GBP", "GEL", "GHS", "GMD", "GTQ", "GYD",
|
|
10
|
+
"HKD", "HNL", "HRK", "HUF", "IDR", "ILS", "INR", "ISK", "JEP", "JMD", "JOD", "JPY", "KES", "KGS",
|
|
11
|
+
"KHR", "KRW", "KWD", "KYD", "KZT", "LBP", "LKR", "LTL", "LVL", "MAD", "MDL", "MGA", "MKD", "MMK",
|
|
12
|
+
"MNT", "MOP", "MUR", "MVR", "MXN", "MYR", "MZN", "NAD", "NGN", "NIO", "NOK", "NPR", "NZD", "OMR",
|
|
13
|
+
"PEN", "PGK", "PHP", "PKR", "PLN", "PYG", "QAR", "RON", "RSD", "RUB", "RWF", "SAR", "SCR", "SEK",
|
|
14
|
+
"SGD", "STD", "SYP", "THB", "TND", "TRY", "TTD", "TWD", "TZS", "UAH", "UGX", "USD", "UYU", "VEF",
|
|
15
|
+
"VND", "VUV", "WST", "XAF", "XCD", "XOF", "XPF", "ZAR", "ZMW"
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
NON_ISO_TO_ISO = {
|
|
19
|
+
"ARN" => "ARS", # Argentinian Pesos
|
|
20
|
+
"AZM" => "AZN", # Azerbaijan New Manat
|
|
21
|
+
"CHP" => "CLP", # Chilean Pesos
|
|
22
|
+
"DHS" => "AED", # UAE Dirhams
|
|
23
|
+
"ECD" => "XCD", # East Caribbean Dollars
|
|
24
|
+
"GHC" => "GHS", # Ghana Cedi
|
|
25
|
+
"JAD" => "JMD", # Jamaican Dollars
|
|
26
|
+
"JYE" => "JPY", # Japanese Yen
|
|
27
|
+
"KUD" => "KWD", # Kuwaiti Dinars
|
|
28
|
+
"MZM" => "MZN", # Mozambique Metical
|
|
29
|
+
"NMP" => "MXN", # Mexican Pesos
|
|
30
|
+
"NTD" => "TWD", # New Taiwan Dollars / Taiwan New Dollars
|
|
31
|
+
"RDD" => "DOP", # Dominican Pesos
|
|
32
|
+
"RMB" => "CNY", # Chinese Renminbi
|
|
33
|
+
"SFR" => "CHF", # Swiss Francs
|
|
34
|
+
"SID" => "SGD", # Singapore Dollars
|
|
35
|
+
"SOL" => "PES", # Peruvian Sol
|
|
36
|
+
"UKL" => "GBP", # Great Britain Pounds
|
|
37
|
+
"WON" => "KRW" # South Korean Won
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
def self.standardize(code)
|
|
41
|
+
code = code.upcase unless code.nil?
|
|
42
|
+
|
|
43
|
+
return code if is_iso?(code)
|
|
44
|
+
NON_ISO_TO_ISO[code] || raise(InvalidCurrencyCodeError, "#{code} is not an ISO currency, nor can it be converted to one.")
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def self.is_iso?(code)
|
|
48
|
+
ISO_CURRENCIES.include? code
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
module ActiveUtils #:nodoc:
|
|
2
|
+
class ActiveUtilsError < StandardError #:nodoc:
|
|
3
|
+
end
|
|
4
|
+
|
|
5
|
+
class ConnectionError < ActiveUtilsError # :nodoc:
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
class RetriableConnectionError < ConnectionError # :nodoc:
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
class ResponseError < ActiveUtilsError # :nodoc:
|
|
12
|
+
attr_reader :response
|
|
13
|
+
|
|
14
|
+
def initialize(response, message = nil)
|
|
15
|
+
@response = response
|
|
16
|
+
@message = message
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def to_s
|
|
20
|
+
"Failed with #{response.code} #{response.message if response.respond_to?(:message)}"
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
class ClientCertificateError < ActiveUtilsError # :nodoc
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
class InvalidResponseError < ActiveUtilsError # :nodoc
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
module ActiveUtils
|
|
2
|
+
module NetworkConnectionRetries
|
|
3
|
+
DEFAULT_RETRIES = 3
|
|
4
|
+
DEFAULT_CONNECTION_ERRORS = {
|
|
5
|
+
EOFError => "The remote server dropped the connection",
|
|
6
|
+
Errno::ECONNRESET => "The remote server reset the connection",
|
|
7
|
+
Timeout::Error => "The connection to the remote server timed out",
|
|
8
|
+
Errno::ETIMEDOUT => "The connection to the remote server timed out",
|
|
9
|
+
SocketError => "The connection to the remote server could not be established",
|
|
10
|
+
OpenSSL::SSL::SSLError => "The SSL connection to the remote server could not be established"
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
def self.included(base)
|
|
14
|
+
base.send(:attr_accessor, :retry_safe)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def retry_exceptions(options={})
|
|
18
|
+
connection_errors = DEFAULT_CONNECTION_ERRORS.merge(options[:connection_exceptions] || {})
|
|
19
|
+
|
|
20
|
+
retry_network_exceptions(options) do
|
|
21
|
+
begin
|
|
22
|
+
yield
|
|
23
|
+
rescue Errno::ECONNREFUSED => e
|
|
24
|
+
raise ActiveUtils::RetriableConnectionError, "The remote server refused the connection"
|
|
25
|
+
rescue OpenSSL::X509::CertificateError => e
|
|
26
|
+
NetworkConnectionRetries.log(options[:logger], :error, e.message, options[:tag])
|
|
27
|
+
raise ActiveUtils::ClientCertificateError, "The remote server did not accept the provided SSL certificate"
|
|
28
|
+
rescue Zlib::BufError => e
|
|
29
|
+
raise ActiveUtils::InvalidResponseError, "The remote server replied with an invalid response"
|
|
30
|
+
rescue *connection_errors.keys => e
|
|
31
|
+
raise ActiveUtils::ConnectionError, derived_error_message(connection_errors, e.class)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
|
|
38
|
+
def retry_network_exceptions(options = {})
|
|
39
|
+
initial_retries = options[:max_retries] || DEFAULT_RETRIES
|
|
40
|
+
retries = initial_retries
|
|
41
|
+
request_start = nil
|
|
42
|
+
|
|
43
|
+
begin
|
|
44
|
+
request_start = Time.now.to_f
|
|
45
|
+
result = yield
|
|
46
|
+
log_with_retry_details(options[:logger], initial_retries-retries + 1, Time.now.to_f - request_start, "success", options[:tag])
|
|
47
|
+
result
|
|
48
|
+
rescue ActiveUtils::RetriableConnectionError => e
|
|
49
|
+
retries -= 1
|
|
50
|
+
|
|
51
|
+
log_with_retry_details(options[:logger], initial_retries-retries, Time.now.to_f - request_start, e.message, options[:tag])
|
|
52
|
+
retry unless retries.zero?
|
|
53
|
+
raise ActiveUtils::ConnectionError, e.message
|
|
54
|
+
rescue ActiveUtils::ConnectionError, ActiveUtils::InvalidResponseError => e
|
|
55
|
+
retries -= 1
|
|
56
|
+
log_with_retry_details(options[:logger], initial_retries-retries, Time.now.to_f - request_start, e.message, options[:tag])
|
|
57
|
+
retry if (options[:retry_safe] || retry_safe) && !retries.zero?
|
|
58
|
+
raise
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def self.log(logger, level, message, tag=nil)
|
|
63
|
+
tag ||= self.class.to_s
|
|
64
|
+
message = "[#{tag}] #{message}"
|
|
65
|
+
logger.send(level, message) if logger
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
private
|
|
69
|
+
def log_with_retry_details(logger, attempts, time, message, tag)
|
|
70
|
+
NetworkConnectionRetries.log(logger, :info, "connection_attempt=%d connection_request_time=%.4fs connection_msg=\"%s\"" % [attempts, time, message], tag)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def derived_error_message(errors, klass)
|
|
74
|
+
key = (errors.keys & klass.ancestors).first
|
|
75
|
+
key ? errors[key] : nil
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
require 'cgi'
|
|
2
|
+
|
|
3
|
+
module ActiveUtils
|
|
4
|
+
class PostData < Hash
|
|
5
|
+
class_attribute :required_fields, :instance_writer => false
|
|
6
|
+
self.required_fields = []
|
|
7
|
+
|
|
8
|
+
def []=(key, value)
|
|
9
|
+
return if value.blank? && !required?(key)
|
|
10
|
+
super
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def to_post_data
|
|
14
|
+
collect { |key, value| "#{key}=#{CGI.escape(value.to_s)}" }.join("&")
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
alias_method :to_s, :to_post_data
|
|
18
|
+
|
|
19
|
+
private
|
|
20
|
+
def required?(key)
|
|
21
|
+
required_fields.include?(key)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
module ActiveUtils #:nodoc:
|
|
2
|
+
module PostsData #:nodoc:
|
|
3
|
+
|
|
4
|
+
def self.included(base)
|
|
5
|
+
base.class_attribute :ssl_strict
|
|
6
|
+
base.ssl_strict = true
|
|
7
|
+
|
|
8
|
+
base.class_attribute :ssl_version
|
|
9
|
+
base.ssl_version = nil
|
|
10
|
+
|
|
11
|
+
base.class_attribute :retry_safe
|
|
12
|
+
base.retry_safe = false
|
|
13
|
+
|
|
14
|
+
base.class_attribute :open_timeout
|
|
15
|
+
base.open_timeout = 60
|
|
16
|
+
|
|
17
|
+
base.class_attribute :read_timeout
|
|
18
|
+
base.read_timeout = 60
|
|
19
|
+
|
|
20
|
+
base.class_attribute :max_retries
|
|
21
|
+
base.max_retries = Connection::MAX_RETRIES
|
|
22
|
+
|
|
23
|
+
base.class_attribute :logger
|
|
24
|
+
base.class_attribute :wiredump_device
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def ssl_get(endpoint, headers={})
|
|
28
|
+
ssl_request(:get, endpoint, nil, headers)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def ssl_post(endpoint, data, headers = {})
|
|
32
|
+
ssl_request(:post, endpoint, data, headers)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def ssl_request(method, endpoint, data, headers)
|
|
36
|
+
handle_response(raw_ssl_request(method, endpoint, data, headers))
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def raw_ssl_request(method, endpoint, data, headers = {})
|
|
40
|
+
logger.warn "#{self.class} using ssl_strict=false, which is insecure" if logger unless ssl_strict
|
|
41
|
+
logger.warn "#{self.class} posting to plaintext endpoint, which is insecure" if logger unless endpoint =~ /^https:/
|
|
42
|
+
|
|
43
|
+
connection = new_connection(endpoint)
|
|
44
|
+
connection.open_timeout = open_timeout
|
|
45
|
+
connection.read_timeout = read_timeout
|
|
46
|
+
connection.retry_safe = retry_safe
|
|
47
|
+
connection.verify_peer = ssl_strict
|
|
48
|
+
connection.ssl_version = ssl_version
|
|
49
|
+
connection.logger = logger
|
|
50
|
+
connection.max_retries = max_retries
|
|
51
|
+
connection.tag = self.class.name
|
|
52
|
+
connection.wiredump_device = wiredump_device
|
|
53
|
+
|
|
54
|
+
connection.pem = @options[:pem] if @options
|
|
55
|
+
connection.pem_password = @options[:pem_password] if @options
|
|
56
|
+
|
|
57
|
+
connection.ignore_http_status = @options[:ignore_http_status] if @options
|
|
58
|
+
|
|
59
|
+
connection.request(method, data, headers)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
private
|
|
63
|
+
|
|
64
|
+
def new_connection(endpoint)
|
|
65
|
+
Connection.new(endpoint)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def handle_response(response)
|
|
69
|
+
case response.code.to_i
|
|
70
|
+
when 200...300
|
|
71
|
+
response.body
|
|
72
|
+
else
|
|
73
|
+
raise ResponseError.new(response)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
end
|
|
78
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
module ActiveUtils #:nodoc:
|
|
2
|
+
module RequiresParameters #:nodoc:
|
|
3
|
+
def requires!(hash, *params)
|
|
4
|
+
params.each do |param|
|
|
5
|
+
if param.is_a?(Array)
|
|
6
|
+
raise ArgumentError.new("Missing required parameter: #{param.first}") unless hash.has_key?(param.first)
|
|
7
|
+
|
|
8
|
+
valid_options = param[1..-1]
|
|
9
|
+
raise ArgumentError.new("Parameter: #{param.first} must be one of #{valid_options.to_sentence(:words_connector => 'or')}") unless valid_options.include?(hash[param.first])
|
|
10
|
+
else
|
|
11
|
+
raise ArgumentError.new("Missing required parameter: #{param}") unless hash.has_key?(param)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
require 'securerandom'
|
|
2
|
+
|
|
3
|
+
module ActiveUtils #:nodoc:
|
|
4
|
+
module Utils #:nodoc:
|
|
5
|
+
def generate_unique_id
|
|
6
|
+
SecureRandom.hex(16)
|
|
7
|
+
end
|
|
8
|
+
module_function :generate_unique_id
|
|
9
|
+
|
|
10
|
+
def deprecated(message)
|
|
11
|
+
warning = Kernel.caller[1] + message
|
|
12
|
+
if respond_to?(:logger) && logger.present?
|
|
13
|
+
logger.warn(warning)
|
|
14
|
+
else
|
|
15
|
+
warn(warning)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
module_function :deprecated
|
|
19
|
+
end
|
|
20
|
+
end
|