Module: Telesign::Country

Defined in:
lib/telesign/lib/country.rb

Defined Under Namespace

Classes: InvalidCountryName

Constant Summary collapse

NAMES_TO_ALPHA2 =
{
  "Andorra"                            => "AD",
  "United Arab Emirates"               => "AE",
  "Afghanistan"                        => "AF",
  "Anguilla"                           => "AI",
  "Albania"                            => "AL",
  "Armenia"                            => "AM",
  "Netherlands Antilles"               => "AN",
  "Angola"                             => "AO",
  "Antarctica"                         => "AQ",
  "Argentina"                          => "AR",
  "American Samoa"                     => "AS",
  "Austria"                            => "AT",
  "Australia"                          => "AU",
  "Aruba"                              => "AW",
  "Azerbaijan"                         => "AZ",
  "Bosnia and Herzegovina"             => "BA",
  "Barbados"                           => "BB",
  "Bangladesh"                         => "BD",
  "Belgium"                            => "BE",
  "Burkina Faso"                       => "BF",
  "Bulgaria"                           => "BG",
  "Bahrain"                            => "BH",
  "Burundi"                            => "BI",
  "Benin"                              => "BJ",
  "Bermuda"                            => "BM",
  "Brunei Darussalam"                  => "BN",
  "Bolivia"                            => "BO",
  "Brazil"                             => "BR",
  "Bahamas"                            => "BS",
  "Bhutan"                             => "BT",
  "Bouvet Island"                      => "BV",
  "Botswana"                           => "BW",
  "Belarus"                            => "BY",
  "Belize"                             => "BZ",
  "Canada"                             => "CA",
  "Cocos (Keeling) Islands"            => "CC",
  "Central African Republic"           => "CF",
  "Congo"                              => "CG",
  "Democratic Republic of the Congo"   => "CD",
  "Switzerland"                        => "CH",
  "Cote d'Ivoire"                      => "CI",
  "Cook Islands"                       => "CK",
  "Chile"                              => "CL",
  "Cameroon"                           => "CM",
  "China"                              => "CN",
  "Colombia"                           => "CO",
  "Costa Rica"                         => "CR",
  "Former Czechoslovakia"              => "CS",
  "Cuba"                               => "CU",
  "Cape Verde"                         => "CV",
  "Christmas Island"                   => "CX",
  "Cyprus"                             => "CY",
  "Czech Republic"                     => "CZ",
  "Germany"                            => "DE",
  "Djibouti"                           => "DJ",
  "Denmark"                            => "DK",
  "Dominica"                           => "DM",
  "Dominican Republic"                 => "DO",
  "Algeria"                            => "DZ",
  "Ecuador"                            => "EC",
  "Estonia"                            => "EE",
  "Egypt"                              => "EG",
  "Western Sahara"                     => "EH",
  "Eritrea"                            => "ER",
  "Spain"                              => "ES",
  "Ethiopia"                           => "ET",
  "Finland"                            => "FI",
  "Fiji"                               => "FJ",
  "Falkland Islands"                   => "FK",
  "Micronesia"                         => "FM",
  "Faroe Islands"                      => "FO",
  "France"                             => "FR",
  "France (European Territory)"        => "FX",
  "Gabon"                              => "GA",
  "Great Britain"                      => "GB",
  "Grenada"                            => "GD",
  "Georgia"                            => "GE",
  "French Guiana"                      => "GF",
  "Ghana"                              => "GH",
  "Gibraltar"                          => "GI",
  "Greenland"                          => "GL",
  "Gambia"                             => "GM",
  "Guinea"                             => "GN",
  "Guadeloupe"                         => "GP",
  "Equatorial Guinea"                  => "GQ",
  "Greece"                             => "GR",
  "S. Georgia & S. Sandwich Isls."     => "GS",
  "Guatemala"                          => "GT",
  "Guam (USA)"                         => "GU",
  "Guinea Bissau"                      => "GW",
  "Guyana"                             => "GY",
  "Hong Kong"                          => "HK",
  "Heard and McDonald Islands"         => "HM",
  "Honduras"                           => "HN",
  "Croatia"                            => "HR",
  "Haiti"                              => "HT",
  "Hungary"                            => "HU",
  "Indonesia"                          => "ID",
  "Ireland"                            => "IE",
  "Israel"                             => "IL",
  "India"                              => "IN",
  "British Indian Ocean Territory"     => "IO",
  "Iraq"                               => "IQ",
  "Iran"                               => "IR",
  "Iceland"                            => "IS",
  "Italy"                              => "IT",
  "Jamaica"                            => "JM",
  "Jordan"                             => "JO",
  "Japan"                              => "JP",
  "Kenya"                              => "KE",
  "Kyrgyzstan"                         => "KG",
  "Cambodia"                           => "KH",
  "Kiribati"                           => "KI",
  "Comoros"                            => "KM",
  "Saint Kitts & Nevis Anguilla"       => "KN",
  "North Korea"                        => "KP",
  "South Korea"                        => "KR",
  "Kuwait"                             => "KW",
  "Cayman Islands"                     => "KY",
  "Kazakhstan"                         => "KZ",
  "Laos"                               => "LA",
  "Lebanon"                            => "LB",
  "Saint Lucia"                        => "LC",
  "Liechtenstein"                      => "LI",
  "Sri Lanka"                          => "LK",
  "Liberia"                            => "LR",
  "Lesotho"                            => "LS",
  "Lithuania"                          => "LT",
  "Luxembourg"                         => "LU",
  "Latvia"                             => "LV",
  "Libya"                              => "LY",
  "Morocco"                            => "MA",
  "Monaco"                             => "MC",
  "Moldova"                            => "MD",
  "Madagascar"                         => "MG",
  "Marshall Islands"                   => "MH",
  "Macedonia"                          => "MK",
  "Mali"                               => "ML",
  "Myanmar"                            => "MM",
  "Mongolia"                           => "MN",
  "Macau"                              => "MO",
  "Northern Mariana Islands"           => "MP",
  "Martinique"                         => "MQ",
  "Mauritania"                         => "MR",
  "Montserrat"                         => "MS",
  "Malta"                              => "MT",
  "Mauritius"                          => "MU",
  "Maldives"                           => "MV",
  "Malawi"                             => "MW",
  "Mexico"                             => "MX",
  "Malaysia"                           => "MY",
  "Mozambique"                         => "MZ",
  "Namibia"                            => "NA",
  "New Caledonia"                      => "NC",
  "Niger"                              => "NE",
  "Norfolk Island"                     => "NF",
  "Nigeria"                            => "NG",
  "Nicaragua"                          => "NI",
  "Netherlands"                        => "NL",
  "Norway"                             => "NO",
  "Nepal"                              => "NP",
  "Nauru"                              => "NR",
  "Neutral Zone"                       => "NT",
  "Niue"                               => "NU",
  "New Zealand"                        => "NZ",
  "Oman"                               => "OM",
  "Panama"                             => "PA",
  "Peru"                               => "PE",
  "French Polynesia"                   => "PF",
  "Papua New Guinea"                   => "PG",
  "Philippines"                        => "PH",
  "Pakistan"                           => "PK",
  "Poland"                             => "PL",
  "St. Pierre and Miquelon"            => "PM",
  "Pitcairn Island"                    => "PN",
  "Portugal"                           => "PT",
  "Palau"                              => "PW",
  "Paraguay"                           => "PY",
  "Qatar"                              => "QA",
  "Reunion"                            => "RE",
  "Romania"                            => "RO",
  "Russia"                             => "RU",
  "Rwanda"                             => "RW",
  "Saudi Arabia"                       => "SA",
  "Solomon Islands"                    => "SB",
  "Serbia and Montenegro"              => "RS",
  "Seychelles"                         => "SC",
  "Sudan"                              => "SD",
  "Sweden"                             => "SE",
  "Singapore"                          => "SG",
  "St. Helena"                         => "SH",
  "Slovenia"                           => "SI",
  "Svalbard and Jan Mayen Islands"     => "SJ",
  "Slovakia"                           => "SK",
  "Sierra Leone"                       => "SL",
  "San Marino"                         => "SM",
  "Senegal"                            => "SN",
  "Somalia"                            => "SO",
  "Suriname"                           => "SR",
  "Sao Tome and Principe"              => "ST",
  "Former USSR"                        => "SU",
  "El Salvador"                        => "SV",
  "Syria"                              => "SY",
  "Swaziland"                          => "SZ",
  "Turks and Caicos Islands"           => "TC",
  "Chad"                               => "TD",
  "French Southern Territories"        => "TF",
  "Togo"                               => "TG",
  "Thailand"                           => "TH",
  "Tajikistan"                         => "TJ",
  "Tokelau"                            => "TK",
  "Turkmenistan"                       => "TM",
  "Tunisia"                            => "TN",
  "Tonga"                              => "TO",
  "East Timor"                         => "TP",
  "Turkey"                             => "TR",
  "Trinidad and Tobago"                => "TT",
  "Tuvalu"                             => "TV",
  "Taiwan"                             => "TW",
  "Tanzania"                           => "TZ",
  "Ukraine"                            => "UA",
  "Uganda"                             => "UG",
  "United Kingdom"                     => "UK",
  "United States"                      => "US",
  "Uruguay"                            => "UY",
  "Uzbekistan"                         => "UZ",
  "Vatican"                            => "VA",
  "Saint Vincent & Grenadines"         => "VC",
  "Venezuela"                          => "VE",
  "Virgin Islands (British)"           => "VG",
  "Virgin Islands (USA)"               => "VI",
  "Vietnam"                            => "VN",
  "Vanuatu"                            => "VU",
  "Wallis and Futuna Islands"          => "WF",
  "Samoa"                              => "WS",
  "Yemen"                              => "YE",
  "Mayotte"                            => "YT",
  "Yugoslavia"                         => "YU",
  "South Africa"                       => "ZA",
  "Zambia"                             => "ZM",
  "Zaire"                              => "ZR",
  "Zimbabwe"                           => "ZW"
}
ALPHA2_TO_CODE =
{
  "AD" => 376,
  "AE" => 971,
  "AF" => 93,
  "AL" => 355,
  "AM" => 374,
  "AN" => 599,
  "AO" => 244,
  "AR" => 54,
  "AT" => 43,
  "AU" => 61,
  "AW" => 297,
  "AZ" => 994,
  "BA" => 387,
  "BD" => 880,
  "BE" => 32,
  "BF" => 226,
  "BG" => 359,
  "BH" => 973,
  "BI" => 257,
  "BJ" => 229,
  "BN" => 673,
  "BO" => 591,
  "BR" => 55,
  "BT" => 975,
  "BW" => 267,
  "BY" => 375,
  "BZ" => 501,
  "CA" => 1,
  "CD" => 243,
  "CF" => 236,
  "CG" => 242,
  "CH" => 41,
  "CI" => 225,
  "CK" => 682,
  "CL" => 56,
  "CM" => 237,
  "CN" => 86,
  "CO" => 57,
  "CR" => 506,
  "CU" => 53,
  "CV" => 238,
  "CY" => 357,
  "CZ" => 420,
  "DE" => 49,
  "DJ" => 253,
  "DK" => 45,
  "DO" => 1,
  "DZ" => 213,
  "EC" => 593,
  "EE" => 372,
  "EG" => 20,
  "ER" => 291,
  "ES" => 34,
  "ET" => 251,
  "FI" => 358,
  "FJ" => 679,
  "FK" => 500,
  "FM" => 691,
  "FO" => 298,
  "FR" => 33,
  "GA" => 241,
  "GB" => 44,
  "GD" => 473,
  "GE" => 995,
  "GF" => 594,
  "GH" => 233,
  "GI" => 350,
  "GL" => 299,
  "GM" => 220,
  "GN" => 224,
  "GP" => 590,
  "GQ" => 240,
  "GR" => 30,
  "GT" => 502,
  "GW" => 245,
  "GY" => 592,
  "HK" => 852,
  "HN" => 504,
  "HR" => 385,
  "HT" => 509,
  "HU" => 36,
  "ID" => 62,
  "IE" => 353,
  "IL" => 972,
  "IN" => 91,
  "IQ" => 964,
  "IR" => 98,
  "IS" => 354,
  "IT" => 39,
  "JM" => 876,
  "JO" => 962,
  "JP" => 81,
  "KE" => 254,
  "KG" => 996,
  "KH" => 855,
  "KI" => 686,
  "KM" => 269,
  "KP" => 850,
  "KR" => 82,
  "KW" => 965,
  "KZ" => 7,
  "LA" => 856,
  "LB" => 961,
  "LI" => 423,
  "LK" => 94,
  "LR" => 231,
  "LS" => 266,
  "LT" => 370,
  "LU" => 352,
  "LV" => 371,
  "LY" => 218,
  "MA" => 212,
  "MC" => 377,
  "MD" => 373,
  "MG" => 261,
  "MH" => 692,
  "MK" => 389,
  "ML" => 223,
  "MM" => 95,
  "MN" => 976,
  "MO" => 853,
  "MQ" => 596,
  "MR" => 222,
  "MT" => 356,
  "MU" => 230,
  "MV" => 960,
  "MW" => 265,
  "MX" => 52,
  "MY" => 60,
  "MZ" => 258,
  "NA" => 264,
  "NC" => 687,
  "NE" => 227,
  "NG" => 234,
  "NI" => 505,
  "NL" => 31,
  "NO" => 47,
  "NP" => 977,
  "NR" => 674,
  "NU" => 683,
  "NZ" => 64,
  "OM" => 968,
  "PA" => 507,
  "PE" => 51,
  "PF" => 689,
  "PG" => 675,
  "PH" => 63,
  "PK" => 92,
  "PL" => 48,
  "PM" => 508,
  "PR" => 1,
  "PT" => 351,
  "PW" => 680,
  "PY" => 595,
  "QA" => 974,
  "RE" => 262,
  "RO" => 40,
  "RS" => 381,
  "RU" => 7,
  "RW" => 250,
  "SA" => 966,
  "SB" => 677,
  "SC" => 248,
  "SD" => 249,
  "SE" => 46,
  "SG" => 65,
  "SH" => 290,
  "SI" => 386,
  "SK" => 421,
  "SL" => 232,
  "SM" => 378,
  "SN" => 221,
  "SO" => 252,
  "SR" => 597,
  "ST" => 239,
  "SV" => 503,
  "SY" => 963,
  "SZ" => 268,
  "TD" => 235,
  "TG" => 228,
  "TH" => 66,
  "TJ" => 992,
  "TK" => 690,
  "TM" => 993,
  "TN" => 216,
  "TO" => 676,
  "TP" => 670,
  "TR" => 90,
  "TV" => 688,
  "TW" => 886,
  "TZ" => 255,
  "UA" => 380,
  "UG" => 256,
  "UK" => 44,
  "US" => 1,
  "UY" => 598,
  "UZ" => 998,
  "VA" => 379,
  "VE" => 58,
  "VN" => 84,
  "VU" => 678,
  "WF" => 681,
  "WS" => 685,
  "YE" => 967,
  "YT" => 269,
  "YU" => 381,
  "ZA" => 27,
  "ZM" => 260,
  "ZW" => 263
}

Class Method Summary collapse

Class Method Details

.country_code_for_name(name) ⇒ Object



463
464
465
466
467
468
469
470
471
472
# File 'lib/telesign/lib/country.rb', line 463

def self.country_code_for_name(name)
  return name.to_i if name =~ /^\d+$/ && ALPHA2_TO_CODE.values.include?(name.to_i)
  
  alpha2 = name =~ /^\w{2}$/ ?
    name :
    NAMES_TO_ALPHA2[name]
    
  code = ALPHA2_TO_CODE[alpha2] if alpha2
  code || raise(InvalidCountryName.new("Could not locate country code for name: #{name}"))
end