|
class Country extend self |
|
# data pulled from: http://www.unc.edu/~rowlett/units/codes/country.htm |
|
|
|
ISO_ALPHA2 = [""," AF"," AX"," AL"," "," DZ"," AS"," AD"," AO"," AI"," AQ"," AG"," AR"," AM"," AW"," "," AU"," AT"," AZ",""," BS"," BH"," BD"," BB"," BY"," BE"," BZ"," BJ"," BM"," BT"," BO","BQ"," BA"," BW"," BV"," BR",""," IO"," BN"," BG"," BF"," BI"," KH"," CM"," CA"," CV"," KY"," CF"," TD"," "," CL"," CN",""," CG"," CD"," CK"," CR"," CI"," HR"," CU","CW"," CY"," CZ"," DK"," DJ",""," DM"," DO"," EC"," EG"," SV"," GQ"," ER"," EE"," ET"," "," FO"," FK"," FJ"," FI"," FR",""," GF"," PF"," TF"," GA"," GM"," GE"," DE"," GH"," GI","GB"," GR"," GL"," GD"," GP"," GU"," GT",""," GG"," GN"," GW"," GY"," HT"," HM"," IS"," IN"," ID"," "," IR"," IQ"," IE",""," IM"," IL"," IT"," JM"," JP"," JE"," JO"," KZ"," KE"," KI"," KP"," KR"," KW"," KG"," LA"," LV",""," LB"," LS"," LR"," LY"," LI"," LT"," LU"," MO"," MK"," MG"," MW"," MY"," MV"," ML"," MT",""," MH"," MQ"," MR"," MU"," YT"," MX"," FM"," MD"," MC"," MN","ME"," MS"," NA"," MZ"," NR"," NP"," NL"," AN"," NC"," NZ"," NI"," NE"," NG"," NU"," NF"," MP"," NO"," OM"," PK",""," PW"," PS"," PA"," PG"," PY"," PE"," PH"," PN"," PL"," PT"," PR"," QA"," RE"," RO"," RU",""," RW","BL"," SH"," KN"," LC","MF"," PM"," VC"," WS"," SM"," ST"," SA"," SN"," RS"," SC"," SL"," SG","","SX"," SK"," GS","SS"," "," ES"," LK"," SD"," SR"," SJ"," SZ"," SE"," CH"," SY",""," TW"," TJ"," "," TZ"," TH"," TL"," TG"," TK"," TO"," TT"," TN"," TR"," TM"," TC"," TV"," UG"," UA",""," AE"," GB"," US"," UM"," UY"," UZ"," VU"," VA"," VE"," VN"," VG"," VI"," "," WF"," EH"," YE",""," ZM"," "," ZW"].freeze |
|
ISO_ALPHA3 = [""," AFG"," ALA"," ALB"," "," DZA"," ASM"," AND"," AGO"," AIA"," ATA"," ATG"," ARG"," ARM"," ABW"," "," AUS"," AUT"," AZE",""," BHS"," BHR"," BGD"," BRB"," BLR"," BEL"," BLZ"," BEN"," BMU"," BTN"," BOL","BES"," BIH"," BWA"," BVT"," BRA",""," IOT"," BRN"," BGR"," BFA"," BDI"," KHM"," CMR"," CAN"," CPV"," CYM"," CAF"," TCD"," "," CHL"," CHN",""," COG"," COD"," COK"," CRI"," CIV"," HRV"," CUB","CUW"," CYP"," CZE"," DNK"," DJI",""," DMA"," DOM"," ECU"," EGY"," SLV"," GNQ"," ERI"," EST"," ETH"," "," FRO"," FLK"," FJI"," FIN"," FRA",""," GUF"," PYF"," ATF"," GAB"," GMB"," GEO"," DEU"," GHA"," GIB","GBR"," GRC"," GRL"," GRD"," GLP"," GUM"," GTM",""," GGY"," GIN"," GNB"," GUY"," HTI"," HMD"," ISL"," IND"," IDN"," "," IRN"," IRQ"," IRL",""," IMN"," ISR"," ITA"," JAM"," JPN"," JEY"," JOR"," KAZ"," KEN"," KIR"," PRK"," KOR"," KWT"," KGZ"," LAO"," LVA",""," LBN"," LSO"," LBR"," LBY"," LIE"," LTU"," LUX"," MAC"," MKD"," MDG"," MWI"," MYS"," MDV"," MLI"," MLT",""," MHL"," MTQ"," MRT"," MUS"," MYT"," MEX"," FSM"," MDA"," MCO"," MNG","MNE"," MSR"," NAM"," MOZ"," NRU"," NPL"," NLD"," ANT"," NCL"," NZL"," NIC"," NER"," NGA"," NIU"," NFK"," MNP"," NOR"," OMN"," PAK",""," PLW"," PSE"," PAN"," PNG"," PRY"," PER"," PHL"," PCN"," POL"," PRT"," PRI"," QAT"," REU"," ROU"," RUS",""," RWA","BLM"," SHN"," KNA"," LCA","MAF"," SPM"," VCT"," WSM"," SMR"," STP"," SAU"," SEN"," SRB"," SYC"," SLE"," SGP","","SXM"," SVK"," SGS","SSD"," "," ESP"," LKA"," SDN"," SUR"," SJM"," SWZ"," SWE"," CHE"," SYR",""," TWN"," TJK"," "," TZA"," THA"," TLS"," TGO"," TKL"," TON"," TTO"," TUN"," TUR"," TKM"," TCA"," TUV"," UGA"," UKR",""," ARE"," GBR"," USA"," UMI"," URY"," UZB"," VUT"," VAT"," VEN"," VNM"," VGB"," VIR"," "," WLF"," ESH"," YEM",""," ZMB"," "," ZWE"].freeze |
|
UN_NUMERIC = [""," 004"," 248"," 008"," "," 012"," 016"," 020"," 024"," 660"," 010"," 028"," 032"," 051"," 533"," "," 036"," 040"," 031",""," 044"," 048"," 050"," 052"," 112"," 056"," 084"," 204"," 060"," 064"," 068","535"," 070"," 072"," 074"," 076",""," 086"," 096"," 100"," 854"," 108"," 116"," 120"," 124"," 132"," 136"," 140"," 148"," 830"," 152"," 156",""," 178"," 180"," 184"," 188"," 384"," 191"," 192","531"," 196"," 203"," 208"," 262",""," 212"," 214"," 218"," 818"," 222"," 226"," 232"," 233"," 231"," "," 234"," 238"," 242"," 246"," 250",""," 254"," 258"," 260"," 266"," 270"," 268"," 276"," 288"," 292","826"," 300"," 304"," 308"," 312"," 316"," 320",""," "," 324"," 624"," 328"," 332"," 334"," 352"," 356"," 360"," "," 364"," 368"," 372",""," 833"," 376"," 380"," 388"," 392"," "," 400"," 398"," 404"," 296"," 408"," 410"," 414"," 417"," 418"," 428",""," 422"," 426"," 430"," 434"," 438"," 440"," 442"," 446"," 807"," 450"," 454"," 458"," 462"," 466"," 470",""," 584"," 474"," 478"," 480"," 175"," 484"," 583"," 498"," 492"," 496","499"," 500"," 516"," 508"," 520"," 524"," 528"," 530"," 540"," 554"," 558"," 562"," 566"," 570"," 574"," 580"," 578"," 512"," 586",""," 585"," 275"," 591"," 598"," 600"," 604"," 608"," 612"," 616"," 620"," 630"," 634"," 638"," 642"," 643",""," 646","652"," 654"," 659"," 662","663"," 666"," 670"," 882"," 674"," 678"," 682"," 686"," 688"," 690"," 694"," 702","","534"," 703"," 239"," "," "," 724"," 144"," 736"," 740"," 744"," 748"," 752"," 756"," 760",""," 158"," 762"," "," 834"," 764"," 626"," 768"," 772"," 776"," 780"," 788"," 792"," 795"," 796"," 798"," 800"," 804",""," 784"," 826"," 840"," 581"," 858"," 860"," 548"," 336"," 862"," 704"," 92"," 850"," "," 876"," 732"," 887",""," 894"," "," 716"].freeze |
|
COUNTRY = [""," AFGHANISTAN"," Ã
LAND ISLANDS"," ALBANIA","ALDERNEY"," ALGERIA (El Djazaïr)"," AMERICAN SAMOA"," ANDORRA"," ANGOLA"," ANGUILLA"," ANTARCTICA"," ANTIGUA AND BARBUDA"," ARGENTINA"," ARMENIA"," ARUBA"," ASCENSION ISLAND"," AUSTRALIA"," AUSTRIA"," AZERBAIJAN",""," BAHAMAS"," BAHRAIN"," BANGLADESH"," BARBADOS"," BELARUS"," BELGIUM"," BELIZE"," BENIN"," BERMUDA"," BHUTAN"," BOLIVIA","BONAIRE, ST. EUSTATIUS, AND SABA"," BOSNIA AND HERZEGOVINA"," BOTSWANA"," BOUVET ISLAND"," BRAZIL",""," BRITISH INDIAN OCEAN TERRITORY"," BRUNEI DARUSSALAM"," BULGARIA"," BURKINA FASO"," BURUNDI"," CAMBODIA"," CAMEROON"," CANADA"," CAPE VERDE"," CAYMAN ISLANDS"," CENTRAL AFRICAN REPUBLIC"," CHAD (Tchad)"," CHANNEL ISLANDS"," CHILE"," CHINA",""," CONGO, REPUBLIC OF"," CONGO, THE DEMOCRATIC REPUBLIC OF THE (formerly Zaire)"," COOK ISLANDS"," COSTA RICA"," CÃTE D'IVOIRE (Ivory Coast)"," CROATIA (Hrvatska)"," CUBA","CURAÃAO"," CYPRUS"," CZECH REPUBLIC"," DENMARK"," DJIBOUTI",""," DOMINICA"," DOMINICAN REPUBLIC"," ECUADOR"," EGYPT"," EL SALVADOR"," EQUATORIAL GUINEA"," ERITREA"," ESTONIA"," ETHIOPIA","EUROPEAN UNION"," FAEROE ISLANDS"," FALKLAND ISLANDS (MALVINAS)"," FIJI"," FINLAND"," FRANCE",""," FRENCH GUIANA"," FRENCH POLYNESIA"," FRENCH SOUTHERN TERRITORIES"," GABON"," GAMBIA, THE"," GEORGIA"," GERMANY (Deutschland)"," GHANA"," GIBRALTAR","GREAT BRITAIN (United Kingdom)"," GREECE"," GREENLAND"," GRENADA"," GUADELOUPE"," GUAM"," GUATEMALA",""," GUERNSEY"," GUINEA"," GUINEA-BISSAU"," GUYANA"," HAITI"," HEARD ISLAND AND MCDONALD ISLANDS"," ICELAND"," INDIA"," INDONESIA","INTERNATIONAL ORGANIZATIONS "," IRAN (Islamic Republic of Iran)"," IRAQ"," IRELAND",""," ISLE OF MAN"," ISRAEL"," ITALY"," JAMAICA"," JAPAN"," JERSEY"," JORDAN (Hashemite Kingdom of Jordan)"," KAZAKHSTAN"," KENYA"," KIRIBATI"," KOREA (Democratic Peoples Republic of [North] Korea)"," KOREA (Republic of [South] Korea)"," KUWAIT"," KYRGYZSTAN"," LAO PEOPLE'S DEMOCRATIC REPUBLIC"," LATVIA",""," LEBANON"," LESOTHO"," LIBERIA"," LIBYA (Libyan Arab Jamahirya)"," LIECHTENSTEIN (Fürstentum Liechtenstein)"," LITHUANIA"," LUXEMBOURG"," MACAO (Special Administrative Region of China)"," MACEDONIA (Former Yugoslav Republic of Macedonia)"," MADAGASCAR"," MALAWI"," MALAYSIA"," MALDIVES"," MALI"," MALTA",""," MARSHALL ISLANDS"," MARTINIQUE"," MAURITANIA"," MAURITIUS"," MAYOTTE"," MEXICO"," MICRONESIA (Federated States of Micronesia)"," MOLDOVA"," MONACO"," MONGOLIA","MONTENEGRO"," MONTSERRAT"," NAMIBIA"," MOZAMBIQUE (Moçambique)"," NAURU"," NEPAL"," NETHERLANDS"," NETHERLANDS ANTILLES (obsolete)"," NEW CALEDONIA"," NEW ZEALAND"," NICARAGUA"," NIGER"," NIGERIA"," NIUE"," NORFOLK ISLAND"," NORTHERN MARIANA ISLANDS"," NORWAY"," OMAN"," PAKISTAN",""," PALAU"," PALESTINIAN TERRITORIES"," PANAMA"," PAPUA NEW GUINEA"," PARAGUAY"," PERU"," PHILIPPINES"," PITCAIRN"," POLAND"," PORTUGAL"," PUERTO RICO"," QATAR"," RÃUNION"," ROMANIA"," RUSSIAN FEDERATION",""," RWANDA","SAINT BARTHÃLEMY "," SAINT HELENA"," SAINT KITTS AND NEVIS"," SAINT LUCIA","SAINT MARTIN (French portion) "," SAINT PIERRE AND MIQUELON"," SAINT VINCENT AND THE GRENADINES"," SAMOA (formerly Western Samoa)"," SAN MARINO (Republic of)"," SAO TOME AND PRINCIPE"," SAUDI ARABIA (Kingdom of Saudi Arabia)"," SENEGAL"," SERBIA (Republic of Serbia)"," SEYCHELLES"," SIERRA LEONE"," SINGAPORE","","SINT MAARTEN"," SLOVAKIA (Slovak Republic)"," SOUTH GEORGIA AND THE SOUTH SANDWICH ISLANDS","SOUTH SUDAN","SOVIET UNION (Internet code still used)"," SPAIN (España)"," SRI LANKA (formerly Ceylon)"," SUDAN"," SURINAME"," SVALBARD AND JAN MAYEN"," SWAZILAND"," SWEDEN"," SWITZERLAND (Confederation of Helvetia)"," SYRIAN ARAB REPUBLIC",""," TAIWAN (\"Chinese Taipei\" for IOC)"," TAJIKISTAN","TANGANYIKA"," TANZANIA"," THAILAND"," TIMOR-LESTE (formerly East Timor)"," TOGO"," TOKELAU"," TONGA"," TRINIDAD AND TOBAGO"," TUNISIA"," TURKEY"," TURKMENISTAN"," TURKS AND CAICOS ISLANDS"," TUVALU"," UGANDA"," UKRAINE",""," UNITED ARAB EMIRATES"," UNITED KINGDOM"," UNITED STATES"," UNITED STATES MINOR OUTLYING ISLANDS"," URUGUAY"," UZBEKISTAN"," VANUATU"," VATICAN CITY (Holy See)"," VENEZUELA"," VIET NAM"," VIRGIN ISLANDS, BRITISH"," VIRGIN ISLANDS, U.S.","YUGOSLAVIA (Internet code still used)"," WALLIS AND FUTUNA"," WESTERN SAHARA (formerly Spanish Sahara)"," YEMEN (Yemen Arab Republic)",""," ZAMBIA (formerly Northern Rhodesia)","ZANZIBAR"," ZIMBABWE"].freeze |
|
VALID_TYPES = [:iso_alpha2, :iso_alpha3, :un_numerical, :country ].freeze |
|
attr :from_type, :to_type, :input |
|
|
|
#dynamic form |
|
# example: find_country = Country.match("AFG").to_ISO_ALPHA3.for_Country |
|
# or find_country = Country.match["AFG"].to(:iso_alpha3).for_target(:country) |
|
# or find_country = Contry.match("AFG").to_ISO_ALPHA3.for_target(:country) |
|
# or find_alpha_3 = Country.match["United Kingdom"].to_country.for_target(:iso_alpha3) |
|
|
|
def match.[](input) |
|
@search = input |
|
self |
|
end |
|
|
|
def match search |
|
@search = input |
|
self |
|
end |
|
|
|
def semethod_missing(meth, *args, &block) |
|
if meth.to_s =~ /^to_(.+)$/ |
|
to($1) |
|
elsif meth.to_s =~ /^for_(.+)$/ |
|
for_target($1) |
|
elsif meth.to_s =~ /^get_(.+)$/ |
|
get($1) |
|
else |
|
super |
|
end |
|
end |
|
|
|
def to to |
|
@compare_to = convert(to) |
|
self |
|
end |
|
|
|
def for_target target |
|
@target = convert(target) |
|
return @target[@compare_to.index(@search)] |
|
end |
|
|
|
def get input |
|
return convert input |
|
end |
|
|
|
|
|
#verbose static form (as in can't dynamicly change what your looking for, where your looking for and the target) |
|
#example: = find_country = Country.match_country_to_alpha3_for("AFG") |
|
def match_country_for_alpha2_for code |
|
COUNTRY[ISO_ALPHA2.index(code.upcase)] |
|
end |
|
|
|
def match_country_for_alpha3_for code |
|
COUNTRY[ISO_ALPHA3.index(code.upcase)] |
|
end |
|
|
|
def match_country_for_numeric_for code |
|
COUNTRY[UN_NUMERIC.index(code.upcase)] |
|
end |
|
|
|
def match_alpha2_for_country_for country |
|
ISO_ALPHA2[COUNTRY.index(country.upcase)] |
|
end |
|
|
|
def match_alpha3_for_country_for country |
|
ISO_ALPHA3[COUNTRY.index(country.upcase)] |
|
end |
|
|
|
def match_numeric_for_country_for country |
|
UN_NUMERIC[COUNTRY.index(country.upcase)] |
|
end |
|
|
|
private |
|
|
|
def convert input |
|
Country.const_get(input.to_s.uppercase) |
|
end |
|
end |