ca_postal_code 0.1.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/Gemfile.lock +24 -24
- data/LICENSE.md +21 -0
- data/README.md +53 -2
- data/ca_postal_code.gemspec +5 -2
- data/lib/ca_postal_code/region_guessing.rb +30 -0
- data/lib/ca_postal_code/version.rb +1 -1
- data/lib/ca_postal_code.rb +29 -44
- metadata +15 -14
- data/.rspec_status +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 66677149dbc296b6a8c1861898aa7083ca454c253d440742e53f41b0116ddd6d
|
4
|
+
data.tar.gz: b81e91e584af729de3bd05c9a8ef58b80583aa562d194cc742131be5a2a1ea8d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 753a836aaa71e3949be86d120ff950f28da4a47bbd05f609b3794975158a4f945d5d8f92dafa7838b4050691981823b51bed63f6326e33d4d1a5ccd80a19cc78
|
7
|
+
data.tar.gz: 20533950465b7ea1c97ab55f4cd58e197a97e825f0f66965aac359a2a12ead8c2f56bf412f78100397fc4ab87bf3b80689470e92f8a9f616a175a24a8514ced5
|
data/Gemfile.lock
CHANGED
@@ -1,46 +1,46 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
ca_postal_code (0.
|
4
|
+
ca_postal_code (0.2.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
|
-
coderay (1.1.
|
10
|
-
diff-lcs (1.
|
9
|
+
coderay (1.1.3)
|
10
|
+
diff-lcs (1.5.0)
|
11
11
|
interception (0.5)
|
12
|
-
method_source (0.
|
13
|
-
pry (0.
|
14
|
-
coderay (~> 1.1
|
15
|
-
method_source (~>
|
16
|
-
pry-rescue (1.
|
12
|
+
method_source (1.0.0)
|
13
|
+
pry (0.14.2)
|
14
|
+
coderay (~> 1.1)
|
15
|
+
method_source (~> 1.0)
|
16
|
+
pry-rescue (1.5.2)
|
17
17
|
interception (>= 0.5)
|
18
|
-
pry
|
19
|
-
rake (
|
20
|
-
rspec (3.
|
21
|
-
rspec-core (~> 3.
|
22
|
-
rspec-expectations (~> 3.
|
23
|
-
rspec-mocks (~> 3.
|
24
|
-
rspec-core (3.
|
25
|
-
rspec-support (~> 3.
|
26
|
-
rspec-expectations (3.
|
18
|
+
pry (>= 0.12.0)
|
19
|
+
rake (13.0.6)
|
20
|
+
rspec (3.12.0)
|
21
|
+
rspec-core (~> 3.12.0)
|
22
|
+
rspec-expectations (~> 3.12.0)
|
23
|
+
rspec-mocks (~> 3.12.0)
|
24
|
+
rspec-core (3.12.1)
|
25
|
+
rspec-support (~> 3.12.0)
|
26
|
+
rspec-expectations (3.12.2)
|
27
27
|
diff-lcs (>= 1.2.0, < 2.0)
|
28
|
-
rspec-support (~> 3.
|
29
|
-
rspec-mocks (3.
|
28
|
+
rspec-support (~> 3.12.0)
|
29
|
+
rspec-mocks (3.12.3)
|
30
30
|
diff-lcs (>= 1.2.0, < 2.0)
|
31
|
-
rspec-support (~> 3.
|
32
|
-
rspec-support (3.
|
31
|
+
rspec-support (~> 3.12.0)
|
32
|
+
rspec-support (3.12.0)
|
33
33
|
|
34
34
|
PLATFORMS
|
35
35
|
ruby
|
36
36
|
|
37
37
|
DEPENDENCIES
|
38
|
-
bundler (~>
|
38
|
+
bundler (~> 2.4)
|
39
39
|
ca_postal_code!
|
40
40
|
pry
|
41
41
|
pry-rescue
|
42
|
-
rake (~>
|
42
|
+
rake (~> 13.0)
|
43
43
|
rspec (~> 3.0)
|
44
44
|
|
45
45
|
BUNDLED WITH
|
46
|
-
|
46
|
+
2.4.8
|
data/LICENSE.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2018 Jonathan Allard
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
CHANGED
@@ -1,9 +1,60 @@
|
|
1
1
|
# CAPostalCode
|
2
|
+
A toolbox for Canadian postal codes: validating, normalizing and guessing their region.
|
3
|
+
|
4
|
+
```ruby
|
5
|
+
CAPostalCode.valid?("h1b2p3")
|
6
|
+
# => true
|
7
|
+
|
8
|
+
CAPostalCode.normalize("e3b.4k5 ")
|
9
|
+
# => "E3B 4K5"
|
10
|
+
|
11
|
+
CAPostalCode.guess_region("K1A 0B3")
|
12
|
+
# => "ON"
|
13
|
+
|
14
|
+
CAPostalCode.guess_region("X0C 1A1")
|
15
|
+
# => "NU"
|
16
|
+
```
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
### Validity
|
20
|
+
After normalizing the string, verifies if it fits the format for a postal code.
|
21
|
+
|
22
|
+
Strings containing letters D, F, I, O, Q, and those starting with W or Z are invalid.
|
23
|
+
```ruby
|
24
|
+
CAPostalCode.valid?("h1b2p3")
|
25
|
+
# => true
|
26
|
+
|
27
|
+
CAPostalCode.valid?("F8G 1A1")
|
28
|
+
# => false
|
29
|
+
|
30
|
+
CAPostalCode.valid?("potato")
|
31
|
+
# => false
|
32
|
+
|
33
|
+
CAPostalCode.valid?("P0T 4T0")
|
34
|
+
# => true
|
35
|
+
```
|
36
|
+
|
37
|
+
### Normalization
|
38
|
+
Normalize the string to look like a postal code.
|
39
|
+
|
40
|
+
It does not check for validity. Rather, the `.valid?` method relies on this one to make its work easier.
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
CAPostalCode.normalize("e3b.4k5 ")
|
44
|
+
# => "E3B 4K5"
|
45
|
+
|
46
|
+
CAPostalCode.normalize("g1c 4z9")
|
47
|
+
# => "G1C 4Z9"
|
48
|
+
|
49
|
+
CAPostalCode.normalize("potato")
|
50
|
+
# => "POT ATO"
|
51
|
+
```
|
52
|
+
|
53
|
+
### Region guessing
|
2
54
|
Guesses the Canadian province or territory (*region*) based on the postal code.
|
3
55
|
|
4
56
|
For provinces and Yukon, it will guess based on the first letter. For Northwest Territories and Nunavut, it will check the first two or three characters.
|
5
57
|
|
6
|
-
## Usage
|
7
58
|
```ruby
|
8
59
|
CAPostalCode.guess_region("G1A 1A1")
|
9
60
|
# => "QC"
|
@@ -22,7 +73,7 @@ In your Gemfile:
|
|
22
73
|
|
23
74
|
Or install it yourself:
|
24
75
|
|
25
|
-
|
76
|
+
gem install ca_postal_code
|
26
77
|
|
27
78
|
## Development
|
28
79
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/ca_postal_code.gemspec
CHANGED
@@ -8,6 +8,7 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = CAPostalCode::VERSION
|
9
9
|
spec.authors = ["Jonathan Allard"]
|
10
10
|
spec.email = ["jonathan@allard.io"]
|
11
|
+
spec.license = "MIT"
|
11
12
|
|
12
13
|
spec.summary = %q{Guesses the Canadian province or territory (region) based on the postal code.}
|
13
14
|
# spec.description = %q{Write a longer description or delete this line.}
|
@@ -21,8 +22,10 @@ Gem::Specification.new do |spec|
|
|
21
22
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
23
|
spec.require_paths = ["lib"]
|
23
24
|
|
24
|
-
|
25
|
-
|
25
|
+
|
26
|
+
|
27
|
+
spec.add_development_dependency "bundler", "~> 2.4"
|
28
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
26
29
|
spec.add_development_dependency "rspec", "~> 3.0"
|
27
30
|
|
28
31
|
spec.add_development_dependency "pry"
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module CAPostalCode
|
2
|
+
module RegionGuessing
|
3
|
+
PATTERNS = {
|
4
|
+
/^A/ => "NL",
|
5
|
+
/^B/ => "NS",
|
6
|
+
/^C/ => "PE",
|
7
|
+
/^E/ => "NB",
|
8
|
+
/^[GHJ]/ => "QC",
|
9
|
+
/^[KLMNP]/ => "ON",
|
10
|
+
/^R/ => "MB",
|
11
|
+
/^S/ => "SK",
|
12
|
+
/^T/ => "AB",
|
13
|
+
/^V/ => "BC",
|
14
|
+
/^X0[A-C]/ => "NU",
|
15
|
+
/^X0[EG]|^X1A/ => "NT",
|
16
|
+
/^Y/ => "YT",
|
17
|
+
}
|
18
|
+
|
19
|
+
# Guesses province or territory based on postal code first characters.
|
20
|
+
#
|
21
|
+
# Output is defined for valid, normalized postal codes only.
|
22
|
+
def guess_region(string)
|
23
|
+
entry = PATTERNS.detect do |pattern, region|
|
24
|
+
pattern.match?(string)
|
25
|
+
end
|
26
|
+
|
27
|
+
entry[1] if entry
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/lib/ca_postal_code.rb
CHANGED
@@ -1,55 +1,40 @@
|
|
1
1
|
require "ca_postal_code/version"
|
2
|
+
require "ca_postal_code/region_guessing"
|
2
3
|
|
3
4
|
module CAPostalCode
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
entry = patterns.detect do |pattern, region|
|
22
|
-
pattern.match?(string)
|
23
|
-
end
|
24
|
-
|
25
|
-
entry[1] if entry
|
26
|
-
end
|
27
|
-
|
28
|
-
# Lists the patterns the library will use. It is made public only to
|
29
|
-
# make the implementation visible.
|
30
|
-
def self.patterns
|
31
|
-
@@patterns
|
5
|
+
extend RegionGuessing
|
6
|
+
|
7
|
+
# > Postal codes do not include the letters D, F, I, O, Q or U,
|
8
|
+
# > and the first position also does not make use of the letters W or Z.
|
9
|
+
PATTERN = %r{
|
10
|
+
[ABCEGHJKLMNPRSTVXY]
|
11
|
+
[0-9]
|
12
|
+
[ABCEGHJKLMNPRSTVWXYZ]
|
13
|
+
\ ?
|
14
|
+
[0-9]
|
15
|
+
[ABCEGHJKLMNPRSTVWXYZ]
|
16
|
+
[0-9]
|
17
|
+
}x
|
18
|
+
|
19
|
+
def self.valid?(string)
|
20
|
+
return false unless string = normalize(string)
|
21
|
+
strict_valid?(string)
|
32
22
|
end
|
33
23
|
|
34
|
-
|
35
|
-
|
36
|
-
def self.add_pattern(pattern, region)
|
37
|
-
unless pattern.is_a?(Regexp)
|
38
|
-
pattern = Regexp.new("^" + pattern)
|
39
|
-
end
|
40
|
-
|
41
|
-
@@patterns[pattern] = region.to_s
|
24
|
+
def self.strict_valid?(string)
|
25
|
+
string.match? PATTERN
|
42
26
|
end
|
43
27
|
|
44
|
-
|
28
|
+
# Normalize postal code into "A1A 1A1" format.
|
29
|
+
#
|
30
|
+
# Will return `nil` if the normalized string does not have 6 characters.
|
31
|
+
def self.normalize(string)
|
32
|
+
string.gsub!(/[^a-zA-Z0-9]/, '')
|
33
|
+
string.upcase!
|
45
34
|
|
46
|
-
|
47
|
-
unless patterns.is_a?(Array)
|
48
|
-
patterns = [patterns]
|
49
|
-
end
|
35
|
+
return unless string.length == 6
|
50
36
|
|
51
|
-
|
52
|
-
|
53
|
-
end
|
37
|
+
string = string[0..2] + " " + string[3..5]
|
38
|
+
string
|
54
39
|
end
|
55
40
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ca_postal_code
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonathan Allard
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-03-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -16,28 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '2.4'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '2.4'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '13.0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '13.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -80,7 +80,7 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
|
-
description:
|
83
|
+
description:
|
84
84
|
email:
|
85
85
|
- jonathan@allard.io
|
86
86
|
executables: []
|
@@ -89,10 +89,10 @@ extra_rdoc_files: []
|
|
89
89
|
files:
|
90
90
|
- ".gitignore"
|
91
91
|
- ".rspec"
|
92
|
-
- ".rspec_status"
|
93
92
|
- ".travis.yml"
|
94
93
|
- Gemfile
|
95
94
|
- Gemfile.lock
|
95
|
+
- LICENSE.md
|
96
96
|
- README.md
|
97
97
|
- Rakefile
|
98
98
|
- bin/console
|
@@ -100,11 +100,13 @@ files:
|
|
100
100
|
- bin/setup
|
101
101
|
- ca_postal_code.gemspec
|
102
102
|
- lib/ca_postal_code.rb
|
103
|
+
- lib/ca_postal_code/region_guessing.rb
|
103
104
|
- lib/ca_postal_code/version.rb
|
104
105
|
homepage: https://github.com/joallard/ca-postal-code
|
105
|
-
licenses:
|
106
|
+
licenses:
|
107
|
+
- MIT
|
106
108
|
metadata: {}
|
107
|
-
post_install_message:
|
109
|
+
post_install_message:
|
108
110
|
rdoc_options: []
|
109
111
|
require_paths:
|
110
112
|
- lib
|
@@ -119,9 +121,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
119
121
|
- !ruby/object:Gem::Version
|
120
122
|
version: '0'
|
121
123
|
requirements: []
|
122
|
-
|
123
|
-
|
124
|
-
signing_key:
|
124
|
+
rubygems_version: 3.4.1
|
125
|
+
signing_key:
|
125
126
|
specification_version: 4
|
126
127
|
summary: Guesses the Canadian province or territory (region) based on the postal code.
|
127
128
|
test_files: []
|
data/.rspec_status
DELETED
@@ -1,8 +0,0 @@
|
|
1
|
-
example_id | status | run_time |
|
2
|
-
------------------------------------ | ------ | --------------- |
|
3
|
-
./spec/ca_postal_code_spec.rb[1:1:1] | passed | 0.00186 seconds |
|
4
|
-
./spec/ca_postal_code_spec.rb[1:1:2] | passed | 0.0001 seconds |
|
5
|
-
./spec/ca_postal_code_spec.rb[1:1:3] | passed | 0.00007 seconds |
|
6
|
-
./spec/ca_postal_code_spec.rb[1:1:4] | passed | 0.00007 seconds |
|
7
|
-
./spec/ca_postal_code_spec.rb[1:1:5] | passed | 0.0001 seconds |
|
8
|
-
./spec/ca_postal_code_spec.rb[1:2:1] | passed | 0.00016 seconds |
|