Skip to content

Commit c65b0da

Browse files
committed
Merge pull request code-dot-org#7457 from code-dot-org/removeGeographyCountries
Establishes COUNTRY_CODE_TO_COUNTRY_NAME and country_name_from_code.
2 parents 0e718e2 + 8b25981 commit c65b0da

File tree

2 files changed

+274
-0
lines changed

2 files changed

+274
-0
lines changed

lib/country_codes.rb

Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
COUNTRY_CODE_TO_COUNTRY_NAME = {
2+
"AD" => "Andorra",
3+
"AE" => "United Arab Emirates",
4+
"AF" => "Afghanistan",
5+
"AG" => "Antigua and Barbuda",
6+
"AI" => "Anguilla",
7+
"AL" => "Albania",
8+
"AM" => "Armenia",
9+
"AO" => "Angola",
10+
"AQ" => "Antarctica",
11+
"AR" => "Argentina",
12+
"AS" => "American Samoa",
13+
"AT" => "Austria",
14+
"AU" => "Australia",
15+
"AW" => "Aruba",
16+
"AX" => "Åland Islands",
17+
"AZ" => "Azerbaijan",
18+
"BA" => "Bosnia and Herzegovina",
19+
"BB" => "Barbados",
20+
"BD" => "Bangladesh",
21+
"BE" => "Belgium",
22+
"BF" => "Burkina Faso",
23+
"BG" => "Bulgaria",
24+
"BH" => "Bahrain",
25+
"BI" => "Burundi",
26+
"BJ" => "Benin",
27+
"BL" => "Saint Barthélemy",
28+
"BM" => "Bermuda",
29+
"BN" => "Brunei",
30+
"BO" => "Bolivia, Plurinational State of",
31+
"BQ" => "Bonaire, Sint Eustatius and Saba",
32+
"BR" => "Brazil",
33+
"BS" => "Bahamas",
34+
"BT" => "Bhutan",
35+
"BV" => "Bouvet Island",
36+
"BW" => "Botswana",
37+
"BY" => "Belarus",
38+
"CA" => "Canada",
39+
"CC" => "Cocos (Keeling) Islands",
40+
"CD" => "Congo, the Democratic Republic of the",
41+
"CF" => "Central African Republic",
42+
"CG" => "Congo",
43+
"CH" => "Switzerland",
44+
"CI" => "Côte d'Ivoire",
45+
"CK" => "Cook Islands",
46+
"CL" => "Chile",
47+
"CM" => "Cameroon",
48+
"CN" => "China",
49+
"CO" => "Colombia",
50+
"CR" => "Costa Rica",
51+
"CU" => "Cuba",
52+
"CV" => "Cape Verde",
53+
"CW" => "Curaçao",
54+
"CX" => "Christmas Island",
55+
"CY" => "Cyprus",
56+
"CZ" => "Czech Republic",
57+
"DE" => "Germany",
58+
"DJ" => "Djibouti",
59+
"DK" => "Denmark",
60+
"DM" => "Dominica",
61+
"DO" => "Dominican Republic",
62+
"DZ" => "Algeria",
63+
"EC" => "Ecuador",
64+
"EE" => "Estonia",
65+
"EG" => "Egypt",
66+
"EH" => "Western Sahara",
67+
"ER" => "Eritrea",
68+
"ES" => "Spain",
69+
"ET" => "Ethiopia",
70+
"FI" => "Finland",
71+
"FJ" => "Fiji",
72+
"FK" => "Falkland Islands (Malvinas)",
73+
"FM" => "Micronesia, Federated States of",
74+
"FO" => "Faroe Islands",
75+
"FR" => "France",
76+
"GA" => "Gabon",
77+
"GB" => "United Kingdom",
78+
"GD" => "Grenada",
79+
"GE" => "Georgia",
80+
"GF" => "French Guiana",
81+
"GG" => "Guernsey",
82+
"GH" => "Ghana",
83+
"GI" => "Gibraltar",
84+
"GL" => "Greenland",
85+
"GM" => "Gambia",
86+
"GN" => "Guinea",
87+
"GP" => "Guadeloupe",
88+
"GQ" => "Equatorial Guinea",
89+
"GR" => "Greece",
90+
"GS" => "South Georgia and the South Sandwich Islands",
91+
"GT" => "Guatemala",
92+
"GU" => "Guam",
93+
"GW" => "Guinea-Bissau",
94+
"GY" => "Guyana",
95+
"HK" => "Hong Kong",
96+
"HM" => "Heard Island and McDonald Islands",
97+
"HN" => "Honduras",
98+
"HR" => "Croatia",
99+
"HT" => "Haiti",
100+
"HU" => "Hungary",
101+
"ID" => "Indonesia",
102+
"IE" => "Ireland",
103+
"IL" => "Israel",
104+
"IM" => "Isle of Man",
105+
"IN" => "India",
106+
"IO" => "British Indian Ocean Territory",
107+
"IQ" => "Iraq",
108+
"IR" => "Iran, Islamic Republic of",
109+
"IS" => "Iceland",
110+
"IT" => "Italy",
111+
"JE" => "Jersey",
112+
"JM" => "Jamaica",
113+
"JO" => "Jordan",
114+
"JP" => "Japan",
115+
"KE" => "Kenya",
116+
"KG" => "Kyrgyzstan",
117+
"KH" => "Cambodia",
118+
"KI" => "Kiribati",
119+
"KM" => "Comoros",
120+
"KN" => "Saint Kitts and Nevis",
121+
"KP" => "Korea, Democratic People's Republic of",
122+
"KR" => "Korea, Republic of",
123+
"KW" => "Kuwait",
124+
"KY" => "Cayman Islands",
125+
"KZ" => "Kazakhstan",
126+
"LA" => "Lao People's Democratic Republic",
127+
"LB" => "Lebanon",
128+
"LC" => "Saint Lucia",
129+
"LI" => "Liechtenstein",
130+
"LK" => "Sri Lanka",
131+
"LR" => "Liberia",
132+
"LS" => "Lesotho",
133+
"LT" => "Lithuania",
134+
"LU" => "Luxembourg",
135+
"LV" => "Latvia",
136+
"LY" => "Libya",
137+
"MA" => "Morocco",
138+
"MC" => "Monaco",
139+
"MD" => "Moldova",
140+
"ME" => "Montenegro",
141+
"MG" => "Madagascar",
142+
"MH" => "Marshall Islands",
143+
"MK" => "Republic of Macedonia",
144+
"ML" => "Mali",
145+
"MM" => "Myanmar",
146+
"MN" => "Mongolia",
147+
"MO" => "Macao",
148+
"MP" => "Northern Mariana Islands",
149+
"MQ" => "Martinique",
150+
"MR" => "Mauritania",
151+
"MS" => "Montserrat",
152+
"MT" => "Malta",
153+
"MU" => "Mauritius",
154+
"MV" => "Maldives",
155+
"MW" => "Malawi",
156+
"MX" => "Mexico",
157+
"MY" => "Malaysia",
158+
"MZ" => "Mozambique",
159+
"NA" => "Namibia",
160+
"NC" => "New Caledonia",
161+
"NE" => "Niger",
162+
"NF" => "Norfolk Island",
163+
"NG" => "Nigeria",
164+
"NI" => "Nicaragua",
165+
"NL" => "Netherlands",
166+
"NO" => "Norway",
167+
"NP" => "Nepal",
168+
"NR" => "Nauru",
169+
"NU" => "Niue",
170+
"NZ" => "New Zealand",
171+
"OM" => "Oman",
172+
"PA" => "Panama",
173+
"PE" => "Peru",
174+
"PF" => "French Polynesia",
175+
"PG" => "Papua New Guinea",
176+
"PH" => "Philippines",
177+
"PK" => "Pakistan",
178+
"PL" => "Poland",
179+
"PM" => "Saint Pierre and Miquelon",
180+
"PN" => "Pitcairn",
181+
"PR" => "Puerto Rico",
182+
"PS" => "Palestine",
183+
"PT" => "Portugal",
184+
"PW" => "Palau",
185+
"PY" => "Paraguay",
186+
"QA" => "Qatar",
187+
"RE" => "Réunion",
188+
"RO" => "Romania",
189+
"RS" => "Serbia",
190+
"RU" => "Russian Federation",
191+
"RW" => "Rwanda",
192+
"SA" => "Saudi Arabia",
193+
"SB" => "Solomon Islands",
194+
"SC" => "Seychelles",
195+
"SD" => "Sudan",
196+
"SE" => "Sweden",
197+
"SG" => "Singapore",
198+
"SI" => "Slovenia",
199+
"SJ" => "Svalbard and Jan Mayen",
200+
"SK" => "Slovakia",
201+
"SL" => "Sierra Leone",
202+
"SM" => "San Marino",
203+
"SN" => "Senegal",
204+
"SO" => "Somalia",
205+
"SR" => "Suriname",
206+
"SS" => "South Sudan",
207+
"ST" => "Sao Tome and Principe",
208+
"SV" => "El Salvador",
209+
"SX" => "Sint Maarten (Dutch part)",
210+
"SY" => "Syrian Arab Republic",
211+
"SZ" => "Swaziland",
212+
"TC" => "Turks and Caicos Islands",
213+
"TD" => "Chad",
214+
"TG" => "Togo",
215+
"TH" => "Thailand",
216+
"TJ" => "Tajikistan",
217+
"TK" => "Tokelau",
218+
"TL" => "Timor-Leste",
219+
"TM" => "Turkmenistan",
220+
"TN" => "Tunisia",
221+
"TO" => "Tonga",
222+
"TR" => "Turkey",
223+
"TT" => "Trinidad and Tobago",
224+
"TV" => "Tuvalu",
225+
"TW" => "Taiwan",
226+
"TZ" => "Tanzania",
227+
"UA" => "Ukraine",
228+
"UG" => "Uganda",
229+
"US" => "United States",
230+
"UY" => "Uruguay",
231+
"UZ" => "Uzbekistan",
232+
"VA" => "Holy See (Vatican City State)",
233+
"VC" => "Saint Vincent and the Grenadines",
234+
"VE" => "Venezuela, Bolivarian Republic of",
235+
"VG" => "Virgin Islands, British",
236+
"VI" => "Virgin Islands, U.S.",
237+
"VN" => "Viet Nam",
238+
"VU" => "Vanuatu",
239+
"WF" => "Wallis and Futuna",
240+
"WS" => "Samoa",
241+
"YE" => "Yemen",
242+
"YT" => "Mayotte",
243+
"ZA" => "South Africa",
244+
"ZM" => "Zambia",
245+
"ZW" => "Zimbabwe",
246+
}
247+
248+
# Returns the name of the country whose two character country code is code.
249+
# If code is not a valid two character country code, returns code.
250+
def country_name_from_code(code)
251+
return COUNTRY_CODE_TO_COUNTRY_NAME[code.to_s.strip.upcase] || code
252+
end

shared/test/country_codes_test.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require 'minitest/autorun'
2+
require 'rack/test'
3+
4+
require_relative '../../lib/country_codes.rb'
5+
6+
class CountryCodesTest < Minitest::Test
7+
def test_country_name_from_code_valid_codes
8+
assert_equal 'Andorra', country_name_from_code('ad')
9+
assert_equal 'Andorra', country_name_from_code('AD')
10+
assert_equal 'Andorra', country_name_from_code(' AD ')
11+
12+
assert_equal 'United States', country_name_from_code('us')
13+
assert_equal 'United States', country_name_from_code('US')
14+
assert_equal 'United States', country_name_from_code(' US ')
15+
end
16+
17+
def test_country_name_from_code_invalid_codes
18+
assert_equal '', country_name_from_code('')
19+
assert_equal 'ZZ', country_name_from_code('ZZ')
20+
assert_equal 'my country', country_name_from_code('my country')
21+
end
22+
end

0 commit comments

Comments
 (0)