itu_codes 0.2 → 0.3

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.
data/lib/itu_codes.rb CHANGED
@@ -2,472 +2,528 @@
2
2
  # 1 --> Non-US need to be separated by area code
3
3
  # 7 --> Kazakhstan!
4
4
  require 'enumerator'
5
+ require 'carmen'
5
6
 
6
- class ItuCodes < Object
7
- VERSION = '0.2'
7
+ # TODO: add ability to search for common names (i.e. 'USA' or 'United States of America' for 'United States')
8
8
 
9
- @@CODE_LIST = {
10
- "0" => "Reserved",
11
- "1" => {
12
- "1" => "United States of America",
13
- "1684" => "American Samoa",
14
- "1264" => "Anguilla",
15
- "1268" => "Antigua and Barbuda",
16
- "1242" => "Bahamas (Commonwealth of the)",
17
- "1246" => "Barbados",
18
- "1441" => "Bermuda",
19
- "1284" => "British Virgin Islands",
20
- "1204" => "Canada", # from http://www.cnac.ca/co_codes/co_code_status_map.htm
21
- "1226" => "Canada",
22
- "1250" => "Canada",
23
- "1289" => "Canada",
24
- "1306" => "Canada",
25
- "1343" => "Canada",
26
- "1403" => "Canada",
27
- "1416" => "Canada",
28
- "1418" => "Canada",
29
- "1438" => "Canada",
30
- "1450" => "Canada",
31
- "1506" => "Canada",
32
- "1514" => "Canada",
33
- "1519" => "Canada",
34
- "1581" => "Canada",
35
- "1587" => "Canada",
36
- "1604" => "Canada",
37
- "1613" => "Canada",
38
- "1647" => "Canada",
39
- "1705" => "Canada",
40
- "1709" => "Canada",
41
- "1778" => "Canada",
42
- "1780" => "Canada",
43
- "1807" => "Canada",
44
- "1819" => "Canada",
45
- "1867" => "Canada",
46
- "1902" => "Canada",
47
- "1905" => "Canada",
48
- "1345" => "Cayman Islands",
49
- "1767" => "Dominica (Commonwealth of)",
50
- "1809" => "Dominican Republic",
51
- "1829" => "Dominican Republic",
52
- "1473" => "Grenada",
53
- "1671" => "Guam",
54
- "1876" => "Jamaica",
55
- "1664" => "Montserrat",
56
- "1670" => "Northern Mariana Islands (Commonwealth of the)",
57
- "1869" => "Saint Kitts and Nevis",
58
- "1758" => "Saint Lucia",
59
- "1784" => "Saint Vincent and the Grenadines",
60
- "1868" => "Trinidad and Tobago",
61
- "1649" => "Turks and Caicos Islands",
62
- "1340" => "United States Virgin Islands"
63
- },
64
- "20" => "Egypt (Arab Republic of)",
65
- "210" => "Spare code",
66
- "211" => "Spare code",
67
- "212" => "Morocco (Kingdom of)",
68
- "213" => "Algeria (People's Democratic Republic of)",
69
- "214" => "Spare code",
70
- "215" => "Spare code",
71
- "216" => "Tunisia",
72
- "217" => "Spare code",
73
- "218" => "Libya (Socialist People's Libyan Arab Jamahiriya)",
74
- "219" => "Spare code",
75
- "220" => "Gambia (Republic of the)",
76
- "221" => "Senegal (Republic of)",
77
- "222" => "Mauritania (Islamic Republic of)",
78
- "223" => "Mali (Republic of)",
79
- "224" => "Guinea (Republic of)",
80
- "225" => "Côte d'Ivoire (Republic of)",
81
- "226" => "Burkina Faso",
82
- "227" => "Niger (Republic of the)",
83
- "228" => "Togolese Republic",
84
- "229" => "Benin (Republic of)",
85
- "230" => "Mauritius (Republic of)",
86
- "231" => "Liberia (Republic of)",
87
- "232" => "Sierra Leone",
88
- "233" => "Ghana",
89
- "234" => "Nigeria (Federal Republic of)",
90
- "235" => "Chad (Republic of)",
91
- "236" => "Central African Republic",
92
- "237" => "Cameroon (Republic of)",
93
- "238" => "Cape Verde (Republic of)",
94
- "239" => "Sao Tome and Principe (Democratic Republic of)",
95
- "240" => "Equatorial Guinea (Republic of)",
96
- "241" => "Gabonese Republic",
97
- "242" => "Congo (Republic of the)",
98
- "243" => "Democratic Republic of the Congo",
99
- "244" => "Angola (Republic of)",
100
- "245" => "Guinea-Bissau (Republic of)",
101
- "246" => "Diego Garcia",
102
- "247" => "Ascension",
103
- "248" => "Seychelles (Republic of)",
104
- "249" => "Sudan (Republic of the)",
105
- "250" => "Rwanda (Republic of)",
106
- "251" => "Ethiopia (Federal Democratic Republic of)",
107
- "252" => "Somali Democratic Republic",
108
- "253" => "Djibouti (Republic of)",
109
- "254" => "Kenya (Republic of)",
110
- "255" => "Tanzania (United Republic of)",
111
- "256" => "Uganda (Republic of)",
112
- "257" => "Burundi (Republic of)",
113
- "258" => "Mozambique (Republic of)",
114
- "259" => "Spare code",
115
- "260" => "Zambia (Republic of)",
116
- "261" => "Madagascar (Republic of)",
117
- "262" => "Reunion (French Department of)",
118
- "263" => "Zimbabwe (Republic of)",
119
- "264" => "Namibia (Republic of)",
120
- "265" => "Malawi",
121
- "266" => "Lesotho (Kingdom of)",
122
- "267" => "Botswana (Republic of)",
123
- "268" => "Swaziland (Kingdom of)",
124
- "269" => "Comoros (Union of the)",
125
- "269" => "Mayotte",
126
- "27" => "South Africa (Republic of)",
127
- "280" => "Spare code",
128
- "281" => "Spare code",
129
- "282" => "Spare code",
130
- "283" => "Spare code",
131
- "284" => "Spare code",
132
- "285" => "Spare code",
133
- "286" => "Spare code",
134
- "287" => "Spare code",
135
- "288" => "Spare code",
136
- "289" => "Spare code",
137
- "290" => "Saint Helena",
138
- "291" => "Eritrea",
139
- "292" => "Spare code",
140
- "293" => "Spare code",
141
- "294" => "Spare code",
142
- "295" => "Spare code",
143
- "296" => "Spare code",
144
- "297" => "Aruba",
145
- "298" => "Faroe Islands",
146
- "299" => "Greenland (Denmark)",
147
- "30" => "Greece",
148
- "31" => "Netherlands (Kingdom of the)",
149
- "32" => "Belgium",
150
- "33" => "France",
151
- "34" => "Spain",
152
- "350" => "Gibraltar",
153
- "351" => "Portugal",
154
- "352" => "Luxembourg",
155
- "353" => "Ireland",
156
- "354" => "Iceland",
157
- "355" => "Albania (Republic of)",
158
- "356" => "Malta",
159
- "357" => "Cyprus (Republic of)",
160
- "358" => "Finland",
161
- "359" => "Bulgaria (Republic of)",
162
- "36" => "Hungary (Republic of)",
163
- "370" => "Lithuania (Republic of)",
164
- "371" => "Latvia (Republic of)",
165
- "372" => "Estonia (Republic of)",
166
- "373" => "Moldova (Republic of)",
167
- "374" => "Armenia (Republic of)",
168
- "375" => "Belarus (Republic of)",
169
- "376" => "Andorra (Principality of)",
170
- "377" => "Monaco (Principality of)",
171
- "378" => "San Marino (Republic of)",
172
- "379" => "Vatican City State",
173
- "380" => "Ukraine",
174
- "381" => "Serbia and Montenegro",
175
- "382" => "Spare code",
176
- "383" => "Spare code",
177
- "384" => "Spare code",
178
- "385" => "Croatia (Republic of)",
179
- "386" => "Slovenia (Republic of)",
180
- "387" => "Bosnia and Herzegovina",
181
- "388" => "Group of countries, shared code",
182
- "389" => "The Former Yugoslav Republic of Macedonia",
183
- "39" => "Italy",
184
- "39" => "Vatican City State",
185
- "40" => "Romania",
186
- "41" => "Switzerland (Confederation of)",
187
- "420" => "Czech Republic",
188
- "421" => "Slovak Republic",
189
- "422" => "Spare code",
190
- "423" => "Liechtenstein (Principality of)",
191
- "424" => "Spare code",
192
- "425" => "Spare code",
193
- "426" => "Spare code",
194
- "427" => "Spare code",
195
- "428" => "Spare code",
196
- "429" => "Spare code",
197
- "43" => "Austria",
198
- "44" => "United Kingdom of Great Britain and Northern Ireland",
199
- "45" => "Denmark",
200
- "46" => "Sweden",
201
- "47" => "Norway",
202
- "48" => "Poland (Republic of)",
203
- "49" => "Germany (Federal Republic of)",
204
- "500" => "Falkland Islands (Malvinas)",
205
- "501" => "Belize",
206
- "502" => "Guatemala (Republic of)",
207
- "503" => "El Salvador (Republic of)",
208
- "504" => "Honduras (Republic of)",
209
- "505" => "Nicaragua",
210
- "506" => "Costa Rica",
211
- "507" => "Panama (Republic of)",
212
- "508" => "Saint Pierre and Miquelon (Collectivité territoriale de la République française)",
213
- "509" => "Haiti (Republic of)",
214
- "51" => "Peru",
215
- "52" => "Mexico",
216
- "53" => "Cuba",
217
- "54" => "Argentine Republic",
218
- "55" => "Brazil (Federative Republic of)",
219
- "56" => "Chile",
220
- "57" => "Colombia (Republic of)",
221
- "58" => "Venezuela (Bolivarian Republic of)",
222
- "590" => "Guadeloupe (French Department of)",
223
- "591" => "Bolivia (Republic of)",
224
- "592" => "Guyana",
225
- "593" => "Ecuador",
226
- "594" => "French Guiana (French Department of)",
227
- "595" => "Paraguay (Republic of)",
228
- "596" => "Martinique (French Department of)",
229
- "597" => "Suriname (Republic of)",
230
- "598" => "Uruguay (Eastern Republic of)",
231
- "599" => "Netherlands Antilles",
232
- "60" => "Malaysia",
233
- "61" => "Australia",
234
- "62" => "Indonesia (Republic of)",
235
- "63" => "Philippines (Republic of the)",
236
- "64" => "New Zealand",
237
- "65" => "Singapore (Republic of)",
238
- "66" => "Thailand",
239
- "670" => "Democratic Republic of Timor-Leste",
240
- "671" => "Spare code",
241
- "672" => "Australian External Territories",
242
- "673" => "Brunei Darussalam",
243
- "674" => "Nauru (Republic of)",
244
- "675" => "Papua New Guinea",
245
- "676" => "Tonga (Kingdom of)",
246
- "677" => "Solomon Islands",
247
- "678" => "Vanuatu (Republic of)",
248
- "679" => "Fiji (Republic of)",
249
- "680" => "Palau (Republic of)",
250
- "681" => "Wallis and Futuna (Territoire français d'outre-mer)",
251
- "682" => "Cook Islands",
252
- "683" => "Niue",
253
- "684" => "Spare code",
254
- "685" => "Samoa (Independent State of)",
255
- "686" => "Kiribati (Republic of)",
256
- "687" => "New Caledonia (Territoire français d'outre-mer)",
257
- "688" => "Tuvalu",
258
- "689" => "French Polynesia (Territoire français d'outre-mer)",
259
- "690" => "Tokelau",
260
- "691" => "Micronesia (Federated States of)",
261
- "692" => "Marshall Islands (Republic of the)",
262
- "693" => "Spare code",
263
- "694" => "Spare code",
264
- "695" => "Spare code",
265
- "696" => "Spare code",
266
- "697" => "Spare code",
267
- "698" => "Spare code",
268
- "699" => "Spare code",
269
- "7" => [
270
- "Kazakhstan (Republic of)",
271
- "Russian Federation"
272
- ],
273
- "800" => "International Freephone Service",
274
- "801" => "Spare code",
275
- "802" => "Spare code",
276
- "803" => "Spare code",
277
- "804" => "Spare code",
278
- "805" => "Spare code",
279
- "806" => "Spare code",
280
- "807" => "Spare code",
281
- "808" => "International Shared Cost Service (ISCS)",
282
- "809" => "Spare code",
283
- "81" => "Japan",
284
- "82" => "Korea (Republic of)",
285
- "830" => "Spare code",
286
- "831" => "Spare code",
287
- "832" => "Spare code",
288
- "833" => "Spare code",
289
- "834" => "Spare code",
290
- "835" => "Spare code",
291
- "836" => "Spare code",
292
- "837" => "Spare code",
293
- "838" => "Spare code",
294
- "839" => "Spare code",
295
- "84" => "Viet Nam (Socialist Republic of)",
296
- "850" => "Democratic People's Republic of Korea",
297
- "851" => "Spare code",
298
- "852" => "Hong Kong, China",
299
- "853" => "Macao, China",
300
- "854" => "Spare code",
301
- "855" => "Cambodia (Kingdom of)",
302
- "856" => "Lao People's Democratic Republic",
303
- "857" => "Spare code",
304
- "858" => "Spare code",
305
- "859" => "Spare code",
306
- "86" => "China (People's Republic of)",
307
- "870" => "Inmarsat SNAC",
308
- "871" => "Inmarsat (Atlantic Ocean-East)",
309
- "872" => "Inmarsat (Pacific Ocean)",
310
- "873" => "Inmarsat (Indian Ocean)",
311
- "874" => "Inmarsat (Atlantic Ocean-West)",
312
- "875" => "Reserved - Maritime Mobile Service Applications",
313
- "876" => "Reserved - Maritime Mobile Service Applications",
314
- "877" => "Reserved - Maritime Mobile Service Applications",
315
- "878" => "Universal Personal Telecommunication Service (UPT)",
316
- "879" => "Reserved for national non-commercial purposes",
317
- "880" => "Bangladesh (People's Republic of)",
318
- "881" => "International Mobile, shared code",
319
- "882" => "International Networks, shared code",
320
- "883" => "Spare code",
321
- "884" => "Spare code",
322
- "885" => "Spare code",
323
- "886" => "Reserved",
324
- "887" => "Spare code",
325
- "888" => "Reserved for future global service",
326
- "889" => "Spare code",
327
- "890" => "Spare code",
328
- "891" => "Spare code",
329
- "892" => "Spare code",
330
- "893" => "Spare code",
331
- "894" => "Spare code",
332
- "895" => "Spare code",
333
- "896" => "Spare code",
334
- "897" => "Spare code",
335
- "898" => "Spare code",
336
- "899" => "Spare code",
337
- "90" => "Turkey",
338
- "91" => "India (Republic of)",
339
- "92" => "Pakistan (Islamic Republic of)",
340
- "93" => "Afghanistan",
341
- "94" => "Sri Lanka (Democratic Socialist Republic of)",
342
- "95" => "Myanmar (Union of)",
343
- "960" => "Maldives (Republic of)",
344
- "961" => "Lebanon",
345
- "962" => "Jordan (Hashemite Kingdom of)",
346
- "963" => "Syrian Arab Republic",
347
- "964" => "Iraq (Republic of)",
348
- "965" => "Kuwait (State of)",
349
- "966" => "Saudi Arabia (Kingdom of)",
350
- "967" => "Yemen (Republic of)",
351
- "968" => "Oman (Sultanate of)",
352
- "969" => "Reserved - reservation currently under investigation",
353
- "970" => "Reserved",
354
- "971" => "United Arab Emirates",
355
- "972" => "Israel (State of)",
356
- "973" => "Bahrain (Kingdom of)",
357
- "974" => "Qatar (State of)",
358
- "975" => "Bhutan (Kingdom of)",
359
- "976" => "Mongolia",
360
- "977" => "Nepal",
361
- "978" => "Spare code",
362
- "979" => "International Premium Rate Service (IPRS)",
363
- "98" => "Iran (Islamic Republic of)",
364
- "990" => "Spare code",
365
- "991" => "Trial of a proposed new international telecommunication public correspondence",
366
- "992" => "Tajikistan (Republic of)",
367
- "993" => "Turkmenistan",
368
- "994" => "Azerbaijani Republic",
369
- "995" => "Georgia"
370
- }
9
+ module ItuCodes
10
+ include Carmen
11
+
12
+ # TODO: move the base data into a fixture file to speed up loading
13
+ CODE_LIST = [
14
+ { "0" => "Reserved"},
15
+
16
+ { "1" => "United States"},
17
+
18
+ # from http://www.cnac.ca/co_codes/co_code_status_map.htm
19
+ { "1" => "Canada" },
20
+ { "1250" => "Canada" },
21
+ { "1204" => "Canada" },
22
+ { "1226" => "Canada" },
23
+ { "1250" => "Canada" },
24
+ { "1289" => "Canada" },
25
+ { "1306" => "Canada" },
26
+ { "1343" => "Canada" },
27
+ { "1403" => "Canada" },
28
+ { "1416" => "Canada" },
29
+ { "1418" => "Canada" },
30
+ { "1438" => "Canada" },
31
+ { "1450" => "Canada" },
32
+ { "1506" => "Canada" },
33
+ { "1514" => "Canada" },
34
+ { "1519" => "Canada" },
35
+ { "1581" => "Canada" },
36
+ { "1587" => "Canada" },
37
+ { "1604" => "Canada" },
38
+ { "1613" => "Canada" },
39
+ { "1647" => "Canada" },
40
+ { "1705" => "Canada" },
41
+ { "1709" => "Canada" },
42
+ { "1778" => "Canada" },
43
+ { "1780" => "Canada" },
44
+ { "1807" => "Canada" },
45
+ { "1819" => "Canada" },
46
+ { "1867" => "Canada" },
47
+ { "1902" => "Canada" },
48
+ { "1905" => "Canada" },
49
+
50
+ { "1" => "American Samoa" },
51
+ { "1684" => "American Samoa" },
52
+
53
+ { "1" => "Anguilla" },
54
+ { "1264" => "Anguilla" },
55
+
56
+ { "1" => "Antigua and Barbuda" },
57
+ { "1268" => "Antigua and Barbuda" },
58
+
59
+ { "1" => "Bahamas" },
60
+ { "1242" => "Bahamas" },
61
+
62
+ { "1" => "Barbados" },
63
+ { "1246" => "Barbados" },
64
+
65
+ { "1" => "Bermuda" },
66
+ { "1441" => "Bermuda" },
67
+
68
+ { "1" => "British Virgin Islands" },
69
+ { "1284" => "British Virgin Islands" },
70
+
71
+ { "1" => "Cayman Islands" },
72
+ { "1345" => "Cayman Islands" },
73
+
74
+ { "1" => "Dominica" },
75
+ { "1767" => "Dominica" },
76
+
77
+ { "1" => "Dominican Republic" },
78
+ { "1809" => "Dominican Republic" },
79
+ { "1829" => "Dominican Republic" },
80
+
81
+ { "1" => "Grenada" },
82
+ { "1473" => "Grenada" },
83
+
84
+ { "1" => "Guam" },
85
+ { "1671" => "Guam" },
86
+
87
+ { "1" => "Jamaica" },
88
+ { "1876" => "Jamaica" },
89
+
90
+ { "1" => "Montserrat" },
91
+ { "1664" => "Montserrat" },
92
+
93
+ { "1" => "Northern Mariana Islands" },
94
+ { "1670" => "Northern Mariana Islands" },
95
+
96
+ { "1" => "Saint Kitts and Nevis" },
97
+ { "1869" => "Saint Kitts and Nevis" },
98
+
99
+ { "1" => "Saint Lucia" },
100
+ { "1758" => "Saint Lucia" },
101
+
102
+ { "1" => "Saint Vincent and the Grenadines" },
103
+ { "1784" => "Saint Vincent and the Grenadines" },
104
+
105
+ { "1" => "Trinidad and Tobago" },
106
+ { "1868" => "Trinidad and Tobago" },
107
+
108
+ { "1" => "Turks and Caicos Islands" },
109
+ { "1649" => "Turks and Caicos Islands" },
110
+
111
+ { "1" => "United States Virgin Islands" },
112
+ { "1340" => "United States Virgin Islands" },
113
+
114
+ {"20" => "Egypt"},
115
+ {"210" => "Spare code"},
116
+ {"211" => "Spare code"},
117
+ {"212" => "Morocco"},
118
+ {"213" => "Algeria"},
119
+ {"214" => "Spare code"},
120
+ {"215" => "Spare code"},
121
+ {"216" => "Tunisia"},
122
+ {"217" => "Spare code"},
123
+ {"218" => "Libyan Arab Jamahiriya"},
124
+ {"219" => "Spare code"},
125
+ {"220" => "Gambia"},
126
+ {"221" => "Senegal"},
127
+ {"222" => "Mauritania"},
128
+ {"223" => "Mali"},
129
+ {"224" => "Guinea"},
130
+ {"225" => "Cote D'ivoire"},
131
+ {"226" => "Burkina Faso"},
132
+ {"227" => "Niger"},
133
+ {"228" => "Togo"},
134
+ {"229" => "Benin"},
135
+ {"230" => "Mauritius"},
136
+ {"231" => "Liberia"},
137
+ {"232" => "Sierra Leone"},
138
+ {"233" => "Ghana"},
139
+ {"234" => "Nigeria"},
140
+ {"235" => "Chad"},
141
+ {"236" => "Central African Republic"},
142
+ {"237" => "Cameroon"},
143
+ {"238" => "Cape Verde"},
144
+ {"239" => "Sao Tome and Principe"},
145
+ {"240" => "Equatorial Guinea"},
146
+ {"241" => "Gabon"},
147
+ {"242" => "Congo"},
148
+ {"243" => "Congo, the Democratic Republic of the"},
149
+ {"244" => "Angola"},
150
+ {"245" => "Guinea-Bissau"},
151
+ {"246" => "Diego Garcia"},
152
+ {"247" => "Ascension"},
153
+ {"248" => "Seychelles"},
154
+ {"249" => "Sudan"},
155
+ {"250" => "Rwanda"},
156
+ {"251" => "Ethiopia"},
157
+ {"252" => "Somalia"},
158
+ {"253" => "Djibouti"},
159
+ {"254" => "Kenya"},
160
+ {"255" => "Tanzania, United Republic of"},
161
+ {"256" => "Uganda"},
162
+ {"257" => "Burundi"},
163
+ {"258" => "Mozambique"},
164
+ {"259" => "Spare code"},
165
+ {"260" => "Zambia"},
166
+ {"261" => "Madagascar"},
167
+ {"262" => "Reunion"},
168
+ {"263" => "Zimbabwe"},
169
+ {"264" => "Namibia"},
170
+ {"265" => "Malawi"},
171
+ {"266" => "Lesotho"},
172
+ {"267" => "Botswana"},
173
+ {"268" => "Swaziland"},
174
+ {"269" => "Comoros"},
175
+ {"269" => "Mayotte"},
176
+ {"27" => "South Africa"},
177
+ {"280" => "Spare code"},
178
+ {"281" => "Spare code"},
179
+ {"282" => "Spare code"},
180
+ {"283" => "Spare code"},
181
+ {"284" => "Spare code"},
182
+ {"285" => "Spare code"},
183
+ {"286" => "Spare code"},
184
+ {"287" => "Spare code"},
185
+ {"288" => "Spare code"},
186
+ {"289" => "Spare code"},
187
+ {"290" => "Saint Helena"},
188
+ {"291" => "Eritrea"},
189
+ {"292" => "Spare code"},
190
+ {"293" => "Spare code"},
191
+ {"294" => "Spare code"},
192
+ {"295" => "Spare code"},
193
+ {"296" => "Spare code"},
194
+ {"297" => "Aruba"},
195
+ {"298" => "Faroe Islands"},
196
+ {"299" => "Greenland"},
197
+ {"30" => "Greece"},
198
+ {"31" => "Netherlands"},
199
+ {"32" => "Belgium"},
200
+ {"33" => "France"},
201
+ {"34" => "Spain"},
202
+ {"350" => "Gibraltar"},
203
+ {"351" => "Portugal"},
204
+ {"352" => "Luxembourg"},
205
+ {"353" => "Ireland"},
206
+ {"354" => "Iceland"},
207
+ {"355" => "Albania"},
208
+ {"356" => "Malta"},
209
+ {"357" => "Cyprus"},
210
+ {"358" => "Finland"},
211
+ {"359" => "Bulgaria"},
212
+ {"36" => "Hungary"},
213
+ {"370" => "Lithuania"},
214
+ {"371" => "Latvia"},
215
+ {"372" => "Estonia"},
216
+ {"373" => "Moldova, Republic of"},
217
+ {"374" => "Armenia"},
218
+ {"375" => "Belarus"},
219
+ {"376" => "Andorra"},
220
+ {"377" => "Monaco"},
221
+ {"378" => "San Marino"},
222
+ {"379" => "Holy See (Vatican City State},"},
223
+ {"380" => "Ukraine"},
224
+ {"381" => "Serbia and Montenegro"},
225
+ {"382" => "Spare code"},
226
+ {"383" => "Spare code"},
227
+ {"384" => "Spare code"},
228
+ {"385" => "Croatia"},
229
+ {"386" => "Slovenia"},
230
+ {"387" => "Bosnia and Herzegovina"},
231
+ {"388" => "Group of countries, shared code"},
232
+ {"389" => "Macedonia, the Former Yugoslav Republic of"},
233
+ {"39" => "Italy"},
234
+ {"40" => "Romania"},
235
+ {"41" => "Switzerland"},
236
+ {"420" => "Czech Republic"},
237
+ {"421" => "Slovakia"},
238
+ {"422" => "Spare code"},
239
+ {"423" => "Liechtenstein"},
240
+ {"424" => "Spare code"},
241
+ {"425" => "Spare code"},
242
+ {"426" => "Spare code"},
243
+ {"427" => "Spare code"},
244
+ {"428" => "Spare code"},
245
+ {"429" => "Spare code"},
246
+ {"43" => "Austria"},
247
+ {"44" => "United Kingdom"},
248
+ {"45" => "Denmark"},
249
+ {"46" => "Sweden"},
250
+ {"47" => "Norway"},
251
+ {"48" => "Poland"},
252
+ {"49" => "Germany"},
253
+ {"500" => "Falkland Islands"},
254
+ {"501" => "Belize"},
255
+ {"502" => "Guatemala"},
256
+ {"503" => "El Salvador"},
257
+ {"504" => "Honduras"},
258
+ {"505" => "Nicaragua"},
259
+ {"506" => "Costa Rica"},
260
+ {"507" => "Panama"},
261
+ {"508" => "Saint Pierre and Miquelon"},
262
+ {"509" => "Haiti"},
263
+ {"51" => "Peru"},
264
+ {"52" => "Mexico"},
265
+ {"53" => "Cuba"},
266
+ {"54" => "Argentina"},
267
+ {"55" => "Brazil"},
268
+ {"56" => "Chile"},
269
+ {"57" => "Colombia"},
270
+ {"58" => "Venezuela"},
271
+ {"590" => "Guadeloupe"},
272
+ {"591" => "Bolivia"},
273
+ {"592" => "Guyana"},
274
+ {"593" => "Ecuador"},
275
+ {"594" => "French Guiana"},
276
+ {"595" => "Paraguay"},
277
+ {"596" => "Martinique"},
278
+ {"597" => "Suriname"},
279
+ {"598" => "Uruguay"},
280
+ {"599" => "Netherlands Antilles"},
281
+ {"60" => "Malaysia"},
282
+ {"61" => "Australia"},
283
+ {"62" => "Indonesia"},
284
+ {"63" => "Philippines"},
285
+ {"64" => "New Zealand"},
286
+ {"65" => "Singapore"},
287
+ {"66" => "Thailand"},
288
+ {"670" => "Timor-Leste"},
289
+ {"671" => "Spare code"},
290
+ {"672" => "Australian External Territories"},
291
+ {"673" => "Brunei Darussalam"},
292
+ {"674" => "Nauru"},
293
+ {"675" => "Papua New Guinea"},
294
+ {"676" => "Tonga"},
295
+ {"677" => "Solomon Islands"},
296
+ {"678" => "Vanuatu"},
297
+ {"679" => "Fiji"},
298
+ {"680" => "Palau"},
299
+ {"681" => "Wallis and Futuna"},
300
+ {"682" => "Cook Islands"},
301
+ {"683" => "Niue"},
302
+ {"684" => "Spare code"},
303
+ {"685" => "Samoa"},
304
+ {"686" => "Kiribati"},
305
+ {"687" => "New Caledonia"},
306
+ {"688" => "Tuvalu"},
307
+ {"689" => "French Polynesia"},
308
+ {"690" => "Tokelau"},
309
+ {"691" => "Micronesia, Federated States of"},
310
+ {"692" => "Marshall Islands"},
311
+ {"693" => "Spare code"},
312
+ {"694" => "Spare code"},
313
+ {"695" => "Spare code"},
314
+ {"696" => "Spare code"},
315
+ {"697" => "Spare code"},
316
+ {"698" => "Spare code"},
317
+ {"699" => "Spare code"},
318
+
319
+ {"7" => "Russian Federation"},
320
+ {"7" => "Kazakhstan"},
321
+
322
+ {"800" => "International Freephone Service"},
323
+ {"801" => "Spare code"},
324
+ {"802" => "Spare code"},
325
+ {"803" => "Spare code"},
326
+ {"804" => "Spare code"},
327
+ {"805" => "Spare code"},
328
+ {"806" => "Spare code"},
329
+ {"807" => "Spare code"},
330
+ {"808" => "International Shared Cost Service"},
331
+ {"809" => "Spare code"},
332
+ {"81" => "Japan"},
333
+ {"82" => "Korea, Republic of"},
334
+ {"830" => "Spare code"},
335
+ {"831" => "Spare code"},
336
+ {"832" => "Spare code"},
337
+ {"833" => "Spare code"},
338
+ {"834" => "Spare code"},
339
+ {"835" => "Spare code"},
340
+ {"836" => "Spare code"},
341
+ {"837" => "Spare code"},
342
+ {"838" => "Spare code"},
343
+ {"839" => "Spare code"},
344
+ {"84" => "Viet Nam"},
345
+ {"850" => "Korea, Democratic People's Republic of"},
346
+ {"851" => "Spare code"},
347
+ {"852" => "Hong Kong"},
348
+ {"853" => "Macao"},
349
+ {"854" => "Spare code"},
350
+ {"855" => "Cambodia"},
351
+ {"856" => "Lao People's Democratic Republic"},
352
+ {"857" => "Spare code"},
353
+ {"858" => "Spare code"},
354
+ {"859" => "Spare code"},
355
+ {"86" => "China"},
356
+ {"870" => "Inmarsat SNAC"},
357
+ {"871" => "Inmarsat"},
358
+ {"872" => "Inmarsat"},
359
+ {"873" => "Inmarsat"},
360
+ {"874" => "Inmarsat"},
361
+ {"875" => "Reserved - Maritime Mobile Service Applications"},
362
+ {"876" => "Reserved - Maritime Mobile Service Applications"},
363
+ {"877" => "Reserved - Maritime Mobile Service Applications"},
364
+ {"878" => "Universal Personal Telecommunication Service"},
365
+ {"879" => "Reserved for national non-commercial purposes"},
366
+ {"880" => "Bangladesh"},
367
+ {"881" => "International Mobile, shared code"},
368
+ {"882" => "International Networks, shared code"},
369
+ {"883" => "Spare code"},
370
+ {"884" => "Spare code"},
371
+ {"885" => "Spare code"},
372
+ {"886" => "Reserved"},
373
+ {"887" => "Spare code"},
374
+ {"888" => "Reserved for future global service"},
375
+ {"889" => "Spare code"},
376
+ {"890" => "Spare code"},
377
+ {"891" => "Spare code"},
378
+ {"892" => "Spare code"},
379
+ {"893" => "Spare code"},
380
+ {"894" => "Spare code"},
381
+ {"895" => "Spare code"},
382
+ {"896" => "Spare code"},
383
+ {"897" => "Spare code"},
384
+ {"898" => "Spare code"},
385
+ {"899" => "Spare code"},
386
+ {"90" => "Turkey"},
387
+ {"91" => "India"},
388
+ {"92" => "Pakistan"},
389
+ {"93" => "Afghanistan"},
390
+ {"94" => "Sri Lanka"},
391
+ {"95" => "Myanmar"},
392
+ {"960" => "Maldives"},
393
+ {"961" => "Lebanon"},
394
+ {"962" => "Jordan"},
395
+ {"963" => "Syrian Arab Republic"},
396
+ {"964" => "Iraq"},
397
+ {"965" => "Kuwait"},
398
+ {"966" => "Saudi Arabia"},
399
+ {"967" => "Yemen"},
400
+ {"968" => "Oman"},
401
+ {"969" => "Reserved - reservation currently under investigation"},
402
+ {"970" => "Reserved"},
403
+ {"971" => "United Arab Emirates"},
404
+ {"972" => "Israel"},
405
+ {"973" => "Bahrain"},
406
+ {"974" => "Qatar"},
407
+ {"975" => "Bhutan"},
408
+ {"976" => "Mongolia"},
409
+ {"977" => "Nepal"},
410
+ {"978" => "Spare code"},
411
+ {"979" => "International Premium Rate Service"},
412
+ {"98" => "Iran, Islamic Republic of"},
413
+ {"990" => "Spare code"},
414
+ {"991" => "Trial of a proposed new international telecommunication public correspondence"},
415
+ {"992" => "Tajikistan"},
416
+ {"993" => "Turkmenistan"},
417
+ {"994" => "Azerbaijan"},
418
+ {"995" => "Georgia"}
419
+ ]
371
420
 
372
421
  def self.list
373
- @@CODE_LIST
374
- end
375
-
376
- # returns an array of hashes:
377
- # ItuCodes.find(995)
378
- # => [{"995"=>"Georgia"}]
379
- # ItuCodes.find("france")
380
- # => [{"33"=>"France"}]
381
- # ItuCodes.find("stan")
382
- # => [{"992" => "Tajikistan (Republic of)"},
383
- # => {"993" => "Turkmenistan"},
384
- # => {"7" => ["Kazakhstan (Republic of)", "Russian Federation"]},
385
- # => {"92" => "Pakistan (Islamic Republic of)"},
386
- # => {"93" => "Afghanistan"}
387
- # => ]
388
- def self.find (code_or_name)
389
- if code_or_name.to_s.match /[^0-9]/ # non-numeric string passed
422
+ ItuCodes::CODE_LIST
423
+ end
424
+
425
+ def self.lookup(code)
426
+ cleaned = clean(code)
427
+ list.find_all{ |hsh| hsh.keys.first == cleaned }.map do |k|
428
+ k.values.first
429
+ end.uniq
430
+ end
431
+
432
+ def self.find (code_or_name)
433
+ if code_or_name.to_s.match /[^0-9]/ # non-numeric string passed
390
434
  find_by_name(code_or_name)
391
435
  else
392
- find_by_code(code_or_name)
436
+ cleaned = self.clean(code_or_name)
437
+ find_by_itu_code(cleaned)
393
438
  end
394
439
  end
395
-
396
- def self.find_by_code(code)
397
- @@CODE_LIST.find_all { |k,v| k == code.to_s }.map do |pair|
398
- Hash[*pair]
440
+
441
+ def self.find_by_itu_code(code)
442
+ if american?(code) and code.length > 1
443
+ returner = 'United States'
444
+ else
445
+ returner = ItuCodes::CODE_LIST.find_all do |record|
446
+ record.has_key? code
447
+ end.map { |k| k.values.first }.uniq
399
448
  end
449
+ returner.size <= 1 ? returner.shift : returner
400
450
  end
401
451
 
402
- def self.find_by_name(name)
403
- @@CODE_LIST.find_all { |k,v| v.to_s.downcase.include? name.downcase }.map do |pair|
404
- Hash[*pair]
405
- end
452
+ def self.find_by_name(name)
453
+ returner = list.find_all do |record|
454
+ record.has_value? name
455
+ end.map{ |k| k.keys.first }.uniq
456
+ returner.size <= 1 ? returner.shift : returner
457
+ end
458
+
459
+ # ie. find_by_country_iso_code('US')
460
+ def self.iso2itu(iso_code)
461
+ ItuCodes.find_by_name( Carmen::country_name(iso_code) ).reject do |c|
462
+ north_american?(c) and c.length > 1
463
+ end.shift rescue nil
464
+ end
465
+
466
+ # this is tricky for North American destinations: '1' can be the US, Canada or another country
467
+ def self.itu2iso(itu_code)
468
+ ItuCodes.lookup(itu_code).map do |country|
469
+ Carmen::country_code( country ) rescue nil
470
+ end.compact
406
471
  end
407
-
408
- def self.valid_code?(some_code)
409
- some_code = some_code.to_s unless some_code.respond_to? :length
410
- if some_code[0,1] == "1" and some_code.length >= 4
411
- @@CODE_LIST['1'].key? some_code
472
+
473
+ # returns true for any valid ITU code
474
+ # valid_code?(1) => true
475
+ # valid_code?(1818) => false
476
+ def self.valid_code?(some_code)
477
+ if north_american?(some_code)
478
+ some_code.length == 1
412
479
  else
413
- @@CODE_LIST.key? some_code
480
+ self.list.any? { |hsh| hsh.has_key?(some_code) }
414
481
  end
415
482
  end
416
483
 
484
+ def self.north_america
485
+ find_by_itu_code '1'
486
+ end
487
+
488
+ def self.north_american?(some_code)
489
+ some_code.to_s[0,1] == '1'
490
+ end
491
+
417
492
  # parse a destination code (probably with area code) to find the ITU code
418
- # ex: parse_code(1818) => 1
493
+ # ex: parse_code(1818) => 1
419
494
  def self.parse_code(some_code)
420
- some_code = some_code.to_s unless some_code.respond_to? :length
421
-
422
- # North American codes are a headache:
423
- # http://en.wikipedia.org/wiki/List_of_country_calling_codes
424
- if some_code[0,1] == "1" and some_code.length >= 4
425
- if valid_code? some_code[0,4] # NOT the US or Canada --> 4 digit country code
426
- sub_index = 4
427
- else # US or CANADA --> 1 digit country code
428
- sub_index = 1
429
- end
430
- else
431
- sub_index = (1..some_code.length).find do |len|
432
- valid_code? some_code[0,len]
433
- end
434
- end
435
- some_code[0,sub_index] unless sub_index.nil?
436
- end
437
-
438
- def self.parse_area_code(some_code)
439
- some_code = some_code.to_s unless some_code.respond_to? :length
440
- if parse_code(some_code) and ! valid_code? some_code
441
- len = parse_code(some_code).length
442
- some_code[len..-1]
443
- else
444
- nil
495
+ some_code = clean(some_code)
496
+ sub_index = (1..some_code.length).find do |len|
497
+ valid_code? some_code[0,len]
445
498
  end
499
+ some_code[0,sub_index] unless sub_index.nil?
446
500
  end
447
-
448
- def self.compatriots?(some, other)
449
- returner = false
450
-
451
- some_country_code = parse_code some
452
- other_country_code = parse_code other
453
-
454
- if valid_code? some_country_code and valid_code? other_country_code
455
- returner = some_country_code == other_country_code
456
- else
457
- returner = nil
501
+
502
+ # parse a destination code (probably with area code) to find the number without the ITU code
503
+ # ex: parse_number(18184443322) => 8184443322
504
+ def self.parse_number(some_code)
505
+ some_code = clean(some_code)
506
+ sub_index = (1..some_code.length).find do |len|
507
+ valid_code? some_code[0,len]
458
508
  end
459
-
460
- returner
509
+ some_code[sub_index,some_code.length - sub_index] unless sub_index.nil?
510
+ end
511
+
512
+ def self.compatriots?(some, other)
513
+ both_valid = ! ( parse_code(some).nil? or parse_code(other).nil? )
514
+ both_valid and ( parse_code(some) == parse_code(other) ) rescue nil
461
515
  end
462
516
 
463
517
  def self.american?(some_code)
464
- parse_code(some_code) == "1"
518
+ # for non-US North American codes, parse_code will return a 4 digit code
519
+ # for US, '1' will be returned
520
+ some_code = some_code.to_s
521
+ countries = lookup(some_code[0,4])
522
+ north_american?(some_code) and (countries.include?('United States') or countries.empty?)
523
+ end
524
+
525
+ # converts input to string, then strips any non-numeric characters
526
+ def self.clean(input)
527
+ input.to_s.gsub(/[^0-9]/, '')
465
528
  end
466
-
467
- end
468
- # sample usage:
469
- # i = ItuCodes.new
470
- # i.valid_code? 8392813 # => false
471
- # i.valid_code? 7 # => true
472
- # i.parse_code 1818 # => 1
473
- # i.parse_code 4 # => nil
529
+ end