snail 0.5.7 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/lib/snail.rb +55 -32
  2. data/lib/snail/constants.rb +513 -1
  3. data/test/snail_test.rb +37 -3
  4. metadata +25 -44
data/lib/snail.rb CHANGED
@@ -11,10 +11,10 @@ end
11
11
 
12
12
  class Snail
13
13
  include Configurable
14
-
14
+
15
15
  # this will be raised whenever formatting or validation is run on an unsupported or unknown country
16
16
  class UnknownCountryError < ArgumentError; end
17
-
17
+
18
18
  # My made-up standard fields.
19
19
  attr_accessor :name
20
20
  attr_accessor :line_1
@@ -23,7 +23,7 @@ class Snail
23
23
  attr_accessor :region
24
24
  attr_accessor :postal_code
25
25
  attr_accessor :country
26
-
26
+
27
27
  # Aliases for easier assignment compatibility
28
28
  {
29
29
  :full_name => :name,
@@ -39,64 +39,86 @@ class Snail
39
39
  end
40
40
 
41
41
  def self.home_country
42
- @home_country ||= "USA"
42
+ @home_country ||= "US"
43
43
  end
44
44
 
45
45
  def self.home_country=(val)
46
- @home_country = val
46
+ @home_country = lookup_country_iso(val)
47
+ end
48
+
49
+ def self.lookup_country_iso(val)
50
+ return nil if val.blank?
51
+ val = val.upcase
52
+ if ::Snail::Iso3166::ALPHA2[val]
53
+ val
54
+ elsif iso = ::Snail::Iso3166::ALPHA2_EXCEPTIONS[val]
55
+ iso
56
+ elsif iso = ::Snail::Iso3166::ALPHA3_TO_ALPHA2[val]
57
+ iso
58
+ elsif iso_pair = ::Snail::Iso3166::ALPHA2.find { |a2, names| names.include?(val) }
59
+ ActiveSupport::Deprecation.warn("Country name lookup will be deprecated in the near future. Please pass ISO3166 country codes to Snail instead.", caller)
60
+ iso_pair.first
61
+ else
62
+ nil
63
+ end
47
64
  end
48
-
65
+
66
+ # Store country as ISO-3166 Alpha 2
67
+ def country=(val)
68
+ @country = Snail.lookup_country_iso(val)
69
+ end
70
+
49
71
  def to_s
50
72
  [name, line_1, line_2, city_line, country_line].select{|line| !(line.nil? or line.empty?)}.join("\n")
51
73
  end
52
-
53
- def to_html
74
+
75
+ def to_html
54
76
  "<address>#{CGI.escapeHTML(to_s).gsub("\n", '<br />')}</address>".html_safe
55
77
  end
56
-
78
+
57
79
  # this method will get much larger. completeness is out of my scope at this time.
58
80
  # currently it's based on the sampling of city line formats from frank's compulsive guide.
59
81
  def city_line
60
82
  case country
61
- when 'China', 'India'
83
+ when 'CN', 'IN'
62
84
  "#{city}, #{region} #{postal_code}"
63
- when 'Brazil'
85
+ when 'BR'
64
86
  "#{postal_code} #{city}-#{region}"
65
- when 'Mexico', 'Slovakia'
87
+ when 'MX', 'SK'
66
88
  "#{postal_code} #{city}, #{region}"
67
- when 'Italy'
89
+ when 'IT'
68
90
  "#{postal_code} #{city} (#{region})"
69
- when 'Belarus'
91
+ when 'BY'
70
92
  "#{postal_code} #{city}-(#{region})"
71
- when 'USA', 'Canada', 'Australia', nil, ""
93
+ when 'US', 'CA', 'AU', nil, ""
72
94
  "#{city} #{region} #{postal_code}"
73
- when 'Israel', 'Denmark', 'Finland', 'France', 'Germany', 'Greece', 'Italy', 'Norway', 'Spain', 'Sweden', 'Turkey', 'Cyprus', 'Portugal', 'Macedonia', 'Bosnia and Herzegovina'
95
+ when 'IL', 'DK', 'FI', 'FR', 'DE', 'GR', 'IT', 'NO', 'ES', 'SE', 'TR', 'CY', 'PT', 'MK', 'BA'
74
96
  "#{postal_code} #{city}"
75
- when 'Kuwait', 'Syria', 'Oman', 'Estonia','Luxembourg', 'Belgium', 'Iceland', 'Switzerland', 'Austria', 'Moldova', 'Montenegro', 'Serbia', 'Bulgaria', 'Georgia', 'Poland', 'Armenia', 'Croatia', 'Romania', 'Azerbaijan'
97
+ when 'KW', 'SY', 'OM', 'EE', 'LU', 'BE', 'IS', 'CH', 'AT', 'MD', 'ME', 'RS', 'BG', 'GE', 'PL', 'AM', 'HR', 'RO', 'AZ'
76
98
  "#{postal_code} #{city}"
77
- when 'Netherlands'
78
- "#{postal_code} #{region} #{city}"
79
- when 'Ireland'
99
+ when 'NL'
100
+ "#{postal_code} #{city}"
101
+ when 'IE'
80
102
  "#{city}, #{region}"
81
- when 'England', 'Scotland', 'Wales', 'United Kingdom', 'Russia', 'Russian Federation', 'Ukraine', 'Jordan', 'Lebanon','Iran, Islamic Republic of', 'Iran', 'Saudi Arabia', 'New Zealand'
103
+ when 'GB', 'RU', 'UA', 'JO', 'LB', 'IR', 'SA', 'NZ'
82
104
  "#{city} #{postal_code}" # Locally these may be on separate lines. The USPS prefers the city line above the country line, though.
83
- when 'Ecuador'
105
+ when 'EC'
84
106
  "#{postal_code} #{city}"
85
- when 'Hong Kong', 'Syria', 'Iraq', 'Yemen', 'Qatar', 'Albania'
107
+ when 'HK', 'IQ', 'YE', 'QA', 'AL'
86
108
  "#{city}"
87
- when 'United Arab Emirates'
109
+ when 'AE'
88
110
  "#{postal_code}\n#{city}"
89
- when 'Japan'
111
+ when 'JP'
90
112
  "#{city}, #{region}\n#{postal_code}"
91
- when 'Egypt', 'South Africa','Isle of Man', 'Kazakhstan', 'Hungary'
113
+ when 'EG', 'ZA', 'IM', 'KZ', 'HU'
92
114
  "#{city}\n#{postal_code}"
93
- when 'Latvia'
115
+ when 'LV'
94
116
  "#{city}, LV-#{postal_code}"
95
- when 'Lithuania'
117
+ when 'LT'
96
118
  "LT-#{postal_code} #{city}"
97
- when 'Slovenia'
119
+ when 'SI'
98
120
  "SI-#{postal_code} #{city}"
99
- when 'Czech Republic'
121
+ when 'CZ'
100
122
  "#{postal_code} #{region}\n#{city}"
101
123
  else
102
124
  if Kernel.const_defined?("Rails")
@@ -105,8 +127,9 @@ class Snail
105
127
  "#{city} #{region} #{postal_code}"
106
128
  end
107
129
  end
108
-
130
+
131
+ # TODO after country name lookup deprecation, add localized country names to this
109
132
  def country_line
110
- self.class.home_country.to_s.upcase == country.to_s.upcase ? nil : country.to_s.upcase
133
+ ::Snail::Iso3166::ALPHA2[country].first if country and self.class.home_country != country
111
134
  end
112
135
  end
@@ -112,6 +112,7 @@ class Snail
112
112
  'Kenya',
113
113
  'Kiribati',
114
114
  'Korea',
115
+ 'Kosovo',
115
116
  'Kuwait',
116
117
  'Kyrgyzstan',
117
118
  'Laos',
@@ -172,7 +173,7 @@ class Snail
172
173
  'Republic of the Congo',
173
174
  'Reunion',
174
175
  'Romania',
175
- 'Russia',
176
+ 'Russian Federation',
176
177
  'Rwanda',
177
178
  'Saint Helena',
178
179
  'Saint Kitts and Nevis',
@@ -194,6 +195,7 @@ class Snail
194
195
  'Somalia',
195
196
  'South Africa',
196
197
  'South Georgia',
198
+ 'South Sudan',
197
199
  'Spain',
198
200
  'Sri Lanka',
199
201
  'Sudan',
@@ -333,6 +335,7 @@ class Snail
333
335
  "Armed Forces Europe" => "AE",
334
336
  "Armed Forces Middle East" => "AE",
335
337
  "Armed Forces Pacific" => "AP",
338
+ "Australia" => "AQ"
336
339
  }
337
340
  }
338
341
 
@@ -342,5 +345,514 @@ class Snail
342
345
  CA_PROVINCES = REGIONS[:ca]
343
346
  AU_REGIONS = REGIONS[:au]
344
347
 
348
+ module Iso3166
349
+ # as of June 3rd, 2010
350
+ # pulled http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
351
+ ALPHA2 = {
352
+ 'AD' => ["ANDORRA"],
353
+ 'AE' => ["UNITED ARAB EMIRATES"],
354
+ 'AF' => ["AFGHANISTAN"],
355
+ 'AG' => ["ANTIGUA AND BARBUDA"],
356
+ 'AI' => ["ANGUILLA"],
357
+ 'AL' => ["ALBANIA"],
358
+ 'AM' => ["ARMENIA"],
359
+ 'AN' => ["NETHERLANDS ANTILLES"],
360
+ 'AO' => ["ANGOLA"],
361
+ 'AQ' => ["ANTARCTICA"],
362
+ 'AR' => ["ARGENTINA"],
363
+ 'AS' => ["AMERICAN SAMOA"],
364
+ 'AT' => ["AUSTRIA"],
365
+ 'AU' => ["AUSTRALIA"],
366
+ 'AW' => ["ARUBA"],
367
+ 'AX' => ["ALAND ISLANDS"],
368
+ 'AZ' => ["AZERBAIJAN"],
369
+ 'BA' => ["BOSNIA AND HERZEGOVINA"],
370
+ 'BB' => ["BARBADOS"],
371
+ 'BD' => ["BANGLADESH"],
372
+ 'BE' => ["BELGIUM"],
373
+ 'BF' => ["BURKINA FASO"],
374
+ 'BG' => ["BULGARIA"],
375
+ 'BH' => ["BAHRAIN"],
376
+ 'BI' => ["BURUNDI"],
377
+ 'BJ' => ["BENIN"],
378
+ 'BL' => ["SAINT BARTHELEMY"],
379
+ 'BM' => ["BERMUDA"],
380
+ 'BN' => ["BRUNEI DARUSSALAM"],
381
+ 'BO' => ["BOLIVIA"],
382
+ 'BR' => ["BRAZIL"],
383
+ 'BS' => ["BAHAMAS"],
384
+ 'BT' => ["BHUTAN"],
385
+ 'BV' => ["BOUVET ISLAND"],
386
+ 'BW' => ["BOTSWANA"],
387
+ 'BY' => ["BELARUS"],
388
+ 'BZ' => ["BELIZE"],
389
+ 'CA' => ["CANADA"],
390
+ 'CC' => ["COCOS (KEELING) ISLANDS"],
391
+ 'CD' => ["DEMOCRATIC REPUBLIC OF THE CONGO"],
392
+ 'CF' => ["CENTRAL AFRICAN REPUBLIC"],
393
+ 'CG' => ["CONGO", "REPUBLIC OF THE CONGO"],
394
+ 'CH' => ["SWITZERLAND"],
395
+ 'CI' => ["COTE D'IVOIRE"],
396
+ 'CK' => ["COOK ISLANDS"],
397
+ 'CL' => ["CHILE"],
398
+ 'CM' => ["CAMEROON"],
399
+ 'CN' => ["CHINA", "PEOPLE'S REPUBLIC OF CHINA"],
400
+ 'CO' => ["COLOMBIA"],
401
+ 'CR' => ["COSTA RICA"],
402
+ 'CU' => ["CUBA"],
403
+ 'CV' => ["CAPE VERDE"],
404
+ 'CX' => ["CHRISTMAS ISLAND"],
405
+ 'CY' => ["CYPRUS"],
406
+ 'CZ' => ["CZECH REPUBLIC"],
407
+ 'DE' => ["GERMANY"],
408
+ 'DJ' => ["DJIBOUTI"],
409
+ 'DK' => ["DENMARK"],
410
+ 'DM' => ["DOMINICA"],
411
+ 'DO' => ["DOMINICAN REPUBLIC"],
412
+ 'DZ' => ["ALGERIA"],
413
+ 'EC' => ["ECUADOR"],
414
+ 'EE' => ["ESTONIA"],
415
+ 'EG' => ["EGYPT"],
416
+ 'EH' => ["WESTERN SAHARA"],
417
+ 'ER' => ["ERITREA"],
418
+ 'ES' => ["SPAIN"],
419
+ 'ET' => ["ETHIOPIA"],
420
+ 'FI' => ["FINLAND"],
421
+ 'FJ' => ["FIJI"],
422
+ 'FK' => ["FALKLAND ISLANDS"],
423
+ 'FM' => ["MICRONESIA"],
424
+ 'FO' => ["FAROE ISLANDS"],
425
+ 'FR' => ["FRANCE"],
426
+ 'GA' => ["GABON"],
427
+ 'GB' => ["UNITED KINGDOM", "ENGLAND", "NORTHERN IRELAND", "SCOTLAND", "WALES"],
428
+ 'GD' => ["GRENADA"],
429
+ 'GE' => ["GEORGIA"],
430
+ 'GF' => ["FRENCH GUIANA"],
431
+ 'GG' => ["GUERNSEY"],
432
+ 'GH' => ["GHANA"],
433
+ 'GI' => ["GIBRALTAR"],
434
+ 'GL' => ["GREENLAND"],
435
+ 'GM' => ["GAMBIA"],
436
+ 'GN' => ["GUINEA"],
437
+ 'GP' => ["GUADELOUPE"],
438
+ 'GQ' => ["EQUATORIAL GUINEA"],
439
+ 'GR' => ["GREECE"],
440
+ 'GS' => ["SOUTH GEORGIA AND THE SOUTH SANDWICH ISLANDS"],
441
+ 'GT' => ["GUATEMALA"],
442
+ 'GU' => ["GUAM"],
443
+ 'GW' => ["GUINEA-BISSAU"],
444
+ 'GY' => ["GUYANA"],
445
+ 'HK' => ["HONG KONG"],
446
+ 'HM' => ["HEARD ISLAND AND MCDONALD ISLANDS"],
447
+ 'HN' => ["HONDURAS"],
448
+ 'HR' => ["CROATIA"],
449
+ 'HT' => ["HAITI"],
450
+ 'HU' => ["HUNGARY"],
451
+ 'ID' => ["INDONESIA"],
452
+ 'IE' => ["IRELAND"],
453
+ 'IL' => ["ISRAEL"],
454
+ 'IM' => ["ISLE OF MAN"],
455
+ 'IN' => ["INDIA"],
456
+ 'IO' => ["BRITISH INDIAN OCEAN TERRITORY"],
457
+ 'IQ' => ["IRAQ"],
458
+ 'IR' => ["IRAN", "ISLAMIC REPUBLIC OF IRAN"],
459
+ 'IS' => ["ICELAND"],
460
+ 'IT' => ["ITALY"],
461
+ 'JE' => ["JERSEY"],
462
+ 'JM' => ["JAMAICA"],
463
+ 'JO' => ["JORDAN"],
464
+ 'JP' => ["JAPAN"],
465
+ 'KE' => ["KENYA"],
466
+ 'KG' => ["KYRGYZSTAN"],
467
+ 'KH' => ["CAMBODIA"],
468
+ 'KI' => ["KIRIBATI"],
469
+ 'KM' => ["COMOROS"],
470
+ 'KN' => ["SAINT KITTS AND NEVIS"],
471
+ 'KP' => ["NORTH KOREA", "DEMOCRATIC PEOPLE'S REPUBLIC OF KOREA"],
472
+ 'KR' => ["SOUTH KOREA", "KOREA", "REPUBLIC OF KOREA"],
473
+ 'KW' => ["KUWAIT"],
474
+ 'KY' => ["CAYMAN ISLANDS"],
475
+ 'KZ' => ["KAZAKHSTAN"],
476
+ 'LA' => ["LAOS"],
477
+ 'LB' => ["LEBANON"],
478
+ 'LC' => ["SAINT LUCIA"],
479
+ 'LI' => ["LIECHTENSTEIN"],
480
+ 'LK' => ["SRI LANKA"],
481
+ 'LR' => ["LIBERIA"],
482
+ 'LS' => ["LESOTHO"],
483
+ 'LT' => ["LITHUANIA"],
484
+ 'LU' => ["LUXEMBOURG"],
485
+ 'LV' => ["LATVIA"],
486
+ 'LY' => ["LIBYAN ARAB JAMAHIRIYA"],
487
+ 'MA' => ["MOROCCO"],
488
+ 'MC' => ["MONACO"],
489
+ 'MD' => ["MOLDOVA"],
490
+ 'ME' => ["MONTENEGRO"],
491
+ 'MF' => ["SAINT MARTIN"],
492
+ 'MG' => ["MADAGASCAR"],
493
+ 'MH' => ["MARSHALL ISLANDS"],
494
+ 'MK' => ["MACEDONIA"],
495
+ 'ML' => ["MALI"],
496
+ 'MM' => ["MYANMAR"],
497
+ 'MN' => ["MONGOLIA"],
498
+ 'MO' => ["MACAO"],
499
+ 'MP' => ["NORTHERN MARIANA ISLANDS"],
500
+ 'MQ' => ["MARTINIQUE"],
501
+ 'MR' => ["MAURITANIA"],
502
+ 'MS' => ["MONTSERRAT"],
503
+ 'MT' => ["MALTA"],
504
+ 'MU' => ["MAURITIUS"],
505
+ 'MV' => ["MALDIVES"],
506
+ 'MW' => ["MALAWI"],
507
+ 'MX' => ["MEXICO"],
508
+ 'MY' => ["MALAYSIA"],
509
+ 'MZ' => ["MOZAMBIQUE"],
510
+ 'NA' => ["NAMIBIA"],
511
+ 'NC' => ["NEW CALEDONIA"],
512
+ 'NE' => ["NIGER"],
513
+ 'NF' => ["NORFOLK ISLAND"],
514
+ 'NG' => ["NIGERIA"],
515
+ 'NI' => ["NICARAGUA"],
516
+ 'NL' => ["NETHERLANDS"],
517
+ 'NO' => ["NORWAY"],
518
+ 'NP' => ["NEPAL"],
519
+ 'NR' => ["NAURU"],
520
+ 'NU' => ["NIUE"],
521
+ 'NZ' => ["NEW ZEALAND"],
522
+ 'OM' => ["OMAN"],
523
+ 'PA' => ["PANAMA"],
524
+ 'PE' => ["PERU"],
525
+ 'PF' => ["FRENCH POLYNESIA"],
526
+ 'PG' => ["PAPUA NEW GUINEA"],
527
+ 'PH' => ["PHILIPPINES"],
528
+ 'PK' => ["PAKISTAN"],
529
+ 'PL' => ["POLAND"],
530
+ 'PM' => ["SAINT PIERRE AND MIQUELON"],
531
+ 'PN' => ["PITCAIRN"],
532
+ 'PR' => ["PUERTO RICO"],
533
+ 'PS' => ["PALESTINIAN TERRITORIES"],
534
+ 'PT' => ["PORTUGAL"],
535
+ 'PW' => ["PALAU"],
536
+ 'PY' => ["PARAGUAY"],
537
+ 'QA' => ["QATAR"],
538
+ 'RE' => ["REUNION"],
539
+ 'RO' => ["ROMANIA"],
540
+ 'RS' => ["SERBIA"],
541
+ 'RU' => ["RUSSIAN FEDERATION", "RUSSIA"],
542
+ 'RW' => ["RWANDA"],
543
+ 'SA' => ["SAUDI ARABIA"],
544
+ 'SB' => ["SOLOMON ISLANDS"],
545
+ 'SC' => ["SEYCHELLES"],
546
+ 'SD' => ["SUDAN"],
547
+ 'SE' => ["SWEDEN"],
548
+ 'SG' => ["SINGAPORE"],
549
+ 'SH' => ["SAINT HELENA"],
550
+ 'SI' => ["SLOVENIA"],
551
+ 'SJ' => ["SVALBARD AND JAN MAYEN"],
552
+ 'SK' => ["SLOVAKIA"],
553
+ 'SL' => ["SIERRA LEONE"],
554
+ 'SM' => ["SAN MARINO"],
555
+ 'SN' => ["SENEGAL"],
556
+ 'SO' => ["SOMALIA"],
557
+ 'SR' => ["SURINAME"],
558
+ 'SS' => ["SOUTH SUDAN"],
559
+ 'ST' => ["SAO TOME AND PRINCIPE"],
560
+ 'SV' => ["EL SALVADOR"],
561
+ 'SY' => ["SYRIAN ARAB REPUBLIC", "SYRIA"],
562
+ 'SZ' => ["SWAZILAND"],
563
+ 'TC' => ["TURKS AND CAICOS ISLANDS"],
564
+ 'TD' => ["CHAD"],
565
+ 'TF' => ["FRENCH SOUTHERN TERRITORIES"],
566
+ 'TG' => ["TOGO"],
567
+ 'TH' => ["THAILAND"],
568
+ 'TJ' => ["TAJIKISTAN"],
569
+ 'TK' => ["TOKELAU"],
570
+ 'TL' => ["TIMOR-LESTE"],
571
+ 'TM' => ["TURKMENISTAN"],
572
+ 'TN' => ["TUNISIA"],
573
+ 'TO' => ["TONGA"],
574
+ 'TR' => ["TURKEY"],
575
+ 'TT' => ["TRINIDAD AND TOBAGO"],
576
+ 'TV' => ["TUVALU"],
577
+ 'TW' => ["TAIWAN"],
578
+ 'TZ' => ["TANZANIA"],
579
+ 'UA' => ["UKRAINE"],
580
+ 'UG' => ["UGANDA"],
581
+ 'UM' => ["UNITED STATES MINOR OUTLYING ISLANDS"],
582
+ 'US' => ["UNITED STATES", "UNITED STATES OF AMERICA"],
583
+ 'UY' => ["URUGUAY"],
584
+ 'UZ' => ["UZBEKISTAN"],
585
+ 'VA' => ["VATICAN CITY"],
586
+ 'VC' => ["SAINT VINCENT AND THE GRENADINES"],
587
+ 'VE' => ["VENEZUELA"],
588
+ 'VG' => ["VIRGIN ISLANDS, BRITISH"],
589
+ 'VI' => ["VIRGIN ISLANDS, U.S."],
590
+ 'VN' => ["VIET NAM"],
591
+ 'VU' => ["VANUATU"],
592
+ 'WF' => ["WALLIS AND FUTUNA"],
593
+ 'WS' => ["SAMOA"],
594
+ 'YE' => ["YEMEN"],
595
+ 'YT' => ["MAYOTTE"],
596
+ 'ZA' => ["SOUTH AFRICA"],
597
+ 'ZM' => ["ZAMBIA"],
598
+ 'ZW' => ["ZIMBABWE"]
599
+ }
600
+
601
+ ALPHA2_EXCEPTIONS = {
602
+ 'UK' => 'GB', # great britain aka united kingdom
603
+ 'FX' => 'FR', # france
604
+ 'SU' => 'RU' # russia
605
+ }
606
+
607
+ ALPHA3_TO_ALPHA2 = {
608
+ "ABW" => "AW",
609
+ "AFG" => "AF",
610
+ "AGO" => "AO",
611
+ "AIA" => "AI",
612
+ "ALA" => "AX",
613
+ "ALB" => "AL",
614
+ "AND" => "AD",
615
+ "ANT" => "AN",
616
+ "ARE" => "AE",
617
+ "ARG" => "AR",
618
+ "ARM" => "AM",
619
+ "ASM" => "AS",
620
+ "ATA" => "AQ",
621
+ "ATF" => "TF",
622
+ "ATG" => "AG",
623
+ "AUS" => "AU",
624
+ "AUT" => "AT",
625
+ "AZE" => "AZ",
626
+ "BDI" => "BI",
627
+ "BEL" => "BE",
628
+ "BEN" => "BJ",
629
+ "BFA" => "BF",
630
+ "BGD" => "BD",
631
+ "BGR" => "BG",
632
+ "BHR" => "BH",
633
+ "BHS" => "BS",
634
+ "BIH" => "BA",
635
+ "BLM" => "BL",
636
+ "BLR" => "BY",
637
+ "BLZ" => "BZ",
638
+ "BMU" => "BM",
639
+ "BOL" => "BO",
640
+ "BRA" => "BR",
641
+ "BRB" => "BB",
642
+ "BRN" => "BN",
643
+ "BTN" => "BT",
644
+ "BVT" => "BV",
645
+ "BWA" => "BW",
646
+ "CAF" => "CF",
647
+ "CAN" => "CA",
648
+ "CCK" => "CC",
649
+ "CHE" => "CH",
650
+ "CHL" => "CL",
651
+ "CHN" => "CN",
652
+ "CIV" => "CI",
653
+ "CMR" => "CM",
654
+ "COD" => "CD",
655
+ "COG" => "CG",
656
+ "COK" => "CK",
657
+ "COL" => "CO",
658
+ "COM" => "KM",
659
+ "CPV" => "CV",
660
+ "CRI" => "CR",
661
+ "CUB" => "CU",
662
+ "CXR" => "CX",
663
+ "CYM" => "KY",
664
+ "CYP" => "CY",
665
+ "CZE" => "CZ",
666
+ "DEU" => "DE",
667
+ "DJI" => "DJ",
668
+ "DMA" => "DM",
669
+ "DNK" => "DK",
670
+ "DOM" => "DO",
671
+ "DZA" => "DZ",
672
+ "ECU" => "EC",
673
+ "EGY" => "EG",
674
+ "ERI" => "ER",
675
+ "ESH" => "EH",
676
+ "ESP" => "ES",
677
+ "EST" => "EE",
678
+ "ETH" => "ET",
679
+ "FIN" => "FI",
680
+ "FJI" => "FJ",
681
+ "FLK" => "FK",
682
+ "FRA" => "FR",
683
+ "FRO" => "FO",
684
+ "FSM" => "FM",
685
+ "GAB" => "GA",
686
+ "GBR" => "GB",
687
+ "GEO" => "GE",
688
+ "GGY" => "GG",
689
+ "GHA" => "GH",
690
+ "GIB" => "GI",
691
+ "GIN" => "GN",
692
+ "GLP" => "GP",
693
+ "GMB" => "GM",
694
+ "GNB" => "GW",
695
+ "GNQ" => "GQ",
696
+ "GRC" => "GR",
697
+ "GRD" => "GD",
698
+ "GRL" => "GL",
699
+ "GTM" => "GT",
700
+ "GUF" => "GF",
701
+ "GUM" => "GU",
702
+ "GUY" => "GY",
703
+ "HKG" => "HK",
704
+ "HMD" => "HM",
705
+ "HND" => "HN",
706
+ "HRV" => "HR",
707
+ "HTI" => "HT",
708
+ "HUN" => "HU",
709
+ "IDN" => "ID",
710
+ "IMN" => "IM",
711
+ "IND" => "IN",
712
+ "IOT" => "IO",
713
+ "IRL" => "IE",
714
+ "IRN" => "IR",
715
+ "IRQ" => "IQ",
716
+ "ISL" => "IS",
717
+ "ISR" => "IL",
718
+ "ITA" => "IT",
719
+ "JAM" => "JM",
720
+ "JEY" => "JE",
721
+ "JOR" => "JO",
722
+ "JPN" => "JP",
723
+ "KAZ" => "KZ",
724
+ "KEN" => "KE",
725
+ "KGZ" => "KG",
726
+ "KHM" => "KH",
727
+ "KIR" => "KI",
728
+ "KNA" => "KN",
729
+ "KOR" => "KR",
730
+ "KWT" => "KW",
731
+ "LAO" => "LA",
732
+ "LBN" => "LB",
733
+ "LBR" => "LR",
734
+ "LBY" => "LY",
735
+ "LCA" => "LC",
736
+ "LIE" => "LI",
737
+ "LKA" => "LK",
738
+ "LSO" => "LS",
739
+ "LTU" => "LT",
740
+ "LUX" => "LU",
741
+ "LVA" => "LV",
742
+ "MAC" => "MO",
743
+ "MAF" => "MF",
744
+ "MAR" => "MA",
745
+ "MCO" => "MC",
746
+ "MDA" => "MD",
747
+ "MDG" => "MG",
748
+ "MDV" => "MV",
749
+ "MEX" => "MX",
750
+ "MHL" => "MH",
751
+ "MKD" => "MK",
752
+ "MLI" => "ML",
753
+ "MLT" => "MT",
754
+ "MMR" => "MM",
755
+ "MNE" => "ME",
756
+ "MNG" => "MN",
757
+ "MNP" => "MP",
758
+ "MOZ" => "MZ",
759
+ "MRT" => "MR",
760
+ "MSR" => "MS",
761
+ "MTQ" => "MQ",
762
+ "MUS" => "MU",
763
+ "MWI" => "MW",
764
+ "MYS" => "MY",
765
+ "MYT" => "YT",
766
+ "NAM" => "NA",
767
+ "NCL" => "NC",
768
+ "NER" => "NE",
769
+ "NFK" => "NF",
770
+ "NGA" => "NG",
771
+ "NIC" => "NI",
772
+ "NIU" => "NU",
773
+ "NLD" => "NL",
774
+ "NOR" => "NO",
775
+ "NPL" => "NP",
776
+ "NRU" => "NR",
777
+ "NZL" => "NZ",
778
+ "OMN" => "OM",
779
+ "PAK" => "PK",
780
+ "PAN" => "PA",
781
+ "PCN" => "PN",
782
+ "PER" => "PE",
783
+ "PHL" => "PH",
784
+ "PLW" => "PW",
785
+ "PNG" => "PG",
786
+ "POL" => "PL",
787
+ "PRI" => "PR",
788
+ "PRK" => "KP",
789
+ "PRT" => "PT",
790
+ "PRY" => "PY",
791
+ "PSE" => "PS",
792
+ "PYF" => "PF",
793
+ "QAT" => "QA",
794
+ "REU" => "RE",
795
+ "ROU" => "RO",
796
+ "RUS" => "RU",
797
+ "RWA" => "RW",
798
+ "SAU" => "SA",
799
+ "SDN" => "SD",
800
+ "SEN" => "SN",
801
+ "SGP" => "SG",
802
+ "SGS" => "GS",
803
+ "SHN" => "SH",
804
+ "SJM" => "SJ",
805
+ "SLB" => "SB",
806
+ "SLE" => "SL",
807
+ "SLV" => "SV",
808
+ "SMR" => "SM",
809
+ "SOM" => "SO",
810
+ "SPM" => "PM",
811
+ "SRB" => "RS",
812
+ "SSD" => "SS",
813
+ "STP" => "ST",
814
+ "SUR" => "SR",
815
+ "SVK" => "SK",
816
+ "SVN" => "SI",
817
+ "SWE" => "SE",
818
+ "SWZ" => "SZ",
819
+ "SYC" => "SC",
820
+ "SYR" => "SY",
821
+ "TCA" => "TC",
822
+ "TCD" => "TD",
823
+ "TGO" => "TG",
824
+ "THA" => "TH",
825
+ "TJK" => "TJ",
826
+ "TKL" => "TK",
827
+ "TKM" => "TM",
828
+ "TLS" => "TL",
829
+ "TON" => "TO",
830
+ "TTO" => "TT",
831
+ "TUN" => "TN",
832
+ "TUR" => "TR",
833
+ "TUV" => "TV",
834
+ "TWN" => "TW",
835
+ "TZA" => "TZ",
836
+ "UGA" => "UG",
837
+ "UKR" => "UA",
838
+ "UMI" => "UM",
839
+ "URY" => "UY",
840
+ "USA" => "US",
841
+ "UZB" => "UZ",
842
+ "VAT" => "VA",
843
+ "VCT" => "VC",
844
+ "VEN" => "VE",
845
+ "VGB" => "VG",
846
+ "VIR" => "VI",
847
+ "VNM" => "VN",
848
+ "VUT" => "VU",
849
+ "WLF" => "WF",
850
+ "WSM" => "WS",
851
+ "YEM" => "YE",
852
+ "ZAF" => "ZA",
853
+ "ZMB" => "ZM",
854
+ "ZWE" => "ZW"
855
+ }
856
+ end
345
857
 
346
858
  end
data/test/snail_test.rb CHANGED
@@ -3,7 +3,7 @@ require File.dirname(__FILE__) + '/test_helper'
3
3
  class SnailTest < ActiveSupport::TestCase
4
4
  def setup
5
5
  @us = {:name => "John Doe", :line_1 => "12345 5th St", :city => "Somewheres", :state => "NY", :zip => "12345", :country => 'USA'}
6
- @ca = {:name => "John Doe", :line_1 => "12345 5th St", :city => "Somewheres", :state => "NY", :zip => "12345", :country => 'Canada'}
6
+ @ca = {:name => "John Doe", :line_1 => "12345 5th St", :city => "Somewheres", :state => "NY", :zip => "12345", :country => 'CAN'}
7
7
  end
8
8
 
9
9
  test "provides USPS country names" do
@@ -50,6 +50,35 @@ class SnailTest < ActiveSupport::TestCase
50
50
  assert_equal Snail.new(:postcode => "12345").postal_code, Snail.new(:postal_code => "12345").postal_code
51
51
  end
52
52
 
53
+ ##
54
+ ## Country Normalization
55
+ ##
56
+
57
+ test "normalize country name to alpha2" do
58
+ s = Snail.new(@ca.merge(:country => 'Slovenia'))
59
+ assert_equal "SI", s.country
60
+ end
61
+
62
+ test "normalize uppercase country name to alpha2" do
63
+ s = Snail.new(@ca.merge(:country => 'JAPAN'))
64
+ assert_equal "JP", s.country
65
+ end
66
+
67
+ test "normalize alpha3 to alpha2" do
68
+ s = Snail.new(@ca.merge(:country => 'SVN'))
69
+ assert_equal "SI", s.country
70
+ end
71
+
72
+ test "normalize alpha2 exceptions to alpha2" do
73
+ s = Snail.new(@ca.merge(:country => 'UK'))
74
+ assert_equal "GB", s.country
75
+ end
76
+
77
+ test "leave alpha2 as alpha2" do
78
+ s = Snail.new(@ca.merge(:country => 'GB'))
79
+ assert_equal "GB", s.country
80
+ end
81
+
53
82
  ##
54
83
  ## Formatting
55
84
  ##
@@ -73,7 +102,12 @@ class SnailTest < ActiveSupport::TestCase
73
102
 
74
103
  test "includes country name for international addresses" do
75
104
  s = Snail.new(@ca)
76
- assert s.to_s.match(/Canada/i)
105
+ assert s.to_s.match(/Canada\Z/i), s.to_s
106
+ end
107
+
108
+ test "includes first country name for countries with many commonly used names" do
109
+ s = Snail.new(@ca.merge(:country => 'UK'))
110
+ assert s.to_s.match(/United Kingdom\Z/i), s.to_s
77
111
  end
78
112
 
79
113
  test "output ok if country is nil" do
@@ -83,7 +117,7 @@ class SnailTest < ActiveSupport::TestCase
83
117
 
84
118
  test "country names are uppercased" do
85
119
  s = Snail.new(@ca)
86
- assert s.to_s.match(/CANADA/)
120
+ assert s.to_s.match(/CANADA/), s.to_s
87
121
  end
88
122
 
89
123
  test "empty lines are removed" do
metadata CHANGED
@@ -1,76 +1,57 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: snail
3
- version: !ruby/object:Gem::Version
4
- hash: 5
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 5
9
- - 7
10
- version: 0.5.7
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.6.0
5
+ prerelease:
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Lance Ivy
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-08-23 00:00:00 -07:00
19
- default_executable:
12
+ date: 2012-07-09 00:00:00.000000000 Z
20
13
  dependencies: []
21
-
22
- description: International snail mail addressing is a pain. This begins to make it easier.
14
+ description: International snail mail addressing is a pain. This begins to make it
15
+ easier.
23
16
  email: lance@cainlevy.net
24
17
  executables: []
25
-
26
18
  extensions: []
27
-
28
19
  extra_rdoc_files: []
29
-
30
- files:
31
- - lib/snail/constants.rb
20
+ files:
32
21
  - lib/snail/configurable.rb
22
+ - lib/snail/constants.rb
33
23
  - lib/snail.rb
34
24
  - lib/snail_helpers.rb
35
- - test/test_helper.rb
36
25
  - test/snail_test.rb
26
+ - test/test_helper.rb
37
27
  - README.rdoc
38
28
  - MIT-LICENSE
39
- has_rdoc: true
40
29
  homepage: http://github.com/cainlevy/snail
41
30
  licenses: []
42
-
43
31
  post_install_message:
44
- rdoc_options:
32
+ rdoc_options:
45
33
  - --title
46
34
  - Snail
47
35
  - --line-numbers
48
- require_paths:
36
+ require_paths:
49
37
  - lib
50
- required_ruby_version: !ruby/object:Gem::Requirement
38
+ required_ruby_version: !ruby/object:Gem::Requirement
51
39
  none: false
52
- requirements:
53
- - - ">="
54
- - !ruby/object:Gem::Version
55
- hash: 3
56
- segments:
57
- - 0
58
- version: "0"
59
- required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
45
  none: false
61
- requirements:
62
- - - ">="
63
- - !ruby/object:Gem::Version
64
- hash: 3
65
- segments:
66
- - 0
67
- version: "0"
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
68
50
  requirements: []
69
-
70
51
  rubyforge_project:
71
- rubygems_version: 1.3.7
52
+ rubygems_version: 1.8.11
72
53
  signing_key:
73
54
  specification_version: 3
74
55
  summary: Easily format snail mail addresses for international delivery
75
56
  test_files: []
76
-
57
+ has_rdoc: true