model_un 0.1 → 0.2
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/README.md +36 -6
- data/lib/model_un/data.rb +483 -0
- data/lib/model_un/version.rb +1 -1
- data/lib/model_un.rb +39 -2
- data/spec/model_un_spec.rb +62 -17
- metadata +3 -3
data/README.md
CHANGED
@@ -3,7 +3,7 @@ ModelUN - Quick and easy country and US State abbreviations
|
|
3
3
|
|
4
4
|
ModelUN is a tiny little Ruby library for abbreviating and, er, de-abbreviating Countries and US State names.
|
5
5
|
|
6
|
-
Brought to you by [Uh Huh Yeah](http://uhhuhyeah.com/) and [Climber](http://www.climber.com/)
|
6
|
+
Brought to you by [Uh Huh Yeah](http://uhhuhyeah.com/) with help and encouragement from [Climber.com](http://www.climber.com/)
|
7
7
|
|
8
8
|
Installation
|
9
9
|
-------------
|
@@ -11,8 +11,8 @@ Installation
|
|
11
11
|
gem install model_un
|
12
12
|
|
13
13
|
|
14
|
-
ModelUN API
|
15
|
-
|
14
|
+
ModelUN API - US States
|
15
|
+
=======================
|
16
16
|
|
17
17
|
Convert a US State to it's abbreviated name
|
18
18
|
-------------------------------------------
|
@@ -33,14 +33,44 @@ Or, a shortcut for either method
|
|
33
33
|
my_state_abbr = ModelUN.convert(my_state) # returns 'CA'
|
34
34
|
|
35
35
|
my_state_abbr = 'CA'
|
36
|
-
my_state_name = ModelUN.convert(my_state_abbr) # returns 'California'
|
36
|
+
my_state_name = ModelUN.convert(my_state_abbr) # returns 'California'
|
37
|
+
|
37
38
|
|
39
|
+
ModelUN API - Countries
|
40
|
+
=======================
|
38
41
|
|
39
|
-
|
42
|
+
Convert a Country to it's (NATO) abbreviated name
|
43
|
+
-------------------------------------------
|
44
|
+
|
45
|
+
my_country = 'Sweden'
|
46
|
+
my_country_abbr = ModelUN.convert_country_name(my_country) # returns 'SWE'
|
47
|
+
|
48
|
+
Convert a NATO Country abbreviation to it's full name
|
49
|
+
-------------------------------------------------
|
50
|
+
|
51
|
+
my_country_abbr = 'SWE'
|
52
|
+
my_country_name = ModelUN.convert_country_abbr(my_country_abbr) # returns 'Sweden'
|
53
|
+
|
54
|
+
Or, a shortcut for either method
|
55
|
+
--------------------------------
|
56
|
+
|
57
|
+
my_country = 'Sweden'
|
58
|
+
my_country_abbr = ModelUN.convert(my_country) # returns 'SWE'
|
59
|
+
|
60
|
+
my_country_abbr = 'SWE'
|
61
|
+
my_country_name = ModelUN.convert(my_country_abbr) # returns 'Sweden'
|
62
|
+
|
63
|
+
|
64
|
+
Current version: v0.2 (February 2011)
|
40
65
|
=====================================
|
41
66
|
|
42
67
|
* Supports conversion of US State names (including Guam, Marshal and Virgin Islands)
|
43
|
-
*
|
68
|
+
* Supports conversion of Countries against NATO Country names and codes
|
69
|
+
|
70
|
+
Upcoming features:
|
71
|
+
=====================================
|
72
|
+
|
73
|
+
* Support converting countries by their common "nick names". E.g. Accepting "United Kingdom" for "United Kingdom of Great Britain and Northern Ireland"
|
44
74
|
|
45
75
|
Known issues
|
46
76
|
------------
|
data/lib/model_un/data.rb
CHANGED
@@ -186,4 +186,487 @@ module ModelUN
|
|
186
186
|
{'full' => 'WYOMING', 'abbr' => 'WY'}
|
187
187
|
]
|
188
188
|
|
189
|
+
|
190
|
+
NATO_COUNTRY_NAMES = [
|
191
|
+
"AFGHANISTAN",
|
192
|
+
"ALAND ISLANDS",
|
193
|
+
"ALBANIA",
|
194
|
+
"ALGERIA",
|
195
|
+
"AMERICAN SAMOA",
|
196
|
+
"ANDORRA",
|
197
|
+
"ANGOLA",
|
198
|
+
"ANGUILLA",
|
199
|
+
"ANTIGUA AND BARBUDA",
|
200
|
+
"ARGENTINA",
|
201
|
+
"ARMENIA",
|
202
|
+
"ARUBA",
|
203
|
+
"AUSTRALIA",
|
204
|
+
"AUSTRIA",
|
205
|
+
"AZERBAIJAN",
|
206
|
+
"BAHAMAS",
|
207
|
+
"BAHRAIN",
|
208
|
+
"BANGLADESH",
|
209
|
+
"BARBADOS",
|
210
|
+
"BELARUS",
|
211
|
+
"BELGIUM",
|
212
|
+
"BELIZE",
|
213
|
+
"BENIN",
|
214
|
+
"BERMUDA",
|
215
|
+
"BHUTAN",
|
216
|
+
"BOLIVIA (PLURINATIONAL STATE OF)",
|
217
|
+
"BONAIRE, SAINT EUSTATIUS AND SABA",
|
218
|
+
"BOSNIA AND HERZEGOVINA",
|
219
|
+
"BOTSWANA",
|
220
|
+
"BRAZIL",
|
221
|
+
"BRITISH VIRGIN ISLANDS",
|
222
|
+
"BRUNEI DARUSSALAM",
|
223
|
+
"BULGARIA",
|
224
|
+
"BURKINA FASO",
|
225
|
+
"BURUNDI",
|
226
|
+
"CAMBODIA",
|
227
|
+
"CAMEROON",
|
228
|
+
"CANADA",
|
229
|
+
"CAPE VERDE",
|
230
|
+
"CAYMAN ISLANDS",
|
231
|
+
"CENTRAL AFRICAN REPUBLIC",
|
232
|
+
"CHAD",
|
233
|
+
"CHILE",
|
234
|
+
"CHINA",
|
235
|
+
"CHINA, HONG KONG SPECIAL ADMINISTRATIVE REGION",
|
236
|
+
"CHINA, MACAO SPECIAL ADMINISTRATIVE REGION",
|
237
|
+
"COLOMBIA",
|
238
|
+
"COMOROS",
|
239
|
+
"CONGO",
|
240
|
+
"COOK ISLANDS",
|
241
|
+
"COSTA RICA",
|
242
|
+
"COTE D'IVOIRE",
|
243
|
+
"CROATIA",
|
244
|
+
"CUBA",
|
245
|
+
"CURAÇAO",
|
246
|
+
"CYPRUS",
|
247
|
+
"CZECH REPUBLIC",
|
248
|
+
"DEMOCRATIC PEOPLE'S REPUBLIC OF KOREA",
|
249
|
+
"DEMOCRATIC REPUBLIC OF THE CONGO",
|
250
|
+
"DENMARK",
|
251
|
+
"DJIBOUTI",
|
252
|
+
"DOMINICA",
|
253
|
+
"DOMINICAN REPUBLIC",
|
254
|
+
"ECUADOR",
|
255
|
+
"EGYPT",
|
256
|
+
"EL SALVADOR",
|
257
|
+
"EQUATORIAL GUINEA",
|
258
|
+
"ERITREA",
|
259
|
+
"ESTONIA",
|
260
|
+
"ETHIOPIA",
|
261
|
+
"FAEROE ISLANDS",
|
262
|
+
"FALKLAND ISLANDS (MALVINAS)",
|
263
|
+
"FIJI",
|
264
|
+
"FINLAND",
|
265
|
+
"FRANCE",
|
266
|
+
"FRENCH GUIANA",
|
267
|
+
"FRENCH POLYNESIA",
|
268
|
+
"GABON",
|
269
|
+
"GAMBIA",
|
270
|
+
"GEORGIA",
|
271
|
+
"GERMANY",
|
272
|
+
"GHANA",
|
273
|
+
"GIBRALTAR",
|
274
|
+
"GREECE",
|
275
|
+
"GREENLAND",
|
276
|
+
"GRENADA",
|
277
|
+
"GUADELOUPE",
|
278
|
+
"GUAM",
|
279
|
+
"GUATEMALA",
|
280
|
+
"GUERNSEY",
|
281
|
+
"GUINEA",
|
282
|
+
"GUINEA-BISSAU",
|
283
|
+
"GUYANA",
|
284
|
+
"HAITI",
|
285
|
+
"HOLY SEE",
|
286
|
+
"HONDURAS",
|
287
|
+
"HUNGARY",
|
288
|
+
"ICELAND",
|
289
|
+
"INDIA",
|
290
|
+
"INDONESIA",
|
291
|
+
"IRAN (ISLAMIC REPUBLIC OF)",
|
292
|
+
"IRAQ",
|
293
|
+
"IRELAND",
|
294
|
+
"ISLE OF MAN",
|
295
|
+
"ISRAEL",
|
296
|
+
"ITALY",
|
297
|
+
"JAMAICA",
|
298
|
+
"JAPAN",
|
299
|
+
"JERSEY ",
|
300
|
+
"JORDAN",
|
301
|
+
"KAZAKHSTAN",
|
302
|
+
"KENYA",
|
303
|
+
"KIRIBATI",
|
304
|
+
"KUWAIT",
|
305
|
+
"KYRGYZSTAN",
|
306
|
+
"LAO PEOPLE'S DEMOCRATIC REPUBLIC",
|
307
|
+
"LATVIA",
|
308
|
+
"LEBANON",
|
309
|
+
"LESOTHO",
|
310
|
+
"LIBERIA",
|
311
|
+
"LIBYAN ARAB JAMAHIRIYA",
|
312
|
+
"LIECHTENSTEIN",
|
313
|
+
"LITHUANIA",
|
314
|
+
"LUXEMBOURG",
|
315
|
+
"MADAGASCAR",
|
316
|
+
"MALAWI",
|
317
|
+
"MALAYSIA",
|
318
|
+
"MALDIVES",
|
319
|
+
"MALI",
|
320
|
+
"MALTA",
|
321
|
+
"MARSHALL ISLANDS",
|
322
|
+
"MARTINIQUE",
|
323
|
+
"MAURITANIA",
|
324
|
+
"MAURITIUS",
|
325
|
+
"MAYOTTE ",
|
326
|
+
"MEXICO",
|
327
|
+
"MICRONESIA (FEDERATED STATES OF)",
|
328
|
+
"MONACO",
|
329
|
+
"MONGOLIA",
|
330
|
+
"MONTENEGRO",
|
331
|
+
"MONTSERRAT",
|
332
|
+
"MOROCCO",
|
333
|
+
"MOZAMBIQUE",
|
334
|
+
"MYANMAR",
|
335
|
+
"NAMIBIA",
|
336
|
+
"NAURU",
|
337
|
+
"NEPAL",
|
338
|
+
"NETHERLANDS",
|
339
|
+
"NEW CALEDONIA",
|
340
|
+
"NEW ZEALAND",
|
341
|
+
"NICARAGUA",
|
342
|
+
"NIGER",
|
343
|
+
"NIGERIA",
|
344
|
+
"NIUE",
|
345
|
+
"NORFOLK ISLAND",
|
346
|
+
"NORTHERN MARIANA ISLANDS",
|
347
|
+
"NORWAY",
|
348
|
+
"OCCUPIED PALESTINIAN TERRITORY",
|
349
|
+
"OMAN",
|
350
|
+
"PAKISTAN",
|
351
|
+
"PALAU",
|
352
|
+
"PANAMA",
|
353
|
+
"PAPUA NEW GUINEA",
|
354
|
+
"PARAGUAY",
|
355
|
+
"PERU",
|
356
|
+
"PHILIPPINES",
|
357
|
+
"PITCAIRN",
|
358
|
+
"POLAND",
|
359
|
+
"PORTUGAL",
|
360
|
+
"PUERTO RICO",
|
361
|
+
"QATAR",
|
362
|
+
"REPUBLIC OF KOREA",
|
363
|
+
"REPUBLIC OF MOLDOVA",
|
364
|
+
"REUNION",
|
365
|
+
"ROMANIA",
|
366
|
+
"RUSSIAN FEDERATION",
|
367
|
+
"RWANDA",
|
368
|
+
"SAINT-BARTHELEMY",
|
369
|
+
"SAINT HELENA",
|
370
|
+
"SAINT KITTS AND NEVIS",
|
371
|
+
"SAINT LUCIA",
|
372
|
+
"SAINT-MARTIN (FRENCH PART)",
|
373
|
+
"SAINT PIERRE AND MIQUELON",
|
374
|
+
"SAINT VINCENT AND THE GRENADINES",
|
375
|
+
"SAMOA",
|
376
|
+
"SAN MARINO",
|
377
|
+
"SAO TOME AND PRINCIPE",
|
378
|
+
"SAUDI ARABIA",
|
379
|
+
"SENEGAL",
|
380
|
+
"SERBIA",
|
381
|
+
"SEYCHELLES",
|
382
|
+
"SIERRA LEONE",
|
383
|
+
"SINGAPORE",
|
384
|
+
"SINT MAARTEN (DUTCH PART)",
|
385
|
+
"SLOVAKIA",
|
386
|
+
"SLOVENIA",
|
387
|
+
"SOLOMON ISLANDS",
|
388
|
+
"SOMALIA",
|
389
|
+
"SOUTH AFRICA",
|
390
|
+
"SPAIN",
|
391
|
+
"SRI LANKA",
|
392
|
+
"SUDAN",
|
393
|
+
"SURINAME",
|
394
|
+
"SVALBARD AND JAN MAYEN ISLANDS",
|
395
|
+
"SWAZILAND",
|
396
|
+
"SWEDEN",
|
397
|
+
"SWITZERLAND",
|
398
|
+
"SYRIAN ARAB REPUBLIC",
|
399
|
+
"TAJIKISTAN",
|
400
|
+
"THAILAND",
|
401
|
+
"THE FORMER YUGOSLAV REPUBLIC OF MACEDONIA",
|
402
|
+
"TIMOR-LESTE",
|
403
|
+
"TOGO",
|
404
|
+
"TOKELAU",
|
405
|
+
"TONGA",
|
406
|
+
"TRINIDAD AND TOBAGO",
|
407
|
+
"TUNISIA",
|
408
|
+
"TURKEY",
|
409
|
+
"TURKMENISTAN",
|
410
|
+
"TURKS AND CAICOS ISLANDS",
|
411
|
+
"TUVALU",
|
412
|
+
"UGANDA",
|
413
|
+
"UKRAINE",
|
414
|
+
"UNITED ARAB EMIRATES",
|
415
|
+
"UNITED KINGDOM OF GREAT BRITAIN AND NORTHERN IRELAND",
|
416
|
+
"UNITED REPUBLIC OF TANZANIA",
|
417
|
+
"UNITED STATES OF AMERICA",
|
418
|
+
"UNITED STATES VIRGIN ISLANDS",
|
419
|
+
"URUGUAY",
|
420
|
+
"UZBEKISTAN",
|
421
|
+
"VANUATU",
|
422
|
+
"VENEZUELA (BOLIVARIAN REPUBLIC OF)",
|
423
|
+
"VIET NAM",
|
424
|
+
"WALLIS AND FUTUNA ISLANDS",
|
425
|
+
"WESTERN SAHARA",
|
426
|
+
"YEMEN",
|
427
|
+
"ZAMBIA",
|
428
|
+
"ZIMBABWE"
|
429
|
+
]
|
430
|
+
|
431
|
+
NATO_ABBR = [
|
432
|
+
'AFG',
|
433
|
+
'ALA',
|
434
|
+
'ALB',
|
435
|
+
'DZA',
|
436
|
+
'ASM',
|
437
|
+
'AND',
|
438
|
+
'AGO',
|
439
|
+
'AIA',
|
440
|
+
'ATG',
|
441
|
+
'ARG',
|
442
|
+
'ARM',
|
443
|
+
'ABW',
|
444
|
+
'AUS',
|
445
|
+
'AUT',
|
446
|
+
'AZE',
|
447
|
+
'BHS',
|
448
|
+
'BHR',
|
449
|
+
'BGD',
|
450
|
+
'BRB',
|
451
|
+
'BLR',
|
452
|
+
'BEL',
|
453
|
+
'BLZ',
|
454
|
+
'BEN',
|
455
|
+
'BMU',
|
456
|
+
'BTN',
|
457
|
+
'BOL',
|
458
|
+
'BES',
|
459
|
+
'BIH',
|
460
|
+
'BWA',
|
461
|
+
'BRA',
|
462
|
+
'VGB',
|
463
|
+
'BRN',
|
464
|
+
'BGR',
|
465
|
+
'BFA',
|
466
|
+
'BDI',
|
467
|
+
'KHM',
|
468
|
+
'CMR',
|
469
|
+
'CAN',
|
470
|
+
'CPV',
|
471
|
+
'CYM',
|
472
|
+
'CAF',
|
473
|
+
'TCD',
|
474
|
+
'CHL',
|
475
|
+
'CHN',
|
476
|
+
'HKG',
|
477
|
+
'MAC',
|
478
|
+
'COL',
|
479
|
+
'COM',
|
480
|
+
'COG',
|
481
|
+
'COK',
|
482
|
+
'CRI',
|
483
|
+
'CIV',
|
484
|
+
'HRV',
|
485
|
+
'CUB',
|
486
|
+
'CUW',
|
487
|
+
'CYP',
|
488
|
+
'CZE',
|
489
|
+
'PRK',
|
490
|
+
'COD',
|
491
|
+
'DNK',
|
492
|
+
'DJI',
|
493
|
+
'DMA',
|
494
|
+
'DOM',
|
495
|
+
'ECU',
|
496
|
+
'EGY',
|
497
|
+
'SLV',
|
498
|
+
'GNQ',
|
499
|
+
'ERI',
|
500
|
+
'EST',
|
501
|
+
'ETH',
|
502
|
+
'FRO',
|
503
|
+
'FLK',
|
504
|
+
'FJI',
|
505
|
+
'FIN',
|
506
|
+
'FRA',
|
507
|
+
'GUF',
|
508
|
+
'PYF',
|
509
|
+
'GAB',
|
510
|
+
'GMB',
|
511
|
+
'GEO',
|
512
|
+
'DEU',
|
513
|
+
'GHA',
|
514
|
+
'GIB',
|
515
|
+
'GRC',
|
516
|
+
'GRL',
|
517
|
+
'GRD',
|
518
|
+
'GLP',
|
519
|
+
'GUM',
|
520
|
+
'GTM',
|
521
|
+
'GGY',
|
522
|
+
'GIN',
|
523
|
+
'GNB',
|
524
|
+
'GUY',
|
525
|
+
'HTI',
|
526
|
+
'VAT',
|
527
|
+
'HND',
|
528
|
+
'HUN',
|
529
|
+
'ISL',
|
530
|
+
'IND',
|
531
|
+
'IDN',
|
532
|
+
'IRN',
|
533
|
+
'IRQ',
|
534
|
+
'IRL',
|
535
|
+
'IMN',
|
536
|
+
'ISR',
|
537
|
+
'ITA',
|
538
|
+
'JAM',
|
539
|
+
'JPN',
|
540
|
+
'JEY',
|
541
|
+
'JOR',
|
542
|
+
'KAZ',
|
543
|
+
'KEN',
|
544
|
+
'KIR',
|
545
|
+
'KWT',
|
546
|
+
'KGZ',
|
547
|
+
'LAO',
|
548
|
+
'LVA',
|
549
|
+
'LBN',
|
550
|
+
'LSO',
|
551
|
+
'LBR',
|
552
|
+
'LBY',
|
553
|
+
'LIE',
|
554
|
+
'LTU',
|
555
|
+
'LUX',
|
556
|
+
'MDG',
|
557
|
+
'MWI',
|
558
|
+
'MYS',
|
559
|
+
'MDV',
|
560
|
+
'MLI',
|
561
|
+
'MLT',
|
562
|
+
'MHL',
|
563
|
+
'MTQ',
|
564
|
+
'MRT',
|
565
|
+
'MUS',
|
566
|
+
'MYT',
|
567
|
+
'MEX',
|
568
|
+
'FSM',
|
569
|
+
'MCO',
|
570
|
+
'MNG',
|
571
|
+
'MNE',
|
572
|
+
'MSR',
|
573
|
+
'MAR',
|
574
|
+
'MOZ',
|
575
|
+
'MMR',
|
576
|
+
'NAM',
|
577
|
+
'NRU',
|
578
|
+
'NPL',
|
579
|
+
'NLD',
|
580
|
+
'NCL',
|
581
|
+
'NZL',
|
582
|
+
'NIC',
|
583
|
+
'NER',
|
584
|
+
'NGA',
|
585
|
+
'NIU',
|
586
|
+
'NFK',
|
587
|
+
'MNP',
|
588
|
+
'NOR',
|
589
|
+
'PSE',
|
590
|
+
'OMN',
|
591
|
+
'PAK',
|
592
|
+
'PLW',
|
593
|
+
'PAN',
|
594
|
+
'PNG',
|
595
|
+
'PRY',
|
596
|
+
'PER',
|
597
|
+
'PHL',
|
598
|
+
'PCN',
|
599
|
+
'POL',
|
600
|
+
'PRT',
|
601
|
+
'PRI',
|
602
|
+
'QAT',
|
603
|
+
'KOR',
|
604
|
+
'MDA',
|
605
|
+
'REU',
|
606
|
+
'ROU',
|
607
|
+
'RUS',
|
608
|
+
'RWA',
|
609
|
+
'BLM',
|
610
|
+
'SHN',
|
611
|
+
'KNA',
|
612
|
+
'LCA',
|
613
|
+
'MAF',
|
614
|
+
'SPM',
|
615
|
+
'VCT',
|
616
|
+
'WSM',
|
617
|
+
'SMR',
|
618
|
+
'STP',
|
619
|
+
'SAU',
|
620
|
+
'SEN',
|
621
|
+
'SRB',
|
622
|
+
'SYC',
|
623
|
+
'SLE',
|
624
|
+
'SGP',
|
625
|
+
'SXM',
|
626
|
+
'SVK',
|
627
|
+
'SVN',
|
628
|
+
'SLB',
|
629
|
+
'SOM',
|
630
|
+
'ZAF',
|
631
|
+
'ESP',
|
632
|
+
'LKA',
|
633
|
+
'SDN',
|
634
|
+
'SUR',
|
635
|
+
'SJM',
|
636
|
+
'SWZ',
|
637
|
+
'SWE',
|
638
|
+
'CHE',
|
639
|
+
'SYR',
|
640
|
+
'TJK',
|
641
|
+
'THA',
|
642
|
+
'MKD',
|
643
|
+
'TLS',
|
644
|
+
'TGO',
|
645
|
+
'TKL',
|
646
|
+
'TON',
|
647
|
+
'TTO',
|
648
|
+
'TUN',
|
649
|
+
'TUR',
|
650
|
+
'TKM',
|
651
|
+
'TCA',
|
652
|
+
'TUV',
|
653
|
+
'UGA',
|
654
|
+
'UKR',
|
655
|
+
'ARE',
|
656
|
+
'GBR',
|
657
|
+
'TZA',
|
658
|
+
'USA',
|
659
|
+
'VIR',
|
660
|
+
'URY',
|
661
|
+
'UZB',
|
662
|
+
'VUT',
|
663
|
+
'VEN',
|
664
|
+
'VNM',
|
665
|
+
'WLF',
|
666
|
+
'ESH',
|
667
|
+
'YEM',
|
668
|
+
'ZMB',
|
669
|
+
'ZWE'
|
670
|
+
]
|
671
|
+
|
189
672
|
end
|
data/lib/model_un/version.rb
CHANGED
data/lib/model_un.rb
CHANGED
@@ -7,11 +7,14 @@ module ModelUN
|
|
7
7
|
def convert(string)
|
8
8
|
if string.length == 2
|
9
9
|
self.convert_state_abbr(string.upcase)
|
10
|
+
elsif string.length == 3
|
11
|
+
self.convert_nato_country_abbr(string.upcase)
|
10
12
|
else
|
11
|
-
self.
|
13
|
+
self.convert_state_or_country_name(string.upcase)
|
12
14
|
end
|
13
15
|
end
|
14
16
|
|
17
|
+
# US STATES
|
15
18
|
def convert_state_abbr(state_abbr)
|
16
19
|
self.convert_state_abbreviation(state_abbr)
|
17
20
|
end
|
@@ -26,12 +29,46 @@ module ModelUN
|
|
26
29
|
|
27
30
|
def convert_state_name(state_name)
|
28
31
|
begin
|
29
|
-
return US_STATE_ABBR[US_STATE_NAMES.index(state_name)]
|
32
|
+
return US_STATE_ABBR[US_STATE_NAMES.index(state_name.upcase)]
|
30
33
|
rescue Exception
|
31
34
|
return state_name.downcase.gsub(/\b\w/){$&.upcase}
|
32
35
|
end
|
33
36
|
end
|
34
37
|
|
38
|
+
# COUNTRIES (NATO CODES)
|
39
|
+
def convert_country_abbr(country_abbr)
|
40
|
+
self.convert_country_abbr(country_abbr)
|
41
|
+
end
|
42
|
+
|
43
|
+
def convert_nato_country_abbr(country_abbr)
|
44
|
+
begin
|
45
|
+
return NATO_COUNTRY_NAMES[NATO_ABBR.index(country_abbr)].downcase.gsub(/\b\w/){$&.upcase}
|
46
|
+
rescue Exception
|
47
|
+
return country_abbr
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def convert_country_name(country_name)
|
52
|
+
self.convert_nato_country_name(country_name)
|
53
|
+
end
|
54
|
+
|
55
|
+
def convert_nato_country_name(country_name)
|
56
|
+
begin
|
57
|
+
return NATO_ABBR[NATO_COUNTRY_NAMES.index(country_name.upcase)]
|
58
|
+
rescue Exception
|
59
|
+
return country_name.downcase.gsub(/\b\w/){$&.upcase}
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def convert_state_or_country_name(name)
|
64
|
+
state_abbr = self.convert_state_name(name)
|
65
|
+
if state_abbr.upcase == name
|
66
|
+
return self.convert_nato_country_name(name)
|
67
|
+
else
|
68
|
+
return state_abbr
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
35
72
|
def export_states_list
|
36
73
|
{'US States' => US_STATES}.to_yaml.gsub(/\n/,'').gsub(/-/, '').strip
|
37
74
|
end
|
data/spec/model_un_spec.rb
CHANGED
@@ -7,24 +7,69 @@ describe ModelUN do
|
|
7
7
|
ModelUN::US_STATE_ABBR.should_not == nil
|
8
8
|
end
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
10
|
+
describe "states" do
|
11
|
+
it "should return the input if no match on abbreviation" do
|
12
|
+
abbr = 'XX'
|
13
|
+
ModelUN.convert(abbr).should == 'XX'
|
14
|
+
ModelUN.convert_state_abbr(abbr).should == 'XX'
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should return the input if no match on full name" do
|
18
|
+
state = 'Foobar'
|
19
|
+
ModelUN.convert(state).should == state
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should return a full state for an abbreviation" do
|
24
|
+
abbr = 'CA'
|
25
|
+
ModelUN.convert(abbr).should == 'California'
|
26
|
+
ModelUN.convert_state_abbr(abbr).should == 'California'
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should return an abbreviation for a full state name" do
|
30
|
+
state = 'California'
|
31
|
+
ModelUN.convert(state).should == 'CA'
|
32
|
+
ModelUN.convert_state_name(state).should == 'CA'
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should return an abbreviation for a full state name for a state name with two words" do
|
36
|
+
state = 'New Hampshire'
|
37
|
+
ModelUN.convert(state).should == 'NH'
|
38
|
+
ModelUN.convert_state_name(state).should == 'NH'
|
39
|
+
end
|
23
40
|
end
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
41
|
+
|
42
|
+
|
43
|
+
describe "countries" do
|
44
|
+
it "should return the input if no match on country abbreviation" do
|
45
|
+
abbr = 'XX'
|
46
|
+
ModelUN.convert(abbr).should == 'XX'
|
47
|
+
ModelUN.convert_nato_country_abbr(abbr).should == 'XX'
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should return the input if no match on country full name" do
|
51
|
+
country = 'Foobar'
|
52
|
+
ModelUN.convert(country).should == 'Foobar'
|
53
|
+
ModelUN.convert_nato_country_name(country).should == 'Foobar'
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should return a full country name for an abbreviation" do
|
57
|
+
abbr = 'SWE'
|
58
|
+
ModelUN.convert(abbr).should == 'Sweden'
|
59
|
+
ModelUN.convert_nato_country_abbr(abbr).should == 'Sweden'
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should return an abbreviation for a full country name" do
|
63
|
+
country = 'Sweden'
|
64
|
+
ModelUN.convert(country).should == 'SWE'
|
65
|
+
ModelUN.convert_nato_country_name(country).should == 'SWE'
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should return an abbreviation full country name for a country name with two words" do
|
69
|
+
country = 'Czech Republic'
|
70
|
+
ModelUN.convert(country).should == 'CZE'
|
71
|
+
ModelUN.convert_nato_country_name(country).should == 'CZE'
|
72
|
+
end
|
28
73
|
end
|
29
74
|
|
30
75
|
end
|
metadata
CHANGED