personnummer 0.0.7 → 0.0.8

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.

@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0fc6165e81ecc9904de06d3afdf83461feb9eb95
4
+ data.tar.gz: c6928771697ab2d52624352bca95a8ad45c7d40a
5
+ SHA512:
6
+ metadata.gz: e98dd51c3bc8905d85776f2ac45569d69753f31192a979893c17a8ec4522cf6d5a728c09d2be2d4dbe1c1fe97c1cccf16ee739a89b63df7ae37496fc5d75eb16
7
+ data.tar.gz: 91016101d1c9f6e7f14ed09717074922a82e474ef3c3035874035ef62599e0de22b5910d3801ecc3257f039d454eb786248f140318f8e290cc6683d265cf61da
data/Changelog CHANGED
@@ -1,14 +1,22 @@
1
+ 0.0.8 Released
2
+
3
+ 2014-06-29: Support for co-ordination numbers (theneubeck)
4
+
5
+ 0.0.7 Released
6
+
7
+ 2012-09-12: Don't alter an existing control digit, even if it’s wrong
8
+
1
9
  0.0.6 Released
2
10
 
3
- 2011-04-18: Now supports century if given
11
+ 2011-04-18: Now supports century if given
4
12
 
5
13
  0.0.5 Released
6
14
 
7
- 2011-11-25: Tests!
15
+ 2011-11-25: Tests!
8
16
 
9
17
  0.0.4 Released
10
18
 
11
- 2011-01-09: Ruby 1.9 support
19
+ 2011-01-09: Ruby 1.9 support
12
20
 
13
21
  0.0.3 Released
14
22
 
@@ -1,16 +1,11 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- personnummer (0.0.7)
4
+ personnummer (0.0.8)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
8
8
  specs:
9
- configatron (2.9.1)
10
- yamler (>= 0.1.0)
11
- cover_me (1.2.0)
12
- configatron
13
- hashie
14
9
  diff-lcs (1.1.3)
15
10
  guard (1.3.2)
16
11
  listen (>= 0.4.2)
@@ -20,7 +15,6 @@ GEM
20
15
  guard (~> 1.1)
21
16
  guard-rspec (1.2.1)
22
17
  guard (>= 1.1)
23
- hashie (1.2.0)
24
18
  listen (0.5.0)
25
19
  rake (0.9.2.2)
26
20
  rspec (2.11.0)
@@ -33,13 +27,11 @@ GEM
33
27
  rspec-mocks (2.11.2)
34
28
  thor (0.16.0)
35
29
  timecop (0.4.6)
36
- yamler (0.1.0)
37
30
 
38
31
  PLATFORMS
39
32
  ruby
40
33
 
41
34
  DEPENDENCIES
42
- cover_me
43
35
  guard
44
36
  guard-bundler
45
37
  guard-rspec
@@ -9,7 +9,23 @@ Personnummer is a ruby class that handles [Swedish personal identity number](htt
9
9
  ### Example usage
10
10
 
11
11
  <pre>&gt;&gt; p = Personnummer.new('830428-5912')
12
- =&gt; #&lt;Personnummer:0x301128 @number="830428-5912", @age=25, @valid=true, @region="Skaraborgs l\303\244n",
13
- @male=true, @born=#&lt;Date: 4890905/2,0,2299161&gt;&gt;
12
+ #&gt;&gt;Personnummer:0x007fc971369100 @valid=true, @control_digit=2, @divider="-", @serial=591, @born=#&gt;&gt;PersonnummerDate:0x007fc971368318 @divider="-", @day=28, @month=4, @year=1983&gt; #&lt;, @region="Skaraborgs län", @female=false&gt; #&lt;
13
+
14
14
  &gt;&gt; p.valid?
15
15
  =&gt; true</pre>
16
+ </pre>
17
+
18
+ ### This implementation also supports co-ordination numbers
19
+
20
+ [Information about co-ordination numbers (in swedish)](https://www.skatteverket.se/rattsinformation/reglerochstallningstaganden/meddelanden/2010/meddelanden2010/skvm201003.5.233f91f71260075abe8800024297.html)
21
+
22
+ These numbers are obtained by adding 60 to the day of the birth date
23
+
24
+ <pre>
25
+ &gt;&gt; p = Personnummer.new('830488-5912')
26
+ =&gt; #&lt;Personnummer:0x007fbafd1458d8 @valid=false, @control_digit=2, @divider="-", @serial=591, @born=#&lt;PersonnummerDate:0x007fbafd144af0 @divider="-", @day=88, @month=4, @year=1983&gt;, @region="Skaraborgs län", @female=false&gt;
27
+ &gt;&gt; p.born_at
28
+ =&gt; #&lt;Date: 1983-04-28 ((2445453j,0s,0n),+0s,2299161j)&gt;
29
+ &gt;&gt; p.co_ordination_number?
30
+ =&gt; true
31
+ </pre>
data/Rakefile CHANGED
@@ -3,12 +3,4 @@ require 'bundler/gem_tasks'
3
3
  require 'rspec/core/rake_task'
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- namespace :cover_me do
7
- desc "Generates and opens code coverage report."
8
- task :report do
9
- require 'cover_me'
10
- CoverMe.complete!
11
- end
12
- end
13
-
14
6
  task :default => :spec
@@ -3,15 +3,15 @@ require 'date'
3
3
 
4
4
  class Personnummer
5
5
  # Public readonly attributes
6
- attr_reader :born, :region, :control_digit
6
+ attr_reader :region, :control_digit
7
7
 
8
8
  def initialize(number)
9
9
  @valid = false
10
10
 
11
11
  # Match the number
12
12
  number = number.to_s
13
- if number.match(/(\d{2}){0,1}(\d{2})(\d{2})(\d{2})([\-\+]{0,1})(\d{3})(\d{0,1})/)
14
13
 
14
+ if number.match(/(\d{2}){0,1}(\d{2})(\d{2})(\d{2})([\-\+]{0,1})(\d{3})(\d{0,1})/)
15
15
  # Calculate the control digit based on the birth date and serial number
16
16
  cd = luhn_algorithm("#{$~[2]}#{$~[3]}#{$~[4]}#{$~[6]}")
17
17
 
@@ -34,26 +34,8 @@ class Personnummer
34
34
  # Make the personnummer valid if the checksum is correct
35
35
  @valid = true if @control_digit == cd && !$~[7].empty?
36
36
 
37
- # Get the current date
38
- today = Date.today
39
-
40
- if century == 0
41
- # Decide which century corresponds to the number
42
- if year < (today.year-2000) && @divider == '-'
43
- century = 2000
44
- elsif year < (today.year-2000) && @divider == '+'
45
- century = 1900
46
- elsif @divider == '+'
47
- century = 1800
48
- else
49
- century = 1900
50
- end
51
- else
52
- century *= 100
53
- end
54
-
55
- # Get the date the person was born
56
- @born = Date.parse("#{century+year}-#{month}-#{day}")
37
+ @born = Born.new(century, year, month, day, @divider)
38
+ @valid = @born.valid? && @valid
57
39
 
58
40
  # Get the region name
59
41
  @region = region_name(@serial)
@@ -66,15 +48,15 @@ class Personnummer
66
48
  end
67
49
 
68
50
  def age
69
- if Date.today > @born
70
- (Date.today - @born).to_i/365
71
- else
72
- 0
73
- end
51
+ @born.age
52
+ end
53
+
54
+ def born
55
+ @born.to_date
74
56
  end
75
57
 
76
58
  def to_s
77
- "%s%s%03d%d" % [@born.strftime("%y%m%d"), @divider, @serial, @control_digit]
59
+ "%s%s%03d%d" % [born.strftime("%y%m%d"), @divider, @serial, @control_digit]
78
60
  end
79
61
 
80
62
  def valid?
@@ -89,6 +71,10 @@ class Personnummer
89
71
  @female
90
72
  end
91
73
 
74
+ def co_ordination_number?
75
+ @born.co_ordination_number?
76
+ end
77
+
92
78
  private
93
79
 
94
80
  def luhn_algorithm(number)
@@ -155,4 +141,63 @@ private
155
141
  when 930..999 then 'Födda utomlands eller utländska medborgare födda i Sverige'
156
142
  end
157
143
  end
144
+
145
+ class Born
146
+ attr_reader :year, :month
147
+
148
+ def initialize(century, year, month, day, divider)
149
+ @divider = divider
150
+ @day = day
151
+ @month = month
152
+ @year = calculate_century(century, year) + year
153
+ end
154
+
155
+ def day
156
+ co_ordination_number? ? @day - 60 : @day
157
+ end
158
+
159
+ def co_ordination_number?
160
+ @day && 60 < @day and @day < 92
161
+ end
162
+
163
+ def to_date
164
+ Date.parse("#{year}-#{month}-#{day}")
165
+ end
166
+
167
+ def valid?
168
+ to_date && true
169
+ end
170
+
171
+ def age
172
+ if Date.today > to_date
173
+ (Date.today - to_date).to_i/365
174
+ else
175
+ 0
176
+ end
177
+ end
178
+
179
+ private
180
+
181
+ def calculate_century(century, year)
182
+ # Get the current date
183
+ today = Date.today
184
+
185
+ if century == 0
186
+ # Decide which century corresponds to the number
187
+ if year < (today.year-2000) && @divider == '-'
188
+ century = 2000
189
+ elsif year < (today.year-2000) && @divider == '+'
190
+ century = 1900
191
+ elsif @divider == '+'
192
+ century = 1800
193
+ else
194
+ century = 1900
195
+ end
196
+ else
197
+ century *= 100
198
+ end
199
+
200
+ return century
201
+ end
202
+ end
158
203
  end
@@ -3,7 +3,7 @@ $:.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.7"
6
+ s.version = "0.0.8"
7
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/"
@@ -22,6 +22,5 @@ Gem::Specification.new do |s|
22
22
  s.add_development_dependency "guard"
23
23
  s.add_development_dependency "guard-bundler"
24
24
  s.add_development_dependency "guard-rspec"
25
- s.add_development_dependency "cover_me"
26
25
  s.add_development_dependency "timecop"
27
26
  end
@@ -36,14 +36,17 @@ describe Personnummer do
36
36
  p.age.should == 0
37
37
  p2.age.should == 0
38
38
  end
39
+
39
40
  Timecop.freeze(Date.parse('1990-01-01')) do
40
41
  p.age.should == 0
41
42
  p2.age.should == 0
42
43
  end
44
+
43
45
  Timecop.freeze(Date.parse('2000-01-01')) do
44
46
  p.age.should == 10
45
47
  p2.age.should == 10
46
48
  end
49
+
47
50
  Timecop.freeze(Date.parse('2090-01-01')) do
48
51
  p.age.should == 100
49
52
  p2.age.should == 100
@@ -154,7 +157,17 @@ describe Personnummer do
154
157
  end
155
158
  end
156
159
 
157
- private
160
+ describe "co-ordination number" do
161
+ it "should accept a samordningsnummer" do
162
+ Personnummer.new('701063-2391').should be_valid
163
+ end
164
+ it "should not accept an invalid samordningsnummer with less than 62" do
165
+ Personnummer.new('900161-0017').should_not be_valid
166
+ end
167
+ it "should not accept an invalid samordningsnummer with more than 90" do
168
+ Personnummer.new('900191-0017').should_not be_valid
169
+ end
170
+ end
158
171
 
159
172
  def it_maps_range_to_region(range, region)
160
173
  Personnummer.new('901231-%03d' % range.first).region.should == region
@@ -1,6 +1,4 @@
1
1
  $: << File.join(File.dirname(__FILE__), '..', 'lib')
2
2
 
3
- require 'cover_me'
4
-
5
3
  RSpec.configure do |config|
6
4
  end
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: personnummer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
5
- prerelease:
4
+ version: 0.0.8
6
5
  platform: ruby
7
6
  authors:
8
7
  - Peter Hellberg
@@ -11,118 +10,90 @@ authors:
11
10
  autorequire:
12
11
  bindir: bin
13
12
  cert_chain: []
14
- date: 2012-09-12 00:00:00.000000000 Z
13
+ date: 2014-06-29 00:00:00.000000000 Z
15
14
  dependencies:
16
15
  - !ruby/object:Gem::Dependency
17
16
  name: rake
18
17
  requirement: !ruby/object:Gem::Requirement
19
- none: false
20
18
  requirements:
21
- - - ! '>='
19
+ - - ">="
22
20
  - !ruby/object:Gem::Version
23
21
  version: '0'
24
22
  type: :development
25
23
  prerelease: false
26
24
  version_requirements: !ruby/object:Gem::Requirement
27
- none: false
28
25
  requirements:
29
- - - ! '>='
26
+ - - ">="
30
27
  - !ruby/object:Gem::Version
31
28
  version: '0'
32
29
  - !ruby/object:Gem::Dependency
33
30
  name: rspec
34
31
  requirement: !ruby/object:Gem::Requirement
35
- none: false
36
32
  requirements:
37
- - - ! '>='
33
+ - - ">="
38
34
  - !ruby/object:Gem::Version
39
35
  version: '0'
40
36
  type: :development
41
37
  prerelease: false
42
38
  version_requirements: !ruby/object:Gem::Requirement
43
- none: false
44
39
  requirements:
45
- - - ! '>='
40
+ - - ">="
46
41
  - !ruby/object:Gem::Version
47
42
  version: '0'
48
43
  - !ruby/object:Gem::Dependency
49
44
  name: guard
50
45
  requirement: !ruby/object:Gem::Requirement
51
- none: false
52
46
  requirements:
53
- - - ! '>='
47
+ - - ">="
54
48
  - !ruby/object:Gem::Version
55
49
  version: '0'
56
50
  type: :development
57
51
  prerelease: false
58
52
  version_requirements: !ruby/object:Gem::Requirement
59
- none: false
60
53
  requirements:
61
- - - ! '>='
54
+ - - ">="
62
55
  - !ruby/object:Gem::Version
63
56
  version: '0'
64
57
  - !ruby/object:Gem::Dependency
65
58
  name: guard-bundler
66
59
  requirement: !ruby/object:Gem::Requirement
67
- none: false
68
60
  requirements:
69
- - - ! '>='
61
+ - - ">="
70
62
  - !ruby/object:Gem::Version
71
63
  version: '0'
72
64
  type: :development
73
65
  prerelease: false
74
66
  version_requirements: !ruby/object:Gem::Requirement
75
- none: false
76
67
  requirements:
77
- - - ! '>='
68
+ - - ">="
78
69
  - !ruby/object:Gem::Version
79
70
  version: '0'
80
71
  - !ruby/object:Gem::Dependency
81
72
  name: guard-rspec
82
73
  requirement: !ruby/object:Gem::Requirement
83
- none: false
84
74
  requirements:
85
- - - ! '>='
75
+ - - ">="
86
76
  - !ruby/object:Gem::Version
87
77
  version: '0'
88
78
  type: :development
89
79
  prerelease: false
90
80
  version_requirements: !ruby/object:Gem::Requirement
91
- none: false
92
81
  requirements:
93
- - - ! '>='
94
- - !ruby/object:Gem::Version
95
- version: '0'
96
- - !ruby/object:Gem::Dependency
97
- name: cover_me
98
- requirement: !ruby/object:Gem::Requirement
99
- none: false
100
- requirements:
101
- - - ! '>='
102
- - !ruby/object:Gem::Version
103
- version: '0'
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- none: false
108
- requirements:
109
- - - ! '>='
82
+ - - ">="
110
83
  - !ruby/object:Gem::Version
111
84
  version: '0'
112
85
  - !ruby/object:Gem::Dependency
113
86
  name: timecop
114
87
  requirement: !ruby/object:Gem::Requirement
115
- none: false
116
88
  requirements:
117
- - - ! '>='
89
+ - - ">="
118
90
  - !ruby/object:Gem::Version
119
91
  version: '0'
120
92
  type: :development
121
93
  prerelease: false
122
94
  version_requirements: !ruby/object:Gem::Requirement
123
- none: false
124
95
  requirements:
125
- - - ! '>='
96
+ - - ">="
126
97
  - !ruby/object:Gem::Version
127
98
  version: '0'
128
99
  description: Personnummer handles validation of Swedish personal identity numbers.
@@ -132,8 +103,8 @@ executables: []
132
103
  extensions: []
133
104
  extra_rdoc_files: []
134
105
  files:
135
- - .gitignore
136
- - .rspec
106
+ - ".gitignore"
107
+ - ".rspec"
137
108
  - Changelog
138
109
  - Gemfile
139
110
  - Gemfile.lock
@@ -148,33 +119,26 @@ files:
148
119
  - spec/spec_helper.rb
149
120
  homepage: https://github.com/c7/personnummer/
150
121
  licenses: []
122
+ metadata: {}
151
123
  post_install_message:
152
124
  rdoc_options: []
153
125
  require_paths:
154
126
  - lib
155
127
  required_ruby_version: !ruby/object:Gem::Requirement
156
- none: false
157
128
  requirements:
158
- - - ! '>='
129
+ - - ">="
159
130
  - !ruby/object:Gem::Version
160
131
  version: '0'
161
- segments:
162
- - 0
163
- hash: 1400477195476666740
164
132
  required_rubygems_version: !ruby/object:Gem::Requirement
165
- none: false
166
133
  requirements:
167
- - - ! '>='
134
+ - - ">="
168
135
  - !ruby/object:Gem::Version
169
136
  version: '0'
170
- segments:
171
- - 0
172
- hash: 1400477195476666740
173
137
  requirements: []
174
138
  rubyforge_project:
175
- rubygems_version: 1.8.24
139
+ rubygems_version: 2.3.0
176
140
  signing_key:
177
- specification_version: 3
141
+ specification_version: 4
178
142
  summary: Personnummer handles validation of Swedish personal identity numbers.
179
143
  test_files:
180
144
  - spec/lib/personnummer_spec.rb