personnummer 0.0.3 → 0.0.4
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.
- data/Changelog +7 -3
- data/demo.rb +1 -1
- data/lib/personnummer.rb +91 -91
- metadata +6 -4
data/Changelog
CHANGED
@@ -1,9 +1,13 @@
|
|
1
|
+
0.0.4 Released
|
2
|
+
|
3
|
+
2011-01-10: Ruby 1.9 compatibility fixes
|
4
|
+
|
1
5
|
0.0.3 Released
|
2
|
-
|
3
|
-
|
6
|
+
|
7
|
+
2010-04-19: (Less) naïve age calculation
|
4
8
|
|
5
9
|
0.0.2 Released
|
6
10
|
|
7
11
|
2008-08-07: Number argument can now be a Fixnum or String
|
8
12
|
|
9
|
-
0.0.1 Released
|
13
|
+
0.0.1 Released
|
data/demo.rb
CHANGED
data/lib/personnummer.rb
CHANGED
@@ -1,86 +1,86 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
require 'date'
|
2
3
|
|
3
4
|
class Personnummer
|
4
5
|
# Public readonly attributes
|
5
6
|
attr_reader :age, :born, :region, :control_digit
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
# Store the initial number
|
7
|
+
|
8
|
+
def initialize(number)
|
9
|
+
|
10
|
+
@valid = false
|
11
|
+
# Store the initial number
|
12
12
|
@number = number.to_s
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
13
|
+
|
14
|
+
# Match the number
|
15
|
+
if @number.match(/(\d{2})(\d{2})(\d{2})([\-\+]{0,1})(\d{3})(\d{0,1})/)
|
16
|
+
|
17
|
+
# Calculate the control digit based on the birth date and serial number
|
18
|
+
@control_digit = luhn_algorithm("#{$~[1]}#{$~[2]}#{$~[3]}#{$~[5]}")
|
19
|
+
|
20
|
+
# Get the different parts of the number
|
21
|
+
year = $~[1].to_i
|
22
|
+
month = $~[2].to_i
|
23
|
+
day = $~[3].to_i
|
24
|
+
divider = $~[4]
|
25
|
+
serial = $~[5].to_i
|
26
|
+
|
27
|
+
# Set default divider if not present
|
28
|
+
divider ||= '-'
|
29
|
+
|
30
|
+
# Make the personnummer valid if the checksum is correct
|
31
|
+
@valid = true if @control_digit == $~[6].to_i && !$~[6].empty?
|
32
|
+
|
33
|
+
# Get the current date
|
34
|
+
today = Date.today
|
35
|
+
|
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
40
|
century = 1900
|
41
41
|
elsif divider == '+'
|
42
42
|
century = 1800
|
43
43
|
else
|
44
44
|
century = 1900
|
45
45
|
end
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
46
|
+
|
47
|
+
# Get the date the person was born
|
48
|
+
@born = Date.parse("#{century+year}-#{month}-#{day}")
|
49
|
+
|
50
|
+
# Get the region name
|
51
|
+
@region = region_name(serial)
|
52
|
+
|
53
|
+
# (Less) naïve age calculation
|
54
|
+
@age = ((today - @born).to_i/365)
|
55
|
+
|
56
|
+
# Check if the person is female based the serial (even == female)
|
57
|
+
@female = (serial % 2 == 0)
|
58
58
|
else
|
59
|
-
raise Exception.new, "The supplied personnummer is invalid"
|
59
|
+
raise Exception.new, "The supplied personnummer is invalid"
|
60
60
|
end
|
61
|
-
|
62
|
-
|
63
|
-
|
61
|
+
end
|
62
|
+
|
63
|
+
def to_s
|
64
64
|
@number
|
65
65
|
end
|
66
|
-
|
67
|
-
|
68
|
-
|
66
|
+
|
67
|
+
def valid?
|
68
|
+
@valid
|
69
69
|
end
|
70
|
-
|
71
|
-
|
72
|
-
|
70
|
+
|
71
|
+
def male?
|
72
|
+
!@female
|
73
73
|
end
|
74
|
-
|
74
|
+
|
75
75
|
def female?
|
76
76
|
@female
|
77
77
|
end
|
78
78
|
|
79
|
-
private
|
79
|
+
private
|
80
80
|
|
81
81
|
def luhn_algorithm(number)
|
82
82
|
multiplications = []
|
83
|
-
|
83
|
+
|
84
84
|
number.split(//).each_with_index do |digit, i|
|
85
85
|
if i % 2 == 0
|
86
86
|
multiplications << digit.to_i*2
|
@@ -88,8 +88,9 @@ private
|
|
88
88
|
multiplications << digit.to_i
|
89
89
|
end
|
90
90
|
end
|
91
|
-
|
91
|
+
|
92
92
|
sum = 0
|
93
|
+
|
93
94
|
multiplications.each do |number|
|
94
95
|
number.to_s.each_byte do |character|
|
95
96
|
sum += character.chr.to_i
|
@@ -101,45 +102,44 @@ private
|
|
101
102
|
else
|
102
103
|
control_digit = (sum / 10 + 1) * 10 - sum
|
103
104
|
end
|
104
|
-
|
105
|
+
|
105
106
|
control_digit
|
106
107
|
end
|
107
108
|
|
108
109
|
def region_name(code)
|
109
|
-
|
110
|
-
#
|
111
|
-
# (When the previous region code was changed to a serial number)
|
110
|
+
# Don't return a region name if the person was born after 1990
|
111
|
+
# (When the previous region code was changed to a serial number)
|
112
112
|
if @born.year > 1990
|
113
113
|
return ''
|
114
114
|
end
|
115
|
-
|
115
|
+
|
116
116
|
case code
|
117
|
-
when 000..139
|
118
|
-
when 140..159
|
119
|
-
when 160..189
|
120
|
-
when 190..239
|
121
|
-
when 240..269
|
122
|
-
when 270..289
|
123
|
-
when 290..319
|
124
|
-
when 320..329
|
125
|
-
when 330..349
|
126
|
-
when 350..389
|
127
|
-
when 390..459
|
128
|
-
when 460..479
|
129
|
-
when 480..549
|
130
|
-
when 550..589
|
131
|
-
when 590..619
|
132
|
-
when 620..159
|
133
|
-
when 650..659
|
134
|
-
when 660..689
|
135
|
-
when 690..709
|
136
|
-
when 710..739
|
137
|
-
when 750..779
|
138
|
-
when 780..819
|
139
|
-
when 820..849
|
140
|
-
when 850..889
|
141
|
-
when 890..929
|
142
|
-
when 930..999
|
117
|
+
when 000..139 then 'Stockholms Län'
|
118
|
+
when 140..159 then 'Uppsala län'
|
119
|
+
when 160..189 then 'Södermanlands län'
|
120
|
+
when 190..239 then 'Östergötlands län'
|
121
|
+
when 240..269 then 'Jönköpings län'
|
122
|
+
when 270..289 then 'Kronobergs län'
|
123
|
+
when 290..319 then 'Kalmar län'
|
124
|
+
when 320..329 then 'Gotlands län'
|
125
|
+
when 330..349 then 'Blekinge län'
|
126
|
+
when 350..389 then 'Kristianstads län'
|
127
|
+
when 390..459 then 'Malmöhus län'
|
128
|
+
when 460..479 then 'Hallands län'
|
129
|
+
when 480..549 then 'Göteborgs och Bohus län'
|
130
|
+
when 550..589 then 'Älvsborgs län'
|
131
|
+
when 590..619 then 'Skaraborgs län'
|
132
|
+
when 620..159 then 'Värmlands län'
|
133
|
+
when 650..659 then 'Födda utomlands'
|
134
|
+
when 660..689 then 'Örebro län'
|
135
|
+
when 690..709 then 'Västmanlands län'
|
136
|
+
when 710..739 then 'Kopparbergs län'
|
137
|
+
when 750..779 then 'Gävleborgs län'
|
138
|
+
when 780..819 then 'Västernorrlands län'
|
139
|
+
when 820..849 then 'Jämtlands län'
|
140
|
+
when 850..889 then 'Västerbottens län'
|
141
|
+
when 890..929 then 'Norrbottens län'
|
142
|
+
when 930..999 then 'Födda utomlands eller utländska medborgare födda i Sverige'
|
143
143
|
end
|
144
144
|
end
|
145
|
-
end
|
145
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 4
|
9
|
+
version: 0.0.4
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Peter Hellberg
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date:
|
17
|
+
date: 2011-01-10 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
@@ -42,6 +42,7 @@ rdoc_options: []
|
|
42
42
|
require_paths:
|
43
43
|
- lib
|
44
44
|
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
45
46
|
requirements:
|
46
47
|
- - ">="
|
47
48
|
- !ruby/object:Gem::Version
|
@@ -49,6 +50,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
49
50
|
- 0
|
50
51
|
version: "0"
|
51
52
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
52
54
|
requirements:
|
53
55
|
- - ">="
|
54
56
|
- !ruby/object:Gem::Version
|
@@ -58,7 +60,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
58
60
|
requirements: []
|
59
61
|
|
60
62
|
rubyforge_project:
|
61
|
-
rubygems_version: 1.3.
|
63
|
+
rubygems_version: 1.3.7
|
62
64
|
signing_key:
|
63
65
|
specification_version: 3
|
64
66
|
summary: Personnummer handles validation of Swedish personal identity numbers.
|