resident 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d93a0b9f267f96e0a266917baa460796b855613a
4
- data.tar.gz: b3fe8f7e4392247dd2f2184562a7c347ae8d2e62
3
+ metadata.gz: f353c0782a96c1d79d67c61cb7d29d7508ad45bb
4
+ data.tar.gz: adfc48cdb9ca18ab426aff09572f8d5a1d4903cf
5
5
  SHA512:
6
- metadata.gz: 206a9d30f1f4d83d75e007b192fa56fe682750fc7c1aaf9fc5b092eeb3f54c600e43c0f0ba7872637cfc523b451348e69d2cca941de579affc114ce6f7dafe77
7
- data.tar.gz: a1e8a71fdbcc5b007ddf25e61e449254da96cfcf9f78452111bc4ec697db99f2ce2a700ed708efdba38ee4933aee8b4368a7b222f1767cb1f1509ba397308ee0
6
+ metadata.gz: 87117786abbf0a70b38ec3a654fcf133fae1fbdac1c3e8c1b3b957bf037a8abec7b049225627d6d6233f0dcbe91ad6802b7c42eb225b492322a9030f0dcaedce
7
+ data.tar.gz: 11458275db57866d20915a61cf480ca50ec7b3922f787f5bc2dd2cfe0bf70857dabd253b87500af7d13796e95393dabcaa4a6f20c7e7d37a6bef2cf66d65d2b3
data/README.md CHANGED
@@ -12,15 +12,16 @@ There is a special focus on identifying these numbers robustly (i.e. you can saf
12
12
 
13
13
  ## Usage
14
14
 
15
- Really, you can just use <tt>valid?</tt>, <tt>to_s</tt>, and <tt>age</tt> like so:
15
+ Really, you can just use <tt>valid?</tt>, <tt>sanitize</tt>, <tt>to_s</tt>, and <tt>age</tt> like so:
16
16
 
17
17
  require 'resident'
18
18
 
19
19
  number = NationalIdentificationNumber::Swedish.new('0501261853')
20
20
 
21
- number.valid? #=> true
22
- number.to_s #=> "050126-1853"
23
- number.age #=> 9
21
+ number.valid? #=> true
22
+ number.to_s #=> "050126-1853"
23
+ number.sanitize #=> "050126-1853" (Would have been nil if invalid)
24
+ number.age #=> 9
24
25
 
25
26
  Please have a look at the specs to see how the national identification numbers of various countries are handled.
26
27
 
@@ -55,4 +56,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
55
56
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
56
57
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
57
58
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
58
- THE SOFTWARE.
59
+ THE SOFTWARE.
@@ -17,6 +17,10 @@ module NationalIdentificationNumber
17
17
  @number
18
18
  end
19
19
 
20
+ def sanitize
21
+ to_s if valid?
22
+ end
23
+
20
24
  def age
21
25
  age_for_dob date
22
26
  end
@@ -4,7 +4,6 @@ module NationalIdentificationNumber
4
4
  # Inspired by https://github.com/c7/personnummer/blob/master/lib/personnummer.rb
5
5
  # Copyright (c) 2008 Peter Hellberg MIT
6
6
  class Swedish < Base
7
-
8
7
  attr_reader :date # used for testing
9
8
 
10
9
  # Generator for valid Swedish personnummer: http://en.wikipedia.org/wiki/Personal_identity_number_(Sweden)
@@ -31,7 +30,12 @@ module NationalIdentificationNumber
31
30
  if @number.match(/\A(\d{0}|\d{2})(\d{6})(\-{0,1})(\d{4})\Z/)
32
31
  @number = "#{$2}-#{$4}"
33
32
  else
34
- @number.gsub!(/[^\d\-\+]/, '')
33
+ candidate = @number.gsub(/[^\d\-\+]/, '')
34
+ if candidate.match(/\A(\d{0}|\d{2})(\d{6})(\-{0,1})(\d{4})\Z/)
35
+ @number = "#{$2}-#{$4}"
36
+ else
37
+ @number = candidate
38
+ end
35
39
  end
36
40
  end
37
41
 
@@ -98,15 +102,14 @@ module NationalIdentificationNumber
98
102
  end
99
103
  control_digit
100
104
  end
101
-
102
105
  end
103
106
  end
104
107
 
105
- if $0 == __FILE__
106
- # Randomize every part
107
- puts Bukowskis::NationalIdentificationNumber::Swedish.generate
108
- # Use given date, randomize serial
109
- puts Bukowskis::NationalIdentificationNumber::Swedish.generate(Date.new(1975,1,1))
110
- # Use given date and serial, calculate checksum
111
- puts Bukowskis::NationalIdentificationNumber::Swedish.generate(Date.new(1975,1,1), 123)
112
- end
108
+ # if $0 == __FILE__
109
+ # # Randomize every part
110
+ # puts Bukowskis::NationalIdentificationNumber::Swedish.generate
111
+ # # Use given date, randomize serial
112
+ # puts Bukowskis::NationalIdentificationNumber::Swedish.generate(Date.new(1975,1,1))
113
+ # # Use given date and serial, calculate checksum
114
+ # puts Bukowskis::NationalIdentificationNumber::Swedish.generate(Date.new(1975,1,1), 123)
115
+ # end
@@ -5,7 +5,6 @@ include NationalIdentificationNumber
5
5
 
6
6
  describe Finnish do
7
7
  describe '#valid?' do
8
-
9
8
  it "recognizes valid numbers" do
10
9
  expect( Finnish.new('311280-999J') ).to be_valid
11
10
  expect( Finnish.new('131052-308T') ).to be_valid
@@ -32,7 +31,39 @@ describe Finnish do
32
31
  expect( Finnish.new(1234567890) ).to_not be_valid
33
32
  expect( Finnish.new(:really_bad_input_value) ).to_not be_valid
34
33
  end
34
+ end
35
35
 
36
+ describe 'sanitize' do
37
+ context 'valid numbers' do
38
+ it 'is the formatted number' do
39
+ expect(Finnish.new('311280-999J').sanitize).to eq '311280-999J'
40
+ expect(Finnish.new('131052-308T').sanitize).to eq '131052-308T'
41
+ expect(Finnish.new('290164-862u').sanitize).to eq '290164-862U'
42
+ expect(Finnish.new('270368A172X').sanitize).to eq '270368A172X'
43
+ expect(Finnish.new('310145A586a').sanitize).to eq '310145A586A'
44
+ expect(Finnish.new('080266+183P').sanitize).to eq '080266+183P'
45
+ expect(Finnish.new('290248+145c').sanitize).to eq '290248+145C'
46
+ end
47
+ end
48
+
49
+ context 'invalid numbers' do
50
+ it 'is nil' do
51
+ expect(Finnish.new('671301A172V').sanitize).to be nil
52
+ expect(Finnish.new('830231A172M').sanitize).to be nil
53
+ expect(Finnish.new('311180-999J').sanitize).to be nil
54
+ expect(Finnish.new('131052-308S').sanitize).to be nil
55
+ expect(Finnish.new('290164X862U').sanitize).to be nil
56
+ expect(Finnish.new('670368A172X').sanitize).to be nil
57
+ expect(Finnish.new('310145--586A').sanitize).to be nil
58
+ expect(Finnish.new('asdf').sanitize).to be nil
59
+ expect(Finnish.new('1234567890').sanitize).to be nil
60
+ expect(Finnish.new('0000000000000').sanitize).to be nil
61
+ expect(Finnish.new('000000-0000').sanitize).to be nil
62
+ expect(Finnish.new('').sanitize).to be nil
63
+ expect(Finnish.new(1234567890).sanitize).to be nil
64
+ expect(Finnish.new(:really_bad_input_value).sanitize).to be nil
65
+ end
66
+ end
36
67
  end
37
68
 
38
69
  describe 'age' do
@@ -94,4 +125,4 @@ describe Finnish do
94
125
  end
95
126
 
96
127
  end
97
- end
128
+ end
@@ -41,6 +41,40 @@ describe Swedish, '.generate' do
41
41
 
42
42
  end
43
43
 
44
+ describe 'sanitize' do
45
+ context 'invalid numbers' do
46
+ it 'is nil' do
47
+ expect(Swedish.new('991301-1236').sanitize).to be nil # valid checksum, invalid month
48
+ expect(Swedish.new('830231-5554').sanitize).to be nil # valid checksum, invalid day
49
+ expect(Swedish.new('050112--2451').sanitize).to be nil
50
+ expect(Swedish.new('123456-1239').sanitize).to be nil
51
+ expect(Swedish.new('180123-2668').sanitize).to be nil
52
+ expect(Swedish.new('150D1261853').sanitize).to be nil
53
+ expect(Swedish.new('750112-2451').sanitize).to be nil
54
+ expect(Swedish.new('123').sanitize).to be nil
55
+ expect(Swedish.new('000000-0000').sanitize).to be nil
56
+ expect(Swedish.new('0000000000').sanitize).to be nil
57
+ expect(Swedish.new('asdfghj').sanitize).to be nil
58
+ expect(Swedish.new('').sanitize).to be nil
59
+ expect(Swedish.new(12345678).sanitize).to be nil
60
+ expect(Swedish.new(:really_bad_input).sanitize).to be nil
61
+ end
62
+ end
63
+
64
+ context 'valid numbers' do
65
+ it 'is the sanitize dnumber' do
66
+ expect(Swedish.new('19180123-2669').sanitize).to eq '180123-2669'
67
+ expect(Swedish.new('00180123-2669').sanitize).to eq '180123-2669'
68
+ expect(Swedish.new('000180123-2669').sanitize).to eq '180123-2669'
69
+ expect(Swedish.new('050126-1853').sanitize).to eq '050126-1853'
70
+ expect(Swedish.new('0asdfghj501261853').sanitize).to eq '050126-1853'
71
+ expect(Swedish.new('050112-2451').sanitize).to eq '050112-2451'
72
+ expect(Swedish.new('450202-6950').sanitize).to eq '450202-6950'
73
+ expect(Swedish.new('19450202-6950').sanitize).to eq '450202-6950'
74
+ end
75
+ end
76
+ end
77
+
44
78
  describe '#age' do
45
79
 
46
80
  before do
@@ -90,6 +124,7 @@ describe Swedish, '.generate' do
90
124
  it "return the number normalized if the number is valid" do
91
125
  expect( Swedish.new('0501261853').to_s ).to eq '050126-1853'
92
126
  expect( Swedish.new('050126-1853').to_s ).to eq '050126-1853'
127
+ expect( Swedish.new('0asdfghj501261853').to_s).to eq '050126-1853'
93
128
  expect( Swedish.new("190501261853").to_s ).to eq '050126-1853'
94
129
  expect( Swedish.new("19050126-1853").to_s ).to eq '050126-1853'
95
130
  expect( Swedish.new("19050126-185d3").to_s ).to eq '050126-1853'
@@ -180,4 +215,4 @@ describe Swedish, '.generate' do
180
215
  end
181
216
  end
182
217
 
183
- end
218
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resident
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bukowskis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-24 00:00:00.000000000 Z
11
+ date: 2018-07-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -104,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
104
  version: '0'
105
105
  requirements: []
106
106
  rubyforge_project:
107
- rubygems_version: 2.2.2
107
+ rubygems_version: 2.5.1
108
108
  signing_key:
109
109
  specification_version: 4
110
110
  summary: Validate National Identification Numbers.