ca_postal_code 0.1.0 → 0.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: febd2f0c9952da899878811be706b93622be5e59
4
- data.tar.gz: 03c4462db8563c7eee93b4d3acd52241bd5d3c0b
2
+ SHA256:
3
+ metadata.gz: 74e23617eab8b83ff3803b444f697de1d8ce0447446caa0c23aa205582f4f28c
4
+ data.tar.gz: fcba68f76741640a368e4b299a05e6cb519c1c413965929beab6daa345a374f4
5
5
  SHA512:
6
- metadata.gz: 187b930c59b4a1d1cdf6b624c2810166e553f8cf6d43faad1a6ca9f07b1455357dee95fd9a3866709df7fd0bce72efa27c766fd0157ab7be44201338de6bb39e
7
- data.tar.gz: aad6f9d97f0b60df1ef231394df32b69bd1d5330d01c80a4c7130ff345a26a4f2f0a2191e71485412b737051f40878451dc31e017aada18cc1ac20bdccd69042
6
+ metadata.gz: 29f745dd7c28c4f0cb11ce71efca289232d913469ff7ea6efb1c5bc3db7e5fbc98c0df9e0762ad64ef0dffebaf985594cf0dca76bd5560c27d92b325a7f83dd7
7
+ data.tar.gz: e114ca30469b342af67b21966a6f28338e9985d009ca195dd835d516139a967fceb51e3d584a4c02486e119b37aefa7ad0c6a8fe132501927423d8d2b998d0bb
data/Gemfile.lock CHANGED
@@ -1,35 +1,35 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ca_postal_code (0.1.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.2)
10
- diff-lcs (1.3)
9
+ coderay (1.1.3)
10
+ diff-lcs (1.4.4)
11
11
  interception (0.5)
12
- method_source (0.9.0)
13
- pry (0.11.3)
14
- coderay (~> 1.1.0)
15
- method_source (~> 0.9.0)
16
- pry-rescue (1.4.5)
12
+ method_source (1.0.0)
13
+ pry (0.13.1)
14
+ coderay (~> 1.1)
15
+ method_source (~> 1.0)
16
+ pry-rescue (1.5.2)
17
17
  interception (>= 0.5)
18
- pry
19
- rake (10.5.0)
20
- rspec (3.7.0)
21
- rspec-core (~> 3.7.0)
22
- rspec-expectations (~> 3.7.0)
23
- rspec-mocks (~> 3.7.0)
24
- rspec-core (3.7.1)
25
- rspec-support (~> 3.7.0)
26
- rspec-expectations (3.7.0)
18
+ pry (>= 0.12.0)
19
+ rake (13.0.1)
20
+ rspec (3.9.0)
21
+ rspec-core (~> 3.9.0)
22
+ rspec-expectations (~> 3.9.0)
23
+ rspec-mocks (~> 3.9.0)
24
+ rspec-core (3.9.2)
25
+ rspec-support (~> 3.9.3)
26
+ rspec-expectations (3.9.2)
27
27
  diff-lcs (>= 1.2.0, < 2.0)
28
- rspec-support (~> 3.7.0)
29
- rspec-mocks (3.7.0)
28
+ rspec-support (~> 3.9.0)
29
+ rspec-mocks (3.9.1)
30
30
  diff-lcs (>= 1.2.0, < 2.0)
31
- rspec-support (~> 3.7.0)
32
- rspec-support (3.7.1)
31
+ rspec-support (~> 3.9.0)
32
+ rspec-support (3.9.3)
33
33
 
34
34
  PLATFORMS
35
35
  ruby
@@ -39,8 +39,8 @@ DEPENDENCIES
39
39
  ca_postal_code!
40
40
  pry
41
41
  pry-rescue
42
- rake (~> 10.0)
42
+ rake (~> 13.0)
43
43
  rspec (~> 3.0)
44
44
 
45
45
  BUNDLED WITH
46
- 1.16.1
46
+ 2.2.22
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 +22,7 @@ Gem::Specification.new do |spec|
22
22
  spec.require_paths = ["lib"]
23
23
 
24
24
  spec.add_development_dependency "bundler", "~> 1.16"
25
- spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rake", "~> 13.0"
26
26
  spec.add_development_dependency "rspec", "~> 3.0"
27
27
 
28
28
  spec.add_development_dependency "pry"
@@ -0,0 +1,59 @@
1
+ module CAPostalCode
2
+ module RegionGuessing
3
+
4
+ REGIONS = {
5
+ NL: ?A,
6
+ NS: ?B,
7
+ PE: ?C,
8
+ NB: ?E,
9
+ QC: [?G, ?H, ?J],
10
+ ON: [?K, ?L, ?M, ?N, ?P],
11
+ MB: ?R,
12
+ SK: ?S,
13
+ AB: ?T,
14
+ BC: ?V,
15
+ NU: /^X0[A-C]/,
16
+ NT: [/^X0[EG]/, /^X1A/],
17
+ YT: ?Y,
18
+ }
19
+
20
+ # Guesses province or territory based on postal code first characters.
21
+ #
22
+ # Output is defined for valid, normalized postal codes only.
23
+ def guess_region(string)
24
+ entry = region_patterns.detect do |pattern, region|
25
+ pattern.match?(string)
26
+ end
27
+
28
+ entry[1] if entry
29
+ end
30
+
31
+ # Lists the patterns the library will use. It is made public only to
32
+ # make the implementation visible.
33
+ def region_patterns
34
+ @@region_patterns
35
+ end
36
+
37
+ private
38
+
39
+ def self.add_pattern(pattern, region)
40
+ unless pattern.is_a?(Regexp)
41
+ pattern = Regexp.new("^" + pattern)
42
+ end
43
+
44
+ @@region_patterns[pattern] = region.to_s
45
+ end
46
+
47
+ @@region_patterns = {}
48
+
49
+ REGIONS.each do |region, patterns|
50
+ unless patterns.is_a?(Array)
51
+ patterns = [patterns]
52
+ end
53
+
54
+ patterns.each do |pattern|
55
+ add_pattern(pattern, region)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -1,3 +1,3 @@
1
1
  module CAPostalCode
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,55 +1,41 @@
1
1
  require "ca_postal_code/version"
2
+ require "ca_postal_code/region_guessing"
2
3
 
3
4
  module CAPostalCode
4
- REGIONS = {
5
- NL: ?A,
6
- NS: ?B,
7
- PE: ?C,
8
- NB: ?E,
9
- QC: [?G, ?H, ?J],
10
- ON: [?K, ?L, ?M, ?N, ?P],
11
- MB: ?R,
12
- SK: ?S,
13
- AB: ?T,
14
- BC: ?V,
15
- NU: /^X0[A-C]/,
16
- NT: [/^X0[EG]/, /^X1A/],
17
- YT: ?Y,
18
- }
19
-
20
- def self.guess_region(string)
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
- private
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
- @@patterns = {}
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.strip!
33
+ string.upcase!
34
+ string.gsub!(/\W/, '')
45
35
 
46
- REGIONS.each do |region, patterns|
47
- unless patterns.is_a?(Array)
48
- patterns = [patterns]
49
- end
36
+ return unless string.length == 6
50
37
 
51
- patterns.each do |pattern|
52
- add_pattern(pattern, region)
53
- end
38
+ string = string[0..2] + " " + string[3..5]
39
+ string
54
40
  end
55
41
  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.1.0
4
+ version: 0.2.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: 2018-04-11 00:00:00.000000000 Z
11
+ date: 2021-11-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
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: '10.0'
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,7 +89,6 @@ 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
@@ -100,11 +99,12 @@ files:
100
99
  - bin/setup
101
100
  - ca_postal_code.gemspec
102
101
  - lib/ca_postal_code.rb
102
+ - lib/ca_postal_code/region_guessing.rb
103
103
  - lib/ca_postal_code/version.rb
104
104
  homepage: https://github.com/joallard/ca-postal-code
105
105
  licenses: []
106
106
  metadata: {}
107
- post_install_message:
107
+ post_install_message:
108
108
  rdoc_options: []
109
109
  require_paths:
110
110
  - lib
@@ -119,9 +119,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
119
  - !ruby/object:Gem::Version
120
120
  version: '0'
121
121
  requirements: []
122
- rubyforge_project:
123
- rubygems_version: 2.6.13
124
- signing_key:
122
+ rubygems_version: 3.2.22
123
+ signing_key:
125
124
  specification_version: 4
126
125
  summary: Guesses the Canadian province or territory (region) based on the postal code.
127
126
  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 |