postoffice 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/COUNTRYCODES.md +161 -0
- data/LICENSE +6 -0
- data/README.md +50 -0
- data/Rakefile +24 -0
- data/init.rb +2 -0
- data/lib/postoffice.rb +189 -0
- data/test/postcode_validator_test.rb +95 -0
- data/test/test_helper.rb +53 -0
- metadata +57 -0
data/COUNTRYCODES.md
ADDED
@@ -0,0 +1,161 @@
|
|
1
|
+
Postoffice
|
2
|
+
==========
|
3
|
+
|
4
|
+
2 digit ruby symbols are used to specify a country. See README.md for usage details.
|
5
|
+
|
6
|
+
Countries with supported postcodes:
|
7
|
+
:us => "United States"
|
8
|
+
:ca => "Canada"
|
9
|
+
:gb => "United Kingdom (Great Britain)"
|
10
|
+
:cn => "China"
|
11
|
+
:jp => "Japan"
|
12
|
+
:de => "Germany"
|
13
|
+
:fr => "France"
|
14
|
+
:au => "Australia"
|
15
|
+
:it => "Italy"
|
16
|
+
:ch => "Switzerland"
|
17
|
+
:at => "Austria"
|
18
|
+
:es => "Spain"
|
19
|
+
:nl => "Netherlands"
|
20
|
+
:be => "Belgium"
|
21
|
+
:dk => "Denmark"
|
22
|
+
:se => "Sweden"
|
23
|
+
:no => "Norway"
|
24
|
+
:br => "Brazil"
|
25
|
+
:pt => "Portugal"
|
26
|
+
:fi => "Finaland"
|
27
|
+
:ax => "Aland Islands"
|
28
|
+
:kr => "Korea"
|
29
|
+
:tw => "Taiwan"
|
30
|
+
:sg => "Singapore"
|
31
|
+
:dz => "Algeria"
|
32
|
+
:ad => "Andorra"
|
33
|
+
:ar => "Argentina"
|
34
|
+
:am => "Armenia"
|
35
|
+
:az => "Azerbaijan"
|
36
|
+
:bh => "Bahrain"
|
37
|
+
:bd => "Bangladesh"
|
38
|
+
:bb => "Barbados"
|
39
|
+
:by => "Belarus"
|
40
|
+
:bm => "Bermuda"
|
41
|
+
:ba => "Bosnia and Herzegovina"
|
42
|
+
:io => "British Indian Ocean Territory"
|
43
|
+
:bn => "Brunei Darussalam"
|
44
|
+
:bg => "Bulgaria"
|
45
|
+
:kh => "Cambodia"
|
46
|
+
:cv => "Cape Verde"
|
47
|
+
:cl => "Chile"
|
48
|
+
:cr => "Costa Rica"
|
49
|
+
:hr => "Croatia"
|
50
|
+
:cy => "Cyprus"
|
51
|
+
:cz => "Czech Republic"
|
52
|
+
:do => "Dominican Republic"
|
53
|
+
:ec => "Ecuador"
|
54
|
+
:eg => "Egypt"
|
55
|
+
:ee => "Estonia"
|
56
|
+
:fo => "Faroe Islands"
|
57
|
+
:ge => "Georgia"
|
58
|
+
:gr => "Greece"
|
59
|
+
:gl => "Greenland"
|
60
|
+
:gt => "Guatemala"
|
61
|
+
:ht => "Haiti"
|
62
|
+
:hn => "Honduras"
|
63
|
+
:hu => "Hungary"
|
64
|
+
:is => "Iceland"
|
65
|
+
:in => "India"
|
66
|
+
:id => "Indonesia"
|
67
|
+
:ie => "Ireland"
|
68
|
+
:il => "Israel"
|
69
|
+
:jo => "Jordan"
|
70
|
+
:kz => "Kazakhstan"
|
71
|
+
:ke => "Kenya"
|
72
|
+
:kw => "Kuwait"
|
73
|
+
:la => "Lao People's Democratic Republic"
|
74
|
+
:lv => "Latvia"
|
75
|
+
:lb => "Lebanon"
|
76
|
+
:li => "Liechtenstein"
|
77
|
+
:lt => "Lithuania"
|
78
|
+
:lu => "Luxembourg"
|
79
|
+
:mk => "Macedonia"
|
80
|
+
:my => "Malaysia"
|
81
|
+
:mv => "Maldives"
|
82
|
+
:mt => "Malta"
|
83
|
+
:mu => "Mauritius"
|
84
|
+
:mx => "Mexico"
|
85
|
+
:md => "Moldova"
|
86
|
+
:mc => "Monaco"
|
87
|
+
:ma => "Morocco"
|
88
|
+
:np => "Nepal"
|
89
|
+
:nz => "New Zealand"
|
90
|
+
:ni => "Nicaragua"
|
91
|
+
:ng => "Nigeria"
|
92
|
+
:om => "Oman"
|
93
|
+
:pk => "Pakistan"
|
94
|
+
:py => "Paraguay"
|
95
|
+
:ph => "Philippines"
|
96
|
+
:pl => "Poland"
|
97
|
+
:pr => "Puerto Rico"
|
98
|
+
:ro => "Romania"
|
99
|
+
:ru => "Russia"
|
100
|
+
:sm => "San Marino"
|
101
|
+
:sa => "Saudi Arabia"
|
102
|
+
:sn => "Senegal"
|
103
|
+
:sk => "Slovakia"
|
104
|
+
:si => "Slovenia"
|
105
|
+
:za => "South Africa"
|
106
|
+
:lk => "Sri Lanka"
|
107
|
+
:tj => "Tajikistan"
|
108
|
+
:th => "Thailand"
|
109
|
+
:tn => "Tunisia"
|
110
|
+
:tr => "Turkey"
|
111
|
+
:tm => "Turkmenistan"
|
112
|
+
:ua => "Ukraine"
|
113
|
+
:uy => "Uruguay"
|
114
|
+
:uz => "Uzbekistan"
|
115
|
+
:va => "Vatican City"
|
116
|
+
:ve => "Venezuela"
|
117
|
+
:zm => "Zambia"
|
118
|
+
:as => "American Samoa"
|
119
|
+
:cc => "Cocos (Keeling) Islands"
|
120
|
+
:ck => "Cook Islands"
|
121
|
+
:rs => "Serbia"
|
122
|
+
:me => "Montenegro"
|
123
|
+
:cs => "Serbia and Montenegro"
|
124
|
+
:yu => "Yugoslavia"
|
125
|
+
:cx => "Christmas Island"
|
126
|
+
:et => "Ethiopia"
|
127
|
+
:fk => "Falkland Islands (Malvinas)"
|
128
|
+
:nf => "Norfolk Island "
|
129
|
+
:fm => "Micronesia"
|
130
|
+
:gf => "French Guiana"
|
131
|
+
:gn => "Guinea"
|
132
|
+
:gp => "Guadeloupe"
|
133
|
+
:gs => "South Georgia and the South Sandwich Islands"
|
134
|
+
:gu => "Guam"
|
135
|
+
:gw => "Guinea-Bissau"
|
136
|
+
:hm => "Heard Island and McDonald Islands"
|
137
|
+
:iq => "Iraq"
|
138
|
+
:kg => "Kyrgyzstan"
|
139
|
+
:lr => "Liberia"
|
140
|
+
:ls => "Lesotho"
|
141
|
+
:mg => "Madagascar"
|
142
|
+
:mh => "Marshall Islands"
|
143
|
+
:mn => "Mongolia"
|
144
|
+
:mp => "Northern Mariana Islands"
|
145
|
+
:mq => "Martinique"
|
146
|
+
:nc => "New Caledonia"
|
147
|
+
:ne => "Niger"
|
148
|
+
:vi => "Virgin Islands, U.S."
|
149
|
+
:pf => "French Polynesia"
|
150
|
+
:pg => "Papua New Guinea"
|
151
|
+
:pm => "Saint Pierre and Miquelon"
|
152
|
+
:pn => "Pitcairn"
|
153
|
+
:pw => "Palau"
|
154
|
+
:re => "Reunion"
|
155
|
+
:sh => "Saint Helena"
|
156
|
+
:sj => "Svalbard and Jan Mayen"
|
157
|
+
:so => "Somalia"
|
158
|
+
:sz => "Swaziland"
|
159
|
+
:tc => "Turks and Caicos Islands"
|
160
|
+
:wf => "Wallis and Futuna"
|
161
|
+
:yt => "Mayotte"
|
data/LICENSE
ADDED
@@ -0,0 +1,6 @@
|
|
1
|
+
# Licensed under a Creative Commons Attribution-ShareAlike 3.0 License
|
2
|
+
# http://http://creativecommons.org/licenses/by-sa/3.0/
|
3
|
+
#
|
4
|
+
# Attribution: Some of this work is based on 'validates_as_postal_code' by Wes Hays, John Dell
|
5
|
+
# URLs: https://rubygems.org/gems/validates_as_postal_code
|
6
|
+
# https://github.com/gbdev/validates_as_postal_code
|
data/README.md
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
Postoffice
|
2
|
+
==========
|
3
|
+
|
4
|
+
### Rails gem to validate format of various world postal and zip codes, and more.
|
5
|
+
|
6
|
+
Installation:
|
7
|
+
-------------
|
8
|
+
gem install postoffice
|
9
|
+
|
10
|
+
or add the following to your Gemfile
|
11
|
+
|
12
|
+
gem 'postoffice'
|
13
|
+
|
14
|
+
Usage as a Rails validator:
|
15
|
+
---------------------------
|
16
|
+
To validate various world postal codes:
|
17
|
+
|
18
|
+
class MyClass < ActiveRecord::Base
|
19
|
+
validates :postcode, :postcode_format => {:country_code => :us, :message => "entered is not a valid postcode for #{PostOffice.country_name(options[:country_code])}."}
|
20
|
+
end
|
21
|
+
|
22
|
+
To validate Canadian postal codes, simply change the symbol :us to :ca, and you'll get:
|
23
|
+
![An example Postoffice error message](http://i.imgur.com/3ed7b.png "An example Postoffice error message")
|
24
|
+
|
25
|
+
> Note: Don't forget to use the ':allow_blank => true' option, when needed.
|
26
|
+
|
27
|
+
Country name codes abide by the [ISO 3166-1 alpha-2 standard](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2). 155 countries are supported: see COUNTRYCODES.md for the full list.
|
28
|
+
|
29
|
+
General usage:
|
30
|
+
--------------
|
31
|
+
Validating correctness of postcodes directly:
|
32
|
+
|
33
|
+
# Validating an English postcode. Returns 0, since this postcode is valid, returns nil otherwise
|
34
|
+
PostOffice.validate_postcode('N1 9GU', :gb)
|
35
|
+
|
36
|
+
To produce a string of the name of a country, input a supported 2-digit country code as a symbol.
|
37
|
+
|
38
|
+
Example:
|
39
|
+
|
40
|
+
PostOffice.country_name(:us)
|
41
|
+
|
42
|
+
This will output "United States".
|
43
|
+
|
44
|
+
PostOffice.country_name(:lu)
|
45
|
+
|
46
|
+
This will output "Luxembourg".
|
47
|
+
|
48
|
+
License:
|
49
|
+
--------
|
50
|
+
See the LICENSE file.
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rdoc/task'
|
4
|
+
require 'rubygems'
|
5
|
+
require 'active_record'
|
6
|
+
|
7
|
+
desc 'Default: run unit tests.'
|
8
|
+
task :default => :test
|
9
|
+
|
10
|
+
desc 'Test the postoffice plugin.'
|
11
|
+
Rake::TestTask.new(:test) do |t|
|
12
|
+
t.libs << 'lib'
|
13
|
+
t.test_files = FileList['test/**/*_test.rb']
|
14
|
+
t.verbose = true
|
15
|
+
end
|
16
|
+
|
17
|
+
# desc 'Generate documentation for the postoffice plugin.'
|
18
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
19
|
+
rdoc.rdoc_dir = 'rdoc'
|
20
|
+
rdoc.title = 'Postoffice'
|
21
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
22
|
+
rdoc.rdoc_files.include('README')
|
23
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
24
|
+
end
|
data/init.rb
ADDED
data/lib/postoffice.rb
ADDED
@@ -0,0 +1,189 @@
|
|
1
|
+
module PostOffice
|
2
|
+
COUNTRY_DATA = {
|
3
|
+
:us => ["United States",'\d{5}(-\d{4})?'],
|
4
|
+
:ca => ["Canada",'[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}'],
|
5
|
+
:gb => ["United Kingdom (Great Britain)",'([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)'],
|
6
|
+
:cn => ["China",'([0-9]){6}'],
|
7
|
+
:jp => ["Japan",'\d{3}(-)?\d{4}'],
|
8
|
+
:de => ["Germany",'\d{5}'],
|
9
|
+
:fr => ["France",'\d{2}[ ]?\d{3}'],
|
10
|
+
:au => ["Australia",'\d{4}'],
|
11
|
+
:it => ["Italy",'\d{5}'],
|
12
|
+
:ch => ["Switzerland",'\d{4}'],
|
13
|
+
:at => ["Austria",'\d{4}'],
|
14
|
+
:es => ["Spain",'\d{5}'],
|
15
|
+
:nl => ["Netherlands",'\d{4}[ ]?[A-Z]{2}'],
|
16
|
+
:be => ["Belgium",'\d{4}'],
|
17
|
+
:dk => ["Denmark",'\d{4}'],
|
18
|
+
:se => ["Sweden",'\d{3}[ ]?\d{2}'],
|
19
|
+
:no => ["Norway",'\d{4}'],
|
20
|
+
:br => ["Brazil",'\d{5}[\-]?\d{3}'],
|
21
|
+
:pt => ["Portugal",'\d{4}([\-]\d{3})?'],
|
22
|
+
:fi => ["Finaland",'\d{5}'],
|
23
|
+
:ax => ["Aland Islands",'22\d{3}'],
|
24
|
+
:kr => ["Korea",'\d{3}[\-]\d{3}'],
|
25
|
+
:tw => ["Taiwan",'\d{3}(\d{2})?'],
|
26
|
+
:sg => ["Singapore",'\d{6}'],
|
27
|
+
:dz => ["Algeria",'\d{5}'],
|
28
|
+
:ad => ["Andorra",'AD\d{3}'],
|
29
|
+
:ar => ["Argentina",'([A-HJ-NP-Z])?\d{4}([A-Z]{3})?'],
|
30
|
+
:am => ["Armenia",'(37)?\d{4}'],
|
31
|
+
:az => ["Azerbaijan",'\d{4}'],
|
32
|
+
:bh => ["Bahrain",'((1[0-2]|[2-9])\d{2})?'],
|
33
|
+
:bd => ["Bangladesh",'\d{4}'],
|
34
|
+
:bb => ["Barbados",'(BB\d{5})?'],
|
35
|
+
:by => ["Belarus",'\d{6}'],
|
36
|
+
:bm => ["Bermuda",'[A-Z]{2}[ ]?[A-Z0-9]{2}'],
|
37
|
+
:ba => ["Bosnia and Herzegovina",'\d{5}'],
|
38
|
+
:io => ["British Indian Ocean Territory",'BBND 1ZZ'],
|
39
|
+
:bn => ["Brunei Darussalam",'[A-Z]{2}[ ]?\d{4}'],
|
40
|
+
:bg => ["Bulgaria",'\d{4}'],
|
41
|
+
:kh => ["Cambodia",'\d{5}'],
|
42
|
+
:cv => ["Cape Verde",'\d{4}'],
|
43
|
+
:cl => ["Chile",'\d{7}'],
|
44
|
+
:cr => ["Costa Rica",'\d{4,5}|\d{3}-\d{4}'],
|
45
|
+
:hr => ["Croatia",'\d{5}'],
|
46
|
+
:cy => ["Cyprus",'\d{4}'],
|
47
|
+
:cz => ["Czech Republic",'\d{3}[ ]?\d{2}'],
|
48
|
+
:do => ["Dominican Republic",'\d{5}'],
|
49
|
+
:ec => ["Ecuador",'([A-Z]\d{4}[A-Z]|(?:[A-Z]{2})?\d{6})?'],
|
50
|
+
:eg => ["Egypt",'\d{5}'],
|
51
|
+
:ee => ["Estonia",'\d{5}'],
|
52
|
+
:fo => ["Faroe Islands",'\d{3}'],
|
53
|
+
:ge => ["Georgia",'\d{4}'],
|
54
|
+
:gr => ["Greece",'\d{3}[ ]?\d{2}'],
|
55
|
+
:gl => ["Greenland",'39\d{2}'],
|
56
|
+
:gt => ["Guatemala",'\d{5}'],
|
57
|
+
:ht => ["Haiti",'\d{4}'],
|
58
|
+
:hn => ["Honduras",'(?:\d{5})?'],
|
59
|
+
:hu => ["Hungary",'\d{4}'],
|
60
|
+
:is => ["Iceland",'\d{3}'],
|
61
|
+
:in => ["India",'\d{6}'],
|
62
|
+
:id => ["Indonesia",'\d{5}'],
|
63
|
+
:ie => ["Ireland",'((D|DUBLIN)?([1-9]|6[wW]|1[0-8]|2[024]))?'],
|
64
|
+
:il => ["Israel",'\d{5}'],
|
65
|
+
:jo => ["Jordan",'\d{5}'],
|
66
|
+
:kz => ["Kazakhstan",'\d{6}'],
|
67
|
+
:ke => ["Kenya",'\d{5}'],
|
68
|
+
:kw => ["Kuwait",'\d{5}'],
|
69
|
+
:la => ["Lao People's Democratic Republic",'\d{5}'],
|
70
|
+
:lv => ["Latvia",'\d{4}'],
|
71
|
+
:lb => ["Lebanon",'(\d{4}([ ]?\d{4})?)?'],
|
72
|
+
:li => ["Liechtenstein",'(948[5-9])|(949[0-7])'],
|
73
|
+
:lt => ["Lithuania",'\d{5}'],
|
74
|
+
:lu => ["Luxembourg",'\d{4}'],
|
75
|
+
:mk => ["Macedonia",'\d{4}'],
|
76
|
+
:my => ["Malaysia",'\d{5}'],
|
77
|
+
:mv => ["Maldives",'\d{5}'],
|
78
|
+
:mt => ["Malta",'[A-Z]{3}[ ]?\d{2,4}'],
|
79
|
+
:mu => ["Mauritius",'(\d{3}[A-Z]{2}\d{3})?'],
|
80
|
+
:mx => ["Mexico",'\d{5}'],
|
81
|
+
:md => ["Moldova",'\d{4}'],
|
82
|
+
:mc => ["Monaco",'980\d{2}'],
|
83
|
+
:ma => ["Morocco",'\d{5}'],
|
84
|
+
:np => ["Nepal",'\d{5}'],
|
85
|
+
:nz => ["New Zealand",'\d{4}'],
|
86
|
+
:ni => ["Nicaragua",'((\d{4}-)?\d{3}-\d{3}(-\d{1})?)?'],
|
87
|
+
:ng => ["Nigeria",'(\d{6})?'],
|
88
|
+
:om => ["Oman",'(PC )?\d{3}'],
|
89
|
+
:pk => ["Pakistan",'\d{5}'],
|
90
|
+
:py => ["Paraguay",'\d{4}'],
|
91
|
+
:ph => ["Philippines",'\d{4}'],
|
92
|
+
:pl => ["Poland",'\d{2}-\d{3}'],
|
93
|
+
:pr => ["Puerto Rico",'00[679]\d{2}([ \-]\d{4})?'],
|
94
|
+
:ro => ["Romania",'\d{6}'],
|
95
|
+
:ru => ["Russia",'\d{6}'],
|
96
|
+
:sm => ["San Marino",'4789\d'],
|
97
|
+
:sa => ["Saudi Arabia",'\d{5}'],
|
98
|
+
:sn => ["Senegal",'\d{5}'],
|
99
|
+
:sk => ["Slovakia",'\d{3}[ ]?\d{2}'],
|
100
|
+
:si => ["Slovenia",'\d{4}'],
|
101
|
+
:za => ["South Africa",'\d{4}'],
|
102
|
+
:lk => ["Sri Lanka",'\d{5}'],
|
103
|
+
:tj => ["Tajikistan",'\d{6}'],
|
104
|
+
:th => ["Thailand",'\d{5}'],
|
105
|
+
:tn => ["Tunisia",'\d{4}'],
|
106
|
+
:tr => ["Turkey",'\d{5}'],
|
107
|
+
:tm => ["Turkmenistan",'\d{6}'],
|
108
|
+
:ua => ["Ukraine",'\d{5}'],
|
109
|
+
:uy => ["Uruguay",'\d{5}'],
|
110
|
+
:uz => ["Uzbekistan",'\d{6}'],
|
111
|
+
:va => ["Vatican City",'00120'],
|
112
|
+
:ve => ["Venezuela",'\d{4}'],
|
113
|
+
:zm => ["Zambia",'\d{5}'],
|
114
|
+
:as => ["American Samoa",'96799'],
|
115
|
+
:cc => ["Cocos (Keeling) Islands",'6799'],
|
116
|
+
:ck => ["Cook Islands",'\d{4}'],
|
117
|
+
:rs => ["Serbia",'\d{6}'],
|
118
|
+
:me => ["Montenegro",'8\d{4}'],
|
119
|
+
:cs => ["Serbia and Montenegro",'\d{5}'],
|
120
|
+
:yu => ["Yugoslavia",'\d{5}'],
|
121
|
+
:cx => ["Christmas Island",'6798'],
|
122
|
+
:et => ["Ethiopia",'\d{4}'],
|
123
|
+
:fk => ["Falkland Islands (Malvinas)",'FIQQ 1ZZ'],
|
124
|
+
:nf => ["Norfolk Island ",'2899'],
|
125
|
+
:fm => ["Micronesia",'(9694[1-4])([ \-]\d{4})?'],
|
126
|
+
:gf => ["French Guiana",'9[78]3\d{2}'],
|
127
|
+
:gn => ["Guinea",'\d{3}'],
|
128
|
+
:gp => ["Guadeloupe",'9[78][01]\d{2}'],
|
129
|
+
:gs => ["South Georgia and the South Sandwich Islands",'SIQQ 1ZZ'],
|
130
|
+
:gu => ["Guam",'969[123]\d([ \-]\d{4})?'],
|
131
|
+
:gw => ["Guinea-Bissau",'\d{4}'],
|
132
|
+
:hm => ["Heard Island and McDonald Islands",'\d{4}'],
|
133
|
+
:iq => ["Iraq",'\d{5}'],
|
134
|
+
:kg => ["Kyrgyzstan",'\d{6}'],
|
135
|
+
:lr => ["Liberia",'\d{4}'],
|
136
|
+
:ls => ["Lesotho",'\d{3}'],
|
137
|
+
:mg => ["Madagascar",'\d{3}'],
|
138
|
+
:mh => ["Marshall Islands",'969[67]\d([ \-]\d{4})?'],
|
139
|
+
:mn => ["Mongolia",'\d{6}'],
|
140
|
+
:mp => ["Northern Mariana Islands",'9695[012]([ \-]\d{4})?'],
|
141
|
+
:mq => ["Martinique",'9[78]2\d{2}'],
|
142
|
+
:nc => ["New Caledonia",'988\d{2}'],
|
143
|
+
:ne => ["Niger",'\d{4}'],
|
144
|
+
:vi => ["Virgin Islands, U.S.",'008(([0-4]\d)|(5[01]))([ \-]\d{4})?'],
|
145
|
+
:pf => ["French Polynesia",'987\d{2}'],
|
146
|
+
:pg => ["Papua New Guinea",'\d{3}'],
|
147
|
+
:pm => ["Saint Pierre and Miquelon",'9[78]5\d{2}'],
|
148
|
+
:pn => ["Pitcairn",'PCRN 1ZZ'],
|
149
|
+
:pw => ["Palau",'96940'],
|
150
|
+
:re => ["Reunion",'9[78]4\d{2}'],
|
151
|
+
:sh => ["Saint Helena",'(ASCN|STHL) 1ZZ'],
|
152
|
+
:sj => ["Svalbard and Jan Mayen",'\d{4}'],
|
153
|
+
:so => ["Somalia",'\d{5}'],
|
154
|
+
:sz => ["Swaziland",'[HLMS]\d{3}'],
|
155
|
+
:tc => ["Turks and Caicos Islands",'TKCA 1ZZ'],
|
156
|
+
:wf => ["Wallis and Futuna",'986\d{2}'],
|
157
|
+
:yt => ["Mayotte",'976\d{2}']
|
158
|
+
}
|
159
|
+
|
160
|
+
def get_regex(code)
|
161
|
+
result = COUNTRY_DATA[code]
|
162
|
+
result != nil ? result[1] : ""
|
163
|
+
end
|
164
|
+
|
165
|
+
def country_name(code)
|
166
|
+
result = COUNTRY_DATA[code]
|
167
|
+
result != nil ? result[0] : "Not found"
|
168
|
+
end
|
169
|
+
|
170
|
+
def validate_postcode(value, country_code)
|
171
|
+
regexStr = PostOffice.get_regex(country_code)
|
172
|
+
|
173
|
+
value = value.to_s.strip
|
174
|
+
|
175
|
+
return value =~ /^#{regexStr}$/i
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
include PostOffice
|
180
|
+
class PostcodeFormatValidator < ActiveModel::EachValidator
|
181
|
+
def validate_each(object, attribute, value)
|
182
|
+
|
183
|
+
result = PostOffice.validate_postcode(value, options[:country_code])
|
184
|
+
|
185
|
+
if !result
|
186
|
+
object.errors[attribute] << (options[:message] || "entered is not a valid postcode for #{PostOffice.country_name(options[:country_code])}.")
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
# Notes:
|
2
|
+
# ActiveRecord testing in local sqlite, running in memory
|
3
|
+
# http://devblog.avdi.org/2011/01/14/activerecord-golf/
|
4
|
+
|
5
|
+
require 'test/unit'
|
6
|
+
|
7
|
+
require 'active_record'
|
8
|
+
require 'postoffice'
|
9
|
+
|
10
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
11
|
+
|
12
|
+
ActiveRecord::Base.establish_connection(
|
13
|
+
:adapter => "sqlite3",
|
14
|
+
:database => ":memory:")
|
15
|
+
|
16
|
+
# ActiveRecord::Base.connection.create_table(:test_records) do |t|
|
17
|
+
# t.string :postcode
|
18
|
+
# t.string :country_code
|
19
|
+
# end
|
20
|
+
|
21
|
+
class ValidatesAsPostCodeTest < Test::Unit::TestCase
|
22
|
+
|
23
|
+
# Valid post codes
|
24
|
+
def test_valid_post_codes
|
25
|
+
valid_post_codes.each_pair do |key, value|
|
26
|
+
|
27
|
+
test_model_class = Class.new(ActiveRecord::Base) do
|
28
|
+
def self.name
|
29
|
+
'TestRecord'
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.columns; []; end
|
33
|
+
attr_accessor :postcode
|
34
|
+
|
35
|
+
validates :postcode, :postcode_format => {:country_code => key}
|
36
|
+
end
|
37
|
+
|
38
|
+
value.each do |code|
|
39
|
+
assert test_model_class.new(:postcode => code.to_sym).valid?, "#{code} should be valid."
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Invalid post codes
|
45
|
+
def test_invalid_post_codes
|
46
|
+
invalid_post_codes.each_pair do |key, value|
|
47
|
+
test_model_class = Class.new(ActiveRecord::Base) do
|
48
|
+
def self.name
|
49
|
+
'TestRecord'
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.columns; []; end
|
53
|
+
attr_accessor :postcode
|
54
|
+
|
55
|
+
validates :postcode, :postcode_format => {:country_code => key}
|
56
|
+
end
|
57
|
+
|
58
|
+
value.each do |code|
|
59
|
+
assert test_model_class.new(:postcode => code.to_sym).invalid?, "#{code} should be invalid."
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_manual_valid_post_codes
|
65
|
+
assert PostOffice.validate_postcode('N1 9GU', :gb), "should evaluate true"
|
66
|
+
assert PostOffice.validate_postcode('K1A 0A6', :ca), "should evaluate true"
|
67
|
+
assert PostOffice.validate_postcode('20500', :us), "should evaluate true"
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_manual_invalid_post_codes
|
71
|
+
assert !PostOffice.validate_postcode('N1GU', :gb), "should evaluate false"
|
72
|
+
assert !PostOffice.validate_postcode('K1A 0AX6', :ca), "should evaluate false"
|
73
|
+
assert !PostOffice.validate_postcode('2050', :us), "should evaluate false"
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_country_code_name_retrieval
|
77
|
+
assert PostOffice.country_name(:us) == "United States", "should evaluate to 'United States'"
|
78
|
+
assert PostOffice.country_name(:la) == "Lao People's Democratic Republic", "should evaluate to 'Lao People's Democratic Republic'"
|
79
|
+
assert PostOffice.country_name(:vi) == "Virgin Islands, U.S.", "should evaluate to 'Virgin Islands, U.S.'"
|
80
|
+
end
|
81
|
+
|
82
|
+
# Test blank post codes
|
83
|
+
def test_blank_post_code_allowed
|
84
|
+
|
85
|
+
test_model_class = Class.new(ActiveRecord::Base) do
|
86
|
+
def self.columns; []; end
|
87
|
+
attr_accessor :postcode
|
88
|
+
|
89
|
+
validates :postcode, :postcode_format => {:country_code => true}, :allow_blank => true
|
90
|
+
end
|
91
|
+
|
92
|
+
assert !test_model_class.new(:postcode => nil).invalid?, "Blank post code should be legal."
|
93
|
+
assert !test_model_class.new(:postcode => '').invalid?, "Blank post code should be legal."
|
94
|
+
end
|
95
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
def valid_post_codes
|
2
|
+
{:us => ['89511-1000',
|
3
|
+
'89511'],
|
4
|
+
:ca => ['M6K2P4',
|
5
|
+
'M6K 2P4',
|
6
|
+
'T0E 1S2',
|
7
|
+
'T0E1S2',
|
8
|
+
'B4V 2K4',
|
9
|
+
'B4V2K4'],
|
10
|
+
:gb => ['BX2 1LB',
|
11
|
+
'E98 1TT',
|
12
|
+
'EH99 1SP',
|
13
|
+
'N1 9GU',
|
14
|
+
'SW1A 0AA',
|
15
|
+
'SW1A 0PW',
|
16
|
+
'W1A 1AA',
|
17
|
+
'W1N 4DJ'],
|
18
|
+
:cn => ['510000',
|
19
|
+
'010000',
|
20
|
+
'129999'],
|
21
|
+
:jp => ['1006828',
|
22
|
+
'1620854',
|
23
|
+
'1740045'],
|
24
|
+
:ru => ['119021',
|
25
|
+
'247112']}
|
26
|
+
end
|
27
|
+
|
28
|
+
def invalid_post_codes
|
29
|
+
{:us => ['891511-1000',
|
30
|
+
'895x11'],
|
31
|
+
:ca => ['M662P4',
|
32
|
+
'M6K 2PP',
|
33
|
+
'T0E 1zS2',
|
34
|
+
'T0E1S22',
|
35
|
+
'3B4V 2K4',
|
36
|
+
'B4 V2K4'],
|
37
|
+
:gb => ['2 1LB',
|
38
|
+
'E98x 11TT',
|
39
|
+
'EH99 P1SP',
|
40
|
+
'N1 9GUx',
|
41
|
+
'SW1A 0AAT',
|
42
|
+
'SW14A 0PW',
|
43
|
+
'W1A 145AA',
|
44
|
+
'W1N 4DDJ'],
|
45
|
+
:cn => ['5100010',
|
46
|
+
'010x000',
|
47
|
+
'11999'],
|
48
|
+
:jp => ['10068281',
|
49
|
+
'1620 854',
|
50
|
+
'174z0045'],
|
51
|
+
:ru => ['119 021',
|
52
|
+
'1247112']}
|
53
|
+
end
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: postoffice
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.9
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Christopher Butcher
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-05 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Implements an ActiveModel::EachValidator that can be added to your models
|
15
|
+
in a single line, to validate the format of various world postal codes. More features
|
16
|
+
for working with addresses to come.
|
17
|
+
email: cbutcher@gmail.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- README.md
|
23
|
+
- LICENSE
|
24
|
+
- COUNTRYCODES.md
|
25
|
+
- Rakefile
|
26
|
+
- init.rb
|
27
|
+
- lib/postoffice.rb
|
28
|
+
- test/postcode_validator_test.rb
|
29
|
+
- test/test_helper.rb
|
30
|
+
homepage: https://github.com/chrisbutcher/postoffice
|
31
|
+
licenses:
|
32
|
+
- Creative Commons Attribution-ShareAlike 3.0
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
requirements: []
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.8.24
|
53
|
+
signing_key:
|
54
|
+
specification_version: 3
|
55
|
+
summary: Rails gem to validate format of various world postal codes, and more.
|
56
|
+
test_files:
|
57
|
+
- test/postcode_validator_test.rb
|