personnummer 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of personnummer might be problematic. Click here for more details.

@@ -10,17 +10,18 @@ class Personnummer
10
10
 
11
11
  # Match the number
12
12
  number = number.to_s
13
- if number.match(/(\d{2})(\d{2})(\d{2})([\-\+]{0,1})(\d{3})(\d{0,1})/)
13
+ if number.match(/(\d{2}){0,1}(\d{2})(\d{2})(\d{2})([\-\+]{0,1})(\d{3})(\d{0,1})/)
14
14
 
15
15
  # Calculate the control digit based on the birth date and serial number
16
- @control_digit = luhn_algorithm("#{$~[1]}#{$~[2]}#{$~[3]}#{$~[5]}")
16
+ @control_digit = luhn_algorithm("#{$~[2]}#{$~[3]}#{$~[4]}#{$~[6]}")
17
17
 
18
18
  # Get the different parts of the number
19
- year = $~[1].to_i
20
- month = $~[2].to_i
21
- day = $~[3].to_i
22
- @divider = $~[4]
23
- @serial = $~[5].to_i
19
+ century = $~[1].to_i
20
+ year = $~[2].to_i
21
+ month = $~[3].to_i
22
+ day = $~[4].to_i
23
+ @divider = $~[5]
24
+ @serial = $~[6].to_i
24
25
 
25
26
  # Set default divider if not present
26
27
  if @divider.empty?
@@ -28,20 +29,24 @@ class Personnummer
28
29
  end
29
30
 
30
31
  # Make the personnummer valid if the checksum is correct
31
- @valid = true if @control_digit == $~[6].to_i && !$~[6].empty?
32
+ @valid = true if @control_digit == $~[7].to_i && !$~[7].empty?
32
33
 
33
34
  # Get the current date
34
35
  today = Date.today
35
36
 
36
- # Decide which century corresponds to the number
37
- if year < (today.year-2000) && @divider == '-'
38
- century = 2000
39
- elsif year < (today.year-2000) && @divider == '+'
40
- century = 1900
41
- elsif @divider == '+'
42
- century = 1800
37
+ if century == 0
38
+ # Decide which century corresponds to the number
39
+ if year < (today.year-2000) && @divider == '-'
40
+ century = 2000
41
+ elsif year < (today.year-2000) && @divider == '+'
42
+ century = 1900
43
+ elsif @divider == '+'
44
+ century = 1800
45
+ else
46
+ century = 1900
47
+ end
43
48
  else
44
- century = 1900
49
+ century *= 100
45
50
  end
46
51
 
47
52
  # Get the date the person was born
@@ -3,8 +3,8 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "personnummer"
6
- s.version = "0.0.5"
7
- s.authors = ["Peter Hellberg", "Victor Koronen"]
6
+ s.version = "0.0.6"
7
+ s.authors = ["Peter Hellberg", "Victor Koronen", "Sebastian Wallin"]
8
8
  s.email = ["peter@c7.se"]
9
9
  s.homepage = "https://github.com/c7/personnummer/"
10
10
  s.summary = %q{Personnummer handles validation of Swedish personal identity numbers.}
@@ -17,7 +17,7 @@ Gem::Specification.new do |s|
17
17
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
18
  s.require_paths = ["lib"]
19
19
 
20
- s.add_development_dependency "ruby-debug19"
20
+ s.add_development_dependency "rake"
21
21
  s.add_development_dependency "rspec"
22
22
  s.add_development_dependency "guard"
23
23
  s.add_development_dependency "guard-bundler"
@@ -9,36 +9,44 @@ describe Personnummer do
9
9
  expect{ Personnummer.new(9001010017) }.not_to raise_error(ArgumentError)
10
10
  expect{ Personnummer.new('900101-0017') }.not_to raise_error(ArgumentError)
11
11
  expect{ Personnummer.new('100101+0017') }.not_to raise_error(ArgumentError)
12
+ expect{ Personnummer.new('19900101-0017') }.not_to raise_error(ArgumentError)
12
13
  end
13
14
 
14
15
  it "accepts a valid personnummer (without control digit)" do
15
16
  expect{ Personnummer.new(900101001) }.not_to raise_error(ArgumentError)
16
17
  expect{ Personnummer.new('900101-001') }.not_to raise_error(ArgumentError)
17
18
  expect{ Personnummer.new('100101+001') }.not_to raise_error(ArgumentError)
19
+ expect{ Personnummer.new('19900101-001') }.not_to raise_error(ArgumentError)
18
20
  end
19
21
 
20
22
  it "throws ArgumentError on an invalid personnummer" do
21
23
  expect{ Personnummer.new(nil) }.to raise_error(ArgumentError)
22
24
  expect{ Personnummer.new(17) }.to raise_error(ArgumentError)
23
25
  expect{ Personnummer.new('112233-4455') }.to raise_error(ArgumentError)
26
+ expect{ Personnummer.new('19112233-4455') }.to raise_error(ArgumentError)
24
27
  end
25
28
  end
26
29
 
27
30
  describe "age" do
28
31
  it "calculates age correctly" do
29
32
  p = Personnummer.new('900101-001')
33
+ p2 = Personnummer.new('19900101-001')
30
34
 
31
35
  Timecop.freeze(Date.parse('1980-01-01')) do
32
36
  p.age.should == 0
37
+ p2.age.should == 0
33
38
  end
34
39
  Timecop.freeze(Date.parse('1990-01-01')) do
35
40
  p.age.should == 0
41
+ p2.age.should == 0
36
42
  end
37
43
  Timecop.freeze(Date.parse('2000-01-01')) do
38
44
  p.age.should == 10
45
+ p2.age.should == 10
39
46
  end
40
47
  Timecop.freeze(Date.parse('2090-01-01')) do
41
48
  p.age.should == 100
49
+ p2.age.should == 100
42
50
  end
43
51
  end
44
52
  end
@@ -49,6 +57,8 @@ describe Personnummer do
49
57
  Personnummer.new('900101-001').born.should == Date.parse('1990-01-01')
50
58
  Personnummer.new('010101-001').born.should == Date.parse('2001-01-01')
51
59
  Personnummer.new('100101+001').born.should == Date.parse('1810-01-01')
60
+ Personnummer.new('19900101-001').born.should == Date.parse('1990-01-01')
61
+ Personnummer.new('18900101-001').born.should == Date.parse('1890-01-01')
52
62
  end
53
63
  end
54
64
  end
@@ -101,6 +111,8 @@ describe Personnummer do
101
111
  Personnummer.new(9001010017).to_s.should == '900101-0017'
102
112
  Personnummer.new('900101-0017').to_s.should == '900101-0017'
103
113
  Personnummer.new('900101-001').to_s.should == '900101-0017'
114
+ Personnummer.new('19900101-001').to_s.should == '900101-0017'
115
+ Personnummer.new('18900101-001').to_s.should == '900101-0017'
104
116
  end
105
117
  end
106
118
 
metadata CHANGED
@@ -1,20 +1,21 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: personnummer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Peter Hellberg
9
9
  - Victor Koronen
10
+ - Sebastian Wallin
10
11
  autorequire:
11
12
  bindir: bin
12
13
  cert_chain: []
13
- date: 2011-11-25 00:00:00.000000000Z
14
+ date: 2012-04-18 00:00:00.000000000 Z
14
15
  dependencies:
15
16
  - !ruby/object:Gem::Dependency
16
- name: ruby-debug19
17
- requirement: &70323661135380 !ruby/object:Gem::Requirement
17
+ name: rake
18
+ requirement: &70353993851320 !ruby/object:Gem::Requirement
18
19
  none: false
19
20
  requirements:
20
21
  - - ! '>='
@@ -22,10 +23,10 @@ dependencies:
22
23
  version: '0'
23
24
  type: :development
24
25
  prerelease: false
25
- version_requirements: *70323661135380
26
+ version_requirements: *70353993851320
26
27
  - !ruby/object:Gem::Dependency
27
28
  name: rspec
28
- requirement: &70323661134940 !ruby/object:Gem::Requirement
29
+ requirement: &70353993850820 !ruby/object:Gem::Requirement
29
30
  none: false
30
31
  requirements:
31
32
  - - ! '>='
@@ -33,10 +34,10 @@ dependencies:
33
34
  version: '0'
34
35
  type: :development
35
36
  prerelease: false
36
- version_requirements: *70323661134940
37
+ version_requirements: *70353993850820
37
38
  - !ruby/object:Gem::Dependency
38
39
  name: guard
39
- requirement: &70323661134520 !ruby/object:Gem::Requirement
40
+ requirement: &70353993850400 !ruby/object:Gem::Requirement
40
41
  none: false
41
42
  requirements:
42
43
  - - ! '>='
@@ -44,10 +45,10 @@ dependencies:
44
45
  version: '0'
45
46
  type: :development
46
47
  prerelease: false
47
- version_requirements: *70323661134520
48
+ version_requirements: *70353993850400
48
49
  - !ruby/object:Gem::Dependency
49
50
  name: guard-bundler
50
- requirement: &70323661134100 !ruby/object:Gem::Requirement
51
+ requirement: &70353993849900 !ruby/object:Gem::Requirement
51
52
  none: false
52
53
  requirements:
53
54
  - - ! '>='
@@ -55,10 +56,10 @@ dependencies:
55
56
  version: '0'
56
57
  type: :development
57
58
  prerelease: false
58
- version_requirements: *70323661134100
59
+ version_requirements: *70353993849900
59
60
  - !ruby/object:Gem::Dependency
60
61
  name: guard-rspec
61
- requirement: &70323661133680 !ruby/object:Gem::Requirement
62
+ requirement: &70353993849460 !ruby/object:Gem::Requirement
62
63
  none: false
63
64
  requirements:
64
65
  - - ! '>='
@@ -66,10 +67,10 @@ dependencies:
66
67
  version: '0'
67
68
  type: :development
68
69
  prerelease: false
69
- version_requirements: *70323661133680
70
+ version_requirements: *70353993849460
70
71
  - !ruby/object:Gem::Dependency
71
72
  name: cover_me
72
- requirement: &70323661133260 !ruby/object:Gem::Requirement
73
+ requirement: &70353993849020 !ruby/object:Gem::Requirement
73
74
  none: false
74
75
  requirements:
75
76
  - - ! '>='
@@ -77,10 +78,10 @@ dependencies:
77
78
  version: '0'
78
79
  type: :development
79
80
  prerelease: false
80
- version_requirements: *70323661133260
81
+ version_requirements: *70353993849020
81
82
  - !ruby/object:Gem::Dependency
82
83
  name: timecop
83
- requirement: &70323661132840 !ruby/object:Gem::Requirement
84
+ requirement: &70353993848600 !ruby/object:Gem::Requirement
84
85
  none: false
85
86
  requirements:
86
87
  - - ! '>='
@@ -88,7 +89,7 @@ dependencies:
88
89
  version: '0'
89
90
  type: :development
90
91
  prerelease: false
91
- version_requirements: *70323661132840
92
+ version_requirements: *70353993848600
92
93
  description: Personnummer handles validation of Swedish personal identity numbers.
93
94
  email:
94
95
  - peter@c7.se
@@ -130,10 +131,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
131
  version: '0'
131
132
  requirements: []
132
133
  rubyforge_project:
133
- rubygems_version: 1.8.6
134
+ rubygems_version: 1.8.15
134
135
  signing_key:
135
136
  specification_version: 3
136
137
  summary: Personnummer handles validation of Swedish personal identity numbers.
137
138
  test_files:
138
139
  - spec/lib/personnummer_spec.rb
139
140
  - spec/spec_helper.rb
141
+ has_rdoc: false