personnummer 0.0.9 → 3.1.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: 032c8bcc1d8384e04c73c0de80184312709cf172
4
- data.tar.gz: b6efa13a2448d00ff2f2e74fb6bbb8c8278ed899
2
+ SHA256:
3
+ metadata.gz: 3dcd80e850a666983b5212119f00f257477686d8e579508b3336274b359a1639
4
+ data.tar.gz: 5cf6b2f2fd0084a10d70dca28da9e6d366d81d0f703533a07aa10754a56f6bbd
5
5
  SHA512:
6
- metadata.gz: c7a092f1bc01b7a7ada2421b0b40cc1c07e5b579b0d338afcf3d1d6524a4c9ac391ca2f6c5cfc2fba408eedeffb825665ebb04c0ec36c6689a50d60b8ac4093e
7
- data.tar.gz: 686f92b1f6d02236ed81825ebfc22fe7915b78679cffe75be5dcc11ac44d597db948a5f2e5d54fbc96dceb6ca50d93e0ce41a59aa33bf761b39431c98d85b0c0
6
+ metadata.gz: 8d579a9dcfaa550c50aba9e5362f7f64b86e0d660255d4c0fbbc5a98eb5f732fa667e986d4361f485d40cbe0fdd02aaf41bf9ea8a7d6f225852e5e6736441b8e
7
+ data.tar.gz: 3bb1d3cd98280c6e7fb5d742d7363ef7190aebb310dd0c43ef96a993d5e9c4f537615b744bbba30f34956f67757ede243ff25a8fe15fd828744012a3f38fa6bb
@@ -0,0 +1,6 @@
1
+ {
2
+ "$schema": "https://docs.renovatebot.com/renovate-schema.json",
3
+ "extends": [
4
+ "local>personnummer/.github:renovate-config"
5
+ ]
6
+ }
@@ -0,0 +1,41 @@
1
+
2
+ name: build
3
+
4
+ on:
5
+ push:
6
+ branches:
7
+ - main
8
+
9
+ # Run tests for any PRs.
10
+ pull_request:
11
+
12
+ jobs:
13
+ build:
14
+ runs-on: ubuntu-latest
15
+
16
+ strategy:
17
+ matrix:
18
+ # Quoted, since YAML would read an unquoted 4.0 as the number 4.
19
+ ruby-version: ['3.3', '3.4', '4.0']
20
+
21
+ steps:
22
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
23
+ - name: Set up Ruby ${{ matrix.ruby-version }}
24
+ uses: ruby/setup-ruby@b3aaceaef7e0ee231b499e7b5f49c03fc1d32199 # v1.338.0
25
+ with:
26
+ ruby-version: ${{ matrix.ruby-version }}
27
+ - name: gem install
28
+ run: gem install minitest timecop
29
+ - name: test
30
+ run: rake
31
+
32
+ # Single stable check for branch protection, so the ruleset does not need
33
+ # to change when the matrix does.
34
+ tests-passed:
35
+ if: always()
36
+ needs: [build]
37
+ runs-on: ubuntu-latest
38
+ steps:
39
+ - name: Fail if any test job failed or was cancelled
40
+ if: contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled')
41
+ run: exit 1
data/.meta.yaml ADDED
@@ -0,0 +1,4 @@
1
+ name: "Ruby"
2
+ maintainer: "@VoxSecundus"
3
+ spec: 3.1
4
+ workflow: "ruby.yml"
data/LICENSE CHANGED
@@ -1,18 +1,21 @@
1
- Copyright (c) 2008 Peter Hellberg
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining a copy of
4
- this software and associated documentation files (the "Software"), to deal in
5
- the Software without restriction, including without limitation the rights to
6
- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7
- the Software, and to permit persons to whom the Software is furnished to do so,
8
- subject to the following conditions:
9
-
1
+ MIT License
2
+
3
+ Copyright (c) 2017-2021 - Personnummer and Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
10
12
  The above copyright notice and this permission notice shall be included in all
11
13
  copies or substantial portions of the Software.
12
-
14
+
13
15
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15
- FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16
- COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
- IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # personnummer [![Build Status](https://github.com/personnummer/ruby/workflows/build/badge.svg)](https://github.com/personnummer/ruby/actions)
2
+
3
+ Validate personal identity numbers.
4
+
5
+ Versions 0.x of the `personnummer` gem are a different library, [c7/personnummer](https://github.com/c7/personnummer) by Peter Hellberg, who handed the gem name over to this project. They are still on RubyGems, and a Gemfile with `gem 'personnummer', '~> 0.1'` keeps getting them. The API there is `Personnummer.new('830428-5912').valid?`, here it is `Personnummer.valid?('830428-5912')`.
6
+
7
+ ## Installation
8
+
9
+ Add this to your `Gemfile`
10
+
11
+ ```
12
+ gem 'personnummer'
13
+ ```
14
+
15
+ Then run `bundle install`
16
+
17
+ ## Example
18
+
19
+ ```ruby
20
+ require 'personnummer'
21
+
22
+ Personnummer.valid?("8507099805")
23
+ # => true
24
+
25
+ pnr = Personnummer.parse("198507099805")
26
+ pnr.format # => "850709-9805"
27
+ pnr.format(true) # => "198507099805"
28
+ pnr.get_date.to_s # => "1985-07-09"
29
+ pnr.get_age # => 41 (in 2026)
30
+ pnr.is_female? # => true
31
+ pnr.is_coord # => false
32
+ pnr.is_interim # => false
33
+ ```
34
+
35
+ `parse` raises `Personnummer::Error` when the input is not a valid number.
36
+
37
+ ### Options
38
+
39
+ `parse` and `valid?` take an options hash.
40
+
41
+ | Option | Default | Description |
42
+ | --------------------------- | ------- | ------------------------------------------------ |
43
+ | `allow_coordination_number` | `true` | Accept coordination numbers (samordningsnummer). |
44
+ | `allow_interim_number` | `false` | Accept interim numbers (T-numbers). |
45
+
46
+ ```ruby
47
+ Personnummer.valid?("000101-T220")
48
+ # => false
49
+
50
+ Personnummer.valid?("000101-T220", allow_interim_number: true)
51
+ # => true
52
+
53
+ Personnummer.valid?("850769-9810", allow_coordination_number: false)
54
+ # => false
55
+ ```
56
+
57
+ See [test/test_personnummer.rb](test/test_personnummer.rb) for more examples.
58
+
59
+ ## In memoriam
60
+
61
+ Fredrik "Frozzare" Forsmo (1991-2026) was the initiator, co-founder and a core contributor of the personnummer project. This library carries his work. He is missed.
62
+
63
+ ## License
64
+
65
+ MIT
data/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
- require 'bundler/gem_tasks'
1
+ require 'rake/testtask'
2
2
 
3
- require 'rspec/core/rake_task'
4
- RSpec::Core::RakeTask.new(:spec)
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ end
5
6
 
6
- task :default => :spec
7
+ desc 'Run tests'
8
+ task :default => :test
data/lib/personnummer.rb CHANGED
@@ -1,210 +1,206 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  require 'date'
3
3
 
4
- class Personnummer
5
- # Public readonly attributes
6
- attr_reader :region, :control_digit
7
-
8
- def initialize(number)
9
- @valid = false
10
-
11
- # Match the number
12
- number = number.to_s
13
-
14
- if number.match(/(\d{2}){0,1}(\d{2})(\d{2})(\d{2})([\-\+]{0,1})(\d{3})(\d{0,1})/)
15
- # Calculate the control digit based on the birth date and serial number
16
- cd = luhn_algorithm("#{$~[2]}#{$~[3]}#{$~[4]}#{$~[6]}")
17
-
18
- # Set control digit to calculated if it's missing
19
- @control_digit = $~[7].nil? || $~[7].empty? ? cd : $~[7].to_i
20
-
21
- # Get the different parts of the number
22
- century = $~[1].to_i
23
- year = $~[2].to_i
24
- month = $~[3].to_i
25
- day = $~[4].to_i
26
- @divider = $~[5]
27
- @serial = $~[6].to_i
28
-
29
- # Set default divider if not present
30
- if @divider.empty?
31
- @divider = '-'
4
+ module Personnummer
5
+ # Inherits from RuntimeError so existing `rescue RuntimeError` keeps working
6
+ class Error < RuntimeError; end
7
+
8
+ DEFAULT_OPTIONS = {
9
+ allow_coordination_number: true,
10
+ allow_interim_number: false
11
+ }.freeze
12
+
13
+ class Personnummer
14
+ def initialize(personnummer, options={})
15
+ parts = get_parts(personnummer)
16
+ # Create instance variable and getter/setter for each pair in `parts` hash
17
+ parts.each do |k,v|
18
+ instance_variable_set("@#{k}", v)
19
+ eigenclass = class<<self; self; end
20
+ eigenclass.class_eval do
21
+ attr_accessor k
22
+ end
32
23
  end
24
+ @options = DEFAULT_OPTIONS.merge(options || {})
33
25
 
34
- # Make the personnummer valid if the checksum is correct
35
- @valid = true if @control_digit == cd && !$~[7].empty?
36
-
37
- @born = Born.new(century, year, month, day, @divider)
38
- @valid = @born.valid? && @valid
26
+ if !valid
27
+ raise Error, "Invalid Personnummer"
28
+ end
39
29
 
40
- # Get the region name
41
- @region = region_name(@serial)
30
+ if !@options[:allow_coordination_number] && is_coord
31
+ raise Error, "Coordination numbers are not allowed"
32
+ end
42
33
 
43
- # Check if the person is female based the serial (even == female)
44
- @female = (@serial % 2 == 0)
45
- else
46
- raise ArgumentError.new, "The supplied personnummer is invalid"
34
+ if !@options[:allow_interim_number] && is_interim
35
+ raise Error, "Interim numbers are not allowed"
36
+ end
47
37
  end
48
- end
49
38
 
50
- def age
51
- @born.age
52
- end
53
-
54
- def born
55
- @born.to_date
56
- end
57
-
58
- def to_s
59
- born_string = born.strftime("%y%m%d")
60
-
61
- if co_ordination_number?
62
- day = born_string[4..6].to_i + 60
63
- born_string = [born_string[0..3], "%02d" % day].join
39
+ # Checks if the Personnummer is a coordination number (Samordningsnummer)
40
+ # (TrueClass/FalseClass)
41
+ def is_coord
42
+ test_date(full_year, @month.to_i, @day.to_i - 60)
64
43
  end
65
44
 
66
- "%s%s%03d%d" % [born_string, @divider, @serial, @control_digit]
67
- end
68
-
69
- def valid?
70
- @valid
71
- end
72
-
73
- def male?
74
- !@female
75
- end
45
+ # Checks if the Personnummer is an interim number (T-nummer)
46
+ # (TrueClass/FalseClass)
47
+ def is_interim
48
+ @num.match?(/\A[TRSUWXJKLMN]/)
49
+ end
76
50
 
77
- def female?
78
- @female
79
- end
51
+ # Returns the short/long formatted number
52
+ def format(long_format=false)
53
+ if long_format
54
+ [@century,@year,@month,@day,@num,@check].join
55
+ else
56
+ [@year,@month,@day,@sep,@num,@check].join
57
+ end
58
+ end
80
59
 
81
- def co_ordination_number?
82
- @born.co_ordination_number?
83
- end
60
+ # Returns the age of the Personnummer's owner
61
+ # (Integer)
62
+ def get_age
63
+ today = Time.new
64
+ date = get_date
84
65
 
85
- private
66
+ return today.year - date.year - ((today.month > date.month || (today.month == date.month && today.day >= date.day)) ? 0 : 1)
67
+ end
86
68
 
87
- def luhn_algorithm(number)
88
- multiplications = []
69
+ # Returns the date of birth, with the 60 day offset of coordination numbers removed
70
+ # (Date)
71
+ def get_date
72
+ day = @day.to_i
73
+ day -= 60 if is_coord
89
74
 
90
- number.split(//).each_with_index do |digit, i|
91
- if i % 2 == 0
92
- multiplications << digit.to_i*2
93
- else
94
- multiplications << digit.to_i
95
- end
75
+ Date.new(full_year, @month.to_i, day)
96
76
  end
97
77
 
98
- sum = 0
99
-
100
- multiplications.each do |num|
101
- num.to_s.each_byte do |character|
102
- sum += character.chr.to_i
103
- end
78
+ # Checks if the Personnummer's owner is male
79
+ # (TrueClass/FalseClass)
80
+ def is_male?
81
+ sexDigit = @num[-1].to_i
82
+ sexDigit % 2 == 1
104
83
  end
105
84
 
106
- if sum % 10 == 0
107
- control_digit = 0
108
- else
109
- control_digit = (sum / 10 + 1) * 10 - sum
85
+ # Checks if the Personnummer's owner is female
86
+ # (TrueClass/FalseClass)
87
+ def is_female?
88
+ !is_male?
110
89
  end
111
90
 
112
- control_digit
113
- end
91
+ private
114
92
 
115
- def region_name(code)
116
- # Don't return a region name if the person was born after 1990
117
- # (When the previous region code was changed to a serial number)
118
- if @born.year > 1990
119
- return ''
120
- end
93
+ # Regex magic to split the input into parts hash.
94
+ # Raises Error if regex is refused.
95
+ # (Hash)
96
+ def get_parts(personnummer)
97
+ reg = /\A(\d{2}){0,1}(\d{2})(\d{2})(\d{2})([\+\-]?)((?!000)\d{3}|[TRSUWXJKLMN]\d{2})(\d)\z/
98
+ match = personnummer.to_s.match(reg)
121
99
 
122
- case code
123
- when 000..139 then 'Stockholms Län'
124
- when 140..159 then 'Uppsala län'
125
- when 160..189 then 'Södermanlands län'
126
- when 190..239 then 'Östergötlands län'
127
- when 240..269 then 'Jönköpings län'
128
- when 270..289 then 'Kronobergs län'
129
- when 290..319 then 'Kalmar län'
130
- when 320..329 then 'Gotlands län'
131
- when 330..349 then 'Blekinge län'
132
- when 350..389 then 'Kristianstads län'
133
- when 390..459 then 'Malmöhus län'
134
- when 460..479 then 'Hallands län'
135
- when 480..549 then 'Göteborgs och Bohus län'
136
- when 550..589 then 'Älvsborgs län'
137
- when 590..619 then 'Skaraborgs län'
138
- when 620..649 then 'Värmlands län'
139
- when 650..659 then 'Födda utomlands'
140
- when 660..689 then 'Örebro län'
141
- when 690..709 then 'Västmanlands län'
142
- when 710..739 then 'Kopparbergs län'
143
- when 750..779 then 'Gävleborgs län'
144
- when 780..819 then 'Västernorrlands län'
145
- when 820..849 then 'Jämtlands län'
146
- when 850..889 then 'Västerbottens län'
147
- when 890..929 then 'Norrbottens län'
148
- when 930..999 then 'Födda utomlands eller utländska medborgare födda i Sverige'
149
- end
150
- end
100
+ if !match
101
+ raise Error, "Could not parse #{personnummer} as a valid Personnummer"
102
+ end
151
103
 
152
- class Born
153
- attr_reader :year, :month
104
+ century = match[1]
105
+ year = match[2]
106
+ month = match[3]
107
+ day = match[4]
108
+ sep = match[5]
109
+ num = match[6]
110
+ check = match[7]
111
+
112
+ if !century
113
+ base_year = Time.new.year
114
+ if sep == '+'
115
+ base_year -= 100
116
+ else
117
+ sep = '-'
118
+ end
119
+ full_year = base_year - ((base_year - year.to_i) % 100)
120
+ century = (full_year / 100).to_s
121
+ else
122
+ if Time.new.year - (century + year).to_i < 100
123
+ sep = '-'
124
+ else
125
+ sep = '+'
126
+ end
127
+ end
154
128
 
155
- def initialize(century, year, month, day, divider)
156
- @divider = divider
157
- @day = day
158
- @month = month
159
- @year = calculate_century(century, year) + year
129
+ return {
130
+ century: century,
131
+ year: year,
132
+ month: month,
133
+ day: day,
134
+ sep: sep,
135
+ num: num,
136
+ check: check
137
+ }
160
138
  end
161
139
 
162
- def day
163
- co_ordination_number? ? @day - 60 : @day
140
+ # With the century, since e.g. 1900 is not a leap year, but year 0 is.
141
+ # (Integer)
142
+ def full_year
143
+ "#{@century}#{@year}".to_i
164
144
  end
165
145
 
166
- def co_ordination_number?
167
- @day && 60 < @day and @day < 92
146
+ # Checks if the given values form a valid date
147
+ # (TrueClass/FalseClass)
148
+ def test_date(year, month, day)
149
+ # Date.valid_date? counts negative days from the end of the month, so e.g. day 30 - 60 would pass.
150
+ day > 0 && Date.valid_date?(year, month, day)
168
151
  end
169
152
 
170
- def to_date
171
- Date.parse("#{year}-#{month}-#{day}")
172
- end
153
+ # Checks if the Personnummer is valid
154
+ # (TrueClass/FalseClass)
155
+ def valid
156
+ if @check.length == 0
157
+ return false
158
+ end
173
159
 
174
- def valid?
175
- to_date && true
176
- end
160
+ is_valid = luhn(@year + @month + @day + @num.sub(/\A[TRSUWXJKLMN]/, '1')) == @check.to_i
177
161
 
178
- def age
179
- if Date.today > to_date
180
- (Date.today - to_date).to_i/365
181
- else
182
- 0
162
+ if is_valid && test_date(full_year, @month.to_i, @day.to_i)
163
+ return true
183
164
  end
165
+
166
+ return is_valid && test_date(full_year, @month.to_i, @day.to_i - 60)
184
167
  end
185
168
 
186
- private
169
+ # Implementation of Luhn algorithm for calculating Personnummer checksum
170
+ # (Integer)
171
+ def luhn(str)
172
+ sum = 0
187
173
 
188
- def calculate_century(century, year)
189
- # Get the current date
190
- today = Date.today
191
-
192
- if century == 0
193
- # Decide which century corresponds to the number
194
- if year < (today.year-2000) && @divider == '-'
195
- century = 2000
196
- elsif year < (today.year-2000) && @divider == '+'
197
- century = 1900
198
- elsif @divider == '+'
199
- century = 1800
200
- else
201
- century = 1900
174
+ for i in 0...str.length
175
+ v = str[i].to_i
176
+ v *= 2 - (i % 2)
177
+ if v > 9
178
+ v -= 9
202
179
  end
203
- else
204
- century *= 100
180
+ sum += v
205
181
  end
206
182
 
207
- return century
183
+ ((sum.to_f / 10).ceil * 10 - sum.to_f).to_i
184
+ end
185
+ end
186
+
187
+ # Return Personnummer object from given string/integer with options
188
+ # (Personnummer)
189
+ def self.parse(personnummer, options={})
190
+ Personnummer.new(personnummer, options)
191
+ end
192
+
193
+ # Check validity of string/integer input as Personnummer
194
+ # (TrueClass/FalseClass)
195
+ # Anything but a hash in place of the options is the old include_coord flag
196
+ def self.valid?(personnummer, options={})
197
+ options = {allow_coordination_number: !!options} unless options.is_a?(Hash)
198
+
199
+ begin
200
+ parse(personnummer, options)
201
+ true
202
+ rescue Error
203
+ false
208
204
  end
209
205
  end
210
206
  end
data/personnummer.gemspec CHANGED
@@ -2,25 +2,22 @@
2
2
  $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
- s.name = "personnummer"
6
- s.version = "0.0.9"
7
- s.authors = ["Peter Hellberg", "Victor Koronen", "Sebastian Wallin"]
8
- s.email = ["peter@c7.se"]
9
- s.homepage = "https://github.com/c7/personnummer/"
10
- s.summary = %q{Personnummer handles validation of Swedish personal identity numbers.}
11
- s.description = %q{Personnummer handles validation of Swedish personal identity numbers.}
12
-
13
- s.has_rdoc = false
14
-
15
- s.files = `git ls-files`.split("\n")
16
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
- s.require_paths = ["lib"]
19
-
20
- s.add_development_dependency "rake"
21
- s.add_development_dependency "rspec"
22
- s.add_development_dependency "guard"
23
- s.add_development_dependency "guard-bundler"
24
- s.add_development_dependency "guard-rspec"
25
- s.add_development_dependency "timecop"
26
- end
5
+ s.name = 'personnummer'
6
+ s.version = '3.1.0'
7
+ s.date = '2026-11-13'
8
+ s.summary = 'Validate Swedish social security numbers'
9
+ s.description = 'Validate Swedish social security numbers'
10
+ s.authors = ['Jack Millard']
11
+ s.email = ['millard64@hotmail.co.uk']
12
+ s.files = `git ls-files`.split("\n")
13
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
15
+ s.require_paths = ["lib"]
16
+ s.homepage = 'https://github.com/personnummer/ruby'
17
+ s.license = 'MIT'
18
+ # The oldest Ruby in the CI matrix.
19
+ s.required_ruby_version = '>= 3.3'
20
+ s.add_development_dependency 'rake'
21
+ s.add_development_dependency 'minitest'
22
+ s.add_development_dependency 'timecop'
23
+ end
@@ -0,0 +1,228 @@
1
+ require 'minitest/autorun'
2
+ require 'timecop'
3
+ require 'personnummer'
4
+
5
+ class PersonnummerTest < Minitest::Test
6
+ def test_with_control_digit
7
+ assert_equal true, Personnummer::valid?(8507099805)
8
+ assert_equal true, Personnummer::valid?('198507099805')
9
+ assert_equal true, Personnummer::valid?('198507099813')
10
+ assert_equal true, Personnummer::valid?('850709-9813')
11
+ assert_equal true, Personnummer::valid?('196411139808')
12
+ assert_equal false, Personnummer::valid?('19090527 1474')
13
+ end
14
+
15
+ def test_without_control_digit
16
+ assert_equal false, Personnummer::valid?('19850709980')
17
+ assert_equal false, Personnummer::valid?('19850709981')
18
+ assert_equal false, Personnummer::valid?('19641113980')
19
+ end
20
+
21
+ def test_wrong_personnummer_or_types
22
+ assert_equal false, Personnummer::valid?(nil)
23
+ assert_equal false, Personnummer::valid?([])
24
+ assert_equal false, Personnummer::valid?({})
25
+ assert_equal false, Personnummer::valid?(false)
26
+ assert_equal false, Personnummer::valid?(true)
27
+ assert_equal false, Personnummer::valid?(0)
28
+ assert_equal false, Personnummer::valid?('112233-4455')
29
+ assert_equal false, Personnummer::valid?('19112233-4455')
30
+ assert_equal false, Personnummer::valid?('20112233-4455')
31
+ assert_equal false, Personnummer::valid?('9999999999')
32
+ assert_equal false, Personnummer::valid?('199999999999')
33
+ assert_equal false, Personnummer::valid?('199909193776')
34
+ assert_equal false, Personnummer::valid?('Just a string')
35
+ assert_equal false, Personnummer::valid?('990919+3776')
36
+ assert_equal false, Personnummer::valid?('990919-3776')
37
+ assert_equal false, Personnummer::valid?('9909193776')
38
+ assert_equal false, Personnummer::valid?('20150916-0006')
39
+ end
40
+
41
+ def test_coordination_numbers
42
+ assert_equal true, Personnummer::valid?('198507699802')
43
+ assert_equal false, Personnummer::valid?('850769-9802', false)
44
+ assert_equal false, Personnummer::valid?('198507699810', false)
45
+ assert_equal true, Personnummer::valid?('850769-9810')
46
+ end
47
+
48
+ def test_wrong_coordination_numbers
49
+ assert_equal false, Personnummer::valid?('900161-0017', false)
50
+ assert_equal false, Personnummer::valid?('640893-3231')
51
+ assert_equal false, Personnummer::valid?('750232-4630')
52
+ end
53
+
54
+ def test_is_coord
55
+ assert_equal true, Personnummer.parse('850769-9810').is_coord
56
+ assert_equal false, Personnummer.parse('850709-9813').is_coord
57
+ assert_equal false, Personnummer.parse('750330-4631').is_coord
58
+ end
59
+
60
+ def test_leap_days
61
+ # 2000 is a leap year.
62
+ assert_equal true, Personnummer::valid?('20000229-1540')
63
+ assert_equal true, Personnummer::valid?('000229-1540')
64
+ assert_equal true, Personnummer::valid?('20000289-1547')
65
+
66
+ # 1900 is not.
67
+ assert_equal false, Personnummer::valid?('19000229-1540')
68
+ assert_equal false, Personnummer::valid?('000229+1540')
69
+ assert_equal false, Personnummer::valid?('19000289-1547')
70
+ end
71
+
72
+ def test_format
73
+ assert_equal '640327-3813', Personnummer.parse(6403273813).format
74
+ assert_equal '510818-9167', Personnummer.parse('510818-9167').format
75
+ assert_equal '900101-0017', Personnummer.parse('19900101-0017').format
76
+ assert_equal '130401+2931', Personnummer.parse('19130401+2931').format
77
+ assert_equal '640823-3234', Personnummer.parse('196408233234').format
78
+ assert_equal '000101-0107', Personnummer.parse('0001010107').format
79
+ assert_equal '000101-0107', Personnummer.parse('000101-0107').format
80
+ assert_equal '130401+2931', Personnummer.parse('191304012931').format
81
+ assert_equal '196403273813', Personnummer.parse(6403273813).format(true)
82
+ assert_equal '195108189167', Personnummer.parse('510818-9167').format(true)
83
+ assert_equal '199001010017', Personnummer.parse('19900101-0017').format(true)
84
+ assert_equal '191304012931', Personnummer.parse('19130401+2931').format(true)
85
+ assert_equal '196408233234', Personnummer.parse('196408233234').format(true)
86
+ assert_equal '200001010107', Personnummer.parse('0001010107').format(true)
87
+ assert_equal '200001010107', Personnummer.parse('000101-0107').format(true)
88
+ assert_equal '190001010107', Personnummer.parse('000101+0107').format(true)
89
+ end
90
+
91
+ def test_get_age
92
+ Timecop.freeze(Time.utc(2020, "May", 1, 9, 0,0)) do
93
+ assert_equal 34, Personnummer.parse('198507099805').get_age
94
+ assert_equal 34, Personnummer.parse('198507099813').get_age
95
+ assert_equal 55, Personnummer.parse('196411139808').get_age
96
+ assert_equal 107, Personnummer.parse('19121212+1212').get_age
97
+ assert_equal 34, Personnummer.parse('198507699810').get_age
98
+ assert_equal 34, Personnummer.parse('198507699802').get_age
99
+ end
100
+ end
101
+
102
+ def test_is_male
103
+ assert_equal false, Personnummer.parse('198507099805').is_male?
104
+ assert_equal true, Personnummer.parse('198507099813').is_male?
105
+ assert_equal false, Personnummer.parse('196411139808').is_male?
106
+ assert_equal true, Personnummer.parse('19121212+1212').is_male?
107
+ assert_equal true, Personnummer.parse('198507699810').is_male?
108
+ assert_equal false, Personnummer.parse('198507699802').is_male?
109
+ end
110
+
111
+ def test_is_female
112
+ assert_equal true, Personnummer.parse('198507099805').is_female?
113
+ assert_equal false, Personnummer.parse('198507099813').is_female?
114
+ assert_equal true, Personnummer.parse('196411139808').is_female?
115
+ assert_equal false, Personnummer.parse('19121212+1212').is_female?
116
+ assert_equal false, Personnummer.parse('198507699810').is_female?
117
+ assert_equal true, Personnummer.parse('198507699802').is_female?
118
+ end
119
+
120
+ INTERIM_LETTERS = %w[T R S U W X J K L M N]
121
+ INTERIM_OPTS = {allow_interim_number: true}
122
+
123
+ def test_interim_valid_with_option
124
+ INTERIM_LETTERS.each do |l|
125
+ ["20000101#{l}220", "000101#{l}220", "000101-#{l}220", "20000101-#{l}220"].each do |n|
126
+ assert_equal true, Personnummer.valid?(n, INTERIM_OPTS), n
127
+ end
128
+ end
129
+ assert_equal false, Personnummer.valid?('20000101E221', INTERIM_OPTS)
130
+ assert_equal false, Personnummer.valid?('000101-E221', INTERIM_OPTS)
131
+ end
132
+
133
+ def test_interim_rejected_by_default
134
+ INTERIM_LETTERS.each do |l|
135
+ assert_equal false, Personnummer.valid?("20000101#{l}220"), l
136
+ assert_equal false, Personnummer.valid?("000101-#{l}220"), l
137
+ assert_equal false, Personnummer.valid?("000101-#{l}220", allow_interim_number: false), l
138
+ assert_raises(Personnummer::Error) { Personnummer.parse("000101-#{l}220") }
139
+ end
140
+ end
141
+
142
+ def test_interim_format
143
+ INTERIM_LETTERS.each do |l|
144
+ ["20000101#{l}220", "000101#{l}220", "000101-#{l}220", "20000101-#{l}220"].each do |n|
145
+ p = Personnummer.parse(n, INTERIM_OPTS)
146
+ assert_equal "000101-#{l}220", p.format, n
147
+ assert_equal "20000101#{l}220", p.format(true), n
148
+ end
149
+ end
150
+ end
151
+
152
+ def test_is_interim
153
+ INTERIM_LETTERS.each do |l|
154
+ assert_equal true, Personnummer.parse("20000101#{l}220", INTERIM_OPTS).is_interim
155
+ end
156
+ assert_equal false, Personnummer.parse('850709-9813').is_interim
157
+ assert_equal false, Personnummer.parse('850769-9810').is_interim
158
+ end
159
+
160
+ def test_interim_sex
161
+ assert_equal true, Personnummer.parse('000101-T220', INTERIM_OPTS).is_female?
162
+ assert_equal false, Personnummer.parse('000101-T220', INTERIM_OPTS).is_male?
163
+ end
164
+
165
+ def test_get_date
166
+ assert_equal Date.new(1985, 7, 9), Personnummer.parse('198507099805').get_date
167
+ assert_equal Date.new(1985, 7, 9), Personnummer.parse('850709-9813').get_date
168
+ assert_equal Date.new(1985, 7, 9), Personnummer.parse('198507699810').get_date
169
+ assert_equal Date.new(2000, 1, 1), Personnummer.parse('000101-T220', INTERIM_OPTS).get_date
170
+ end
171
+
172
+ def test_get_age_interim
173
+ Timecop.freeze(Time.utc(2020, "May", 1, 9, 0, 0)) do
174
+ assert_equal 20, Personnummer.parse('000101-T220', INTERIM_OPTS).get_age
175
+ end
176
+ end
177
+
178
+ def test_option_allow_coordination_number
179
+ assert_equal true, Personnummer.valid?('850769-9810', allow_coordination_number: true)
180
+ assert_equal false, Personnummer.valid?('850769-9810', allow_coordination_number: false)
181
+ assert_equal true, Personnummer.valid?('850709-9813', allow_coordination_number: false)
182
+ assert_equal true, Personnummer.parse('850769-9810').is_coord
183
+ assert_equal true, Personnummer.parse('850769-9810', allow_coordination_number: true).is_coord
184
+ assert_raises(Personnummer::Error) { Personnummer.parse('850769-9810', allow_coordination_number: false) }
185
+ assert_raises(Personnummer::Error) { Personnummer::Personnummer.new('850769-9810', allow_coordination_number: false) }
186
+ assert_equal false, Personnummer.parse('850709-9813', allow_coordination_number: false).is_coord
187
+ end
188
+
189
+ def test_option_allow_interim_number
190
+ assert_equal true, Personnummer.valid?('000101-T220', allow_interim_number: true)
191
+ assert_equal false, Personnummer.valid?('000101-T220', allow_interim_number: false)
192
+ assert_equal true, Personnummer.parse('000101-T220', allow_interim_number: true).is_interim
193
+ assert_raises(Personnummer::Error) { Personnummer::Personnummer.new('000101-T220') }
194
+ assert_equal true, Personnummer.valid?('850709-9813', allow_interim_number: true)
195
+ # Options combine
196
+ assert_equal true, Personnummer.valid?('000101-T220', allow_interim_number: true, allow_coordination_number: false)
197
+ assert_equal false, Personnummer.valid?('850769-9810', allow_interim_number: true, allow_coordination_number: false)
198
+ end
199
+
200
+ def test_valid_boolean_form
201
+ assert_equal true, Personnummer.valid?('850769-9810', true)
202
+ assert_equal false, Personnummer.valid?('850769-9810', false)
203
+ assert_equal true, Personnummer.valid?('850709-9813', false)
204
+ assert_equal false, Personnummer.valid?('000101-T220', true)
205
+ # The old flag was only tested for truthiness, and valid? must never raise.
206
+ assert_equal false, Personnummer.valid?('850769-9810', nil)
207
+ assert_equal true, Personnummer.valid?('850769-9810', 'yes')
208
+ end
209
+
210
+ def test_anchoring
211
+ assert_equal false, Personnummer.valid?('xx8507099805yy')
212
+ assert_equal false, Personnummer.valid?('xx198507099805')
213
+ assert_equal false, Personnummer.valid?('198507099805yy')
214
+ assert_equal false, Personnummer.valid?(" 198507099805")
215
+ assert_equal false, Personnummer.valid?("198507099805\n")
216
+ assert_equal false, Personnummer.valid?("x000101-T220", INTERIM_OPTS)
217
+ assert_equal false, Personnummer.valid?("000101-T220x", INTERIM_OPTS)
218
+ end
219
+
220
+ def test_error_class
221
+ assert_operator Personnummer::Error, :<, RuntimeError
222
+ error = assert_raises(Personnummer::Error) { Personnummer.parse('Just a string') }
223
+ assert_equal 'Could not parse Just a string as a valid Personnummer', error.message
224
+ error = assert_raises(Personnummer::Error) { Personnummer.parse('990919-3776') }
225
+ assert_equal 'Invalid Personnummer', error.message
226
+ assert_raises(RuntimeError) { Personnummer.parse('990919-3776') }
227
+ end
228
+ end
metadata CHANGED
@@ -1,16 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: personnummer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
- - Peter Hellberg
8
- - Victor Koronen
9
- - Sebastian Wallin
10
- autorequire:
7
+ - Jack Millard
11
8
  bindir: bin
12
9
  cert_chain: []
13
- date: 2016-09-01 00:00:00.000000000 Z
10
+ date: 2026-11-13 00:00:00.000000000 Z
14
11
  dependencies:
15
12
  - !ruby/object:Gem::Dependency
16
13
  name: rake
@@ -27,49 +24,7 @@ dependencies:
27
24
  - !ruby/object:Gem::Version
28
25
  version: '0'
29
26
  - !ruby/object:Gem::Dependency
30
- name: rspec
31
- requirement: !ruby/object:Gem::Requirement
32
- requirements:
33
- - - ">="
34
- - !ruby/object:Gem::Version
35
- version: '0'
36
- type: :development
37
- prerelease: false
38
- version_requirements: !ruby/object:Gem::Requirement
39
- requirements:
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- version: '0'
43
- - !ruby/object:Gem::Dependency
44
- name: guard
45
- requirement: !ruby/object:Gem::Requirement
46
- requirements:
47
- - - ">="
48
- - !ruby/object:Gem::Version
49
- version: '0'
50
- type: :development
51
- prerelease: false
52
- version_requirements: !ruby/object:Gem::Requirement
53
- requirements:
54
- - - ">="
55
- - !ruby/object:Gem::Version
56
- version: '0'
57
- - !ruby/object:Gem::Dependency
58
- name: guard-bundler
59
- requirement: !ruby/object:Gem::Requirement
60
- requirements:
61
- - - ">="
62
- - !ruby/object:Gem::Version
63
- version: '0'
64
- type: :development
65
- prerelease: false
66
- version_requirements: !ruby/object:Gem::Requirement
67
- requirements:
68
- - - ">="
69
- - !ruby/object:Gem::Version
70
- version: '0'
71
- - !ruby/object:Gem::Dependency
72
- name: guard-rspec
27
+ name: minitest
73
28
  requirement: !ruby/object:Gem::Requirement
74
29
  requirements:
75
30
  - - ">="
@@ -96,31 +51,26 @@ dependencies:
96
51
  - - ">="
97
52
  - !ruby/object:Gem::Version
98
53
  version: '0'
99
- description: Personnummer handles validation of Swedish personal identity numbers.
54
+ description: Validate Swedish social security numbers
100
55
  email:
101
- - peter@c7.se
56
+ - millard64@hotmail.co.uk
102
57
  executables: []
103
58
  extensions: []
104
59
  extra_rdoc_files: []
105
60
  files:
106
- - ".gitignore"
107
- - ".rspec"
108
- - Changelog
109
- - Gemfile
110
- - Gemfile.lock
111
- - Guardfile
61
+ - ".github/renovate.json"
62
+ - ".github/workflows/ruby.yml"
63
+ - ".meta.yaml"
112
64
  - LICENSE
113
- - README.markdown
65
+ - README.md
114
66
  - Rakefile
115
- - demo.rb
116
67
  - lib/personnummer.rb
117
68
  - personnummer.gemspec
118
- - spec/lib/personnummer_spec.rb
119
- - spec/spec_helper.rb
120
- homepage: https://github.com/c7/personnummer/
121
- licenses: []
69
+ - test/test_personnummer.rb
70
+ homepage: https://github.com/personnummer/ruby
71
+ licenses:
72
+ - MIT
122
73
  metadata: {}
123
- post_install_message:
124
74
  rdoc_options: []
125
75
  require_paths:
126
76
  - lib
@@ -128,18 +78,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
128
78
  requirements:
129
79
  - - ">="
130
80
  - !ruby/object:Gem::Version
131
- version: '0'
81
+ version: '3.3'
132
82
  required_rubygems_version: !ruby/object:Gem::Requirement
133
83
  requirements:
134
84
  - - ">="
135
85
  - !ruby/object:Gem::Version
136
86
  version: '0'
137
87
  requirements: []
138
- rubyforge_project:
139
- rubygems_version: 2.5.1
140
- signing_key:
88
+ rubygems_version: 3.6.2
141
89
  specification_version: 4
142
- summary: Personnummer handles validation of Swedish personal identity numbers.
90
+ summary: Validate Swedish social security numbers
143
91
  test_files:
144
- - spec/lib/personnummer_spec.rb
145
- - spec/spec_helper.rb
92
+ - test/test_personnummer.rb
data/.gitignore DELETED
@@ -1,4 +0,0 @@
1
- coverage/*
2
- coverage.data
3
- personnummer-*.gem
4
- pkg/*
data/.rspec DELETED
@@ -1 +0,0 @@
1
- --colour
data/Changelog DELETED
@@ -1,33 +0,0 @@
1
- 0.0.9 Released
2
-
3
- 2016-09-01: Return correct standardized string for samordningsnummer
4
-
5
- 0.0.8 Released
6
-
7
- 2014-06-29: Support for co-ordination numbers (theneubeck)
8
-
9
- 0.0.7 Released
10
-
11
- 2012-09-12: Don't alter an existing control digit, even if it’s wrong
12
-
13
- 0.0.6 Released
14
-
15
- 2011-04-18: Now supports century if given
16
-
17
- 0.0.5 Released
18
-
19
- 2011-11-25: Tests!
20
-
21
- 0.0.4 Released
22
-
23
- 2011-01-09: Ruby 1.9 support
24
-
25
- 0.0.3 Released
26
-
27
- 2010-04-19: (Less) naïve age calculation
28
-
29
- 0.0.2 Released
30
-
31
- 2008-08-07: Number argument can now be a Fixnum or String
32
-
33
- 0.0.1 Released
data/Gemfile DELETED
@@ -1,4 +0,0 @@
1
- source "http://rubygems.org"
2
-
3
- # Specify your gem's dependencies in personnummer.gemspec
4
- gemspec
data/Gemfile.lock DELETED
@@ -1,44 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- personnummer (0.0.9)
5
-
6
- GEM
7
- remote: http://rubygems.org/
8
- specs:
9
- diff-lcs (1.1.3)
10
- guard (1.3.2)
11
- listen (>= 0.4.2)
12
- thor (>= 0.14.6)
13
- guard-bundler (1.0.0)
14
- bundler (~> 1.0)
15
- guard (~> 1.1)
16
- guard-rspec (1.2.1)
17
- guard (>= 1.1)
18
- listen (0.5.0)
19
- rake (0.9.2.2)
20
- rspec (2.11.0)
21
- rspec-core (~> 2.11.0)
22
- rspec-expectations (~> 2.11.0)
23
- rspec-mocks (~> 2.11.0)
24
- rspec-core (2.11.1)
25
- rspec-expectations (2.11.3)
26
- diff-lcs (~> 1.1.3)
27
- rspec-mocks (2.11.2)
28
- thor (0.16.0)
29
- timecop (0.4.6)
30
-
31
- PLATFORMS
32
- ruby
33
-
34
- DEPENDENCIES
35
- guard
36
- guard-bundler
37
- guard-rspec
38
- personnummer!
39
- rake
40
- rspec
41
- timecop
42
-
43
- BUNDLED WITH
44
- 1.11.2
data/Guardfile DELETED
@@ -1,14 +0,0 @@
1
- # A sample Guardfile
2
- # More info at https://github.com/guard/guard#readme
3
-
4
- guard 'bundler' do
5
- watch('Gemfile')
6
- watch(/^.+\.gemspec/)
7
- end
8
-
9
- guard 'rspec', :version => 2, :all_after_pass => false, :all_on_start => false do
10
- watch(%r{^spec/.+_spec\.rb$})
11
- watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
12
- watch('spec/spec_helper.rb') { "spec" }
13
- end
14
-
data/README.markdown DELETED
@@ -1,35 +0,0 @@
1
- # Personnummer
2
-
3
- Personnummer is a ruby class that handles [Swedish personal identity number](http://en.wikipedia.org/wiki/Personal_identity_number_(Sweden)).
4
-
5
- ### Installation
6
-
7
- $ gem install personnummer
8
-
9
- ### Example usage
10
-
11
- ```
12
- >> p = Personnummer.new('830428-5912')
13
- #>>Personnummer:0x007fc971369100 @valid=true, @control_digit=2, @divider="-", @serial=591, @born=#>>PersonnummerDate:0x007fc971368318 @divider="-", @day=28, @month=4, @year=1983> #<, @region="Skaraborgs län", @female=false> #<
14
-
15
- >> p.valid?
16
- => true
17
-
18
- >> p = Personnummer.new('830428')
19
- => ArgumentError: The supplied personnummer is invalid
20
- ```
21
-
22
- ### This implementation also supports co-ordination numbers
23
-
24
- [Information about co-ordination numbers (in swedish)](https://www.skatteverket.se/rattsinformation/reglerochstallningstaganden/meddelanden/2010/meddelanden2010/skvm201003.5.233f91f71260075abe8800024297.html)
25
-
26
- These numbers are obtained by adding 60 to the day of the birth date
27
-
28
- ```
29
- >> p = Personnummer.new('830488-5912')
30
- => #<Personnummer:0x007fbafd1458d8 ...>
31
- >> p.born
32
- => #<Date: 1983-04-28 ((2445453j,0s,0n),+0s,2299161j)>
33
- >> p.co_ordination_number?
34
- => true
35
- ```
data/demo.rb DELETED
@@ -1,21 +0,0 @@
1
- # Real world scripts would use:
2
- # require 'rubygems'
3
- # require 'personnummer'
4
- #
5
- # Instead of:
6
- require './lib/personnummer'
7
-
8
- # Valid personnummer
9
- p = Personnummer.new(8304285912)
10
- designation = (p.male?) ? 'His' : 'Her'
11
- puts "#{designation} control digit is: #{p.control_digit}"
12
-
13
- if p.valid?
14
- designation = (p.male?) ? 'He' : 'She'
15
- puts "#{designation} was born in #{p.region} aproximately #{p.age} years ago" if p.region
16
- end
17
-
18
- # Incomplete personnummer
19
- p = Personnummer.new('870312-594')
20
- designation = (p.male?) ? 'His' : 'Her'
21
- puts "#{designation} control digit is: #{p.control_digit}"
@@ -1,177 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
- require 'spec_helper'
3
- require 'personnummer'
4
- require 'timecop'
5
-
6
- describe Personnummer do
7
- describe "initialize" do
8
- it "accepts a valid personnummer (with control digit)" do
9
- expect{ Personnummer.new(9001010017) }.not_to raise_error(ArgumentError)
10
- expect{ Personnummer.new('900101-0017') }.not_to raise_error(ArgumentError)
11
- expect{ Personnummer.new('100101+0017') }.not_to raise_error(ArgumentError)
12
- expect{ Personnummer.new('19900101-0017') }.not_to raise_error(ArgumentError)
13
- end
14
-
15
- it "accepts a valid personnummer (without control digit)" do
16
- expect{ Personnummer.new(900101001) }.not_to raise_error(ArgumentError)
17
- expect{ Personnummer.new('900101-001') }.not_to raise_error(ArgumentError)
18
- expect{ Personnummer.new('100101+001') }.not_to raise_error(ArgumentError)
19
- expect{ Personnummer.new('19900101-001') }.not_to raise_error(ArgumentError)
20
- end
21
-
22
- it "throws ArgumentError on an invalid personnummer" do
23
- expect{ Personnummer.new(nil) }.to raise_error(ArgumentError)
24
- expect{ Personnummer.new(17) }.to raise_error(ArgumentError)
25
- expect{ Personnummer.new('112233-4455') }.to raise_error(ArgumentError)
26
- expect{ Personnummer.new('19112233-4455') }.to raise_error(ArgumentError)
27
- end
28
- end
29
-
30
- describe "age" do
31
- it "calculates age correctly" do
32
- p = Personnummer.new('900101-001')
33
- p2 = Personnummer.new('19900101-001')
34
-
35
- Timecop.freeze(Date.parse('1980-01-01')) do
36
- p.age.should == 0
37
- p2.age.should == 0
38
- end
39
-
40
- Timecop.freeze(Date.parse('1990-01-01')) do
41
- p.age.should == 0
42
- p2.age.should == 0
43
- end
44
-
45
- Timecop.freeze(Date.parse('2000-01-01')) do
46
- p.age.should == 10
47
- p2.age.should == 10
48
- end
49
-
50
- Timecop.freeze(Date.parse('2090-01-01')) do
51
- p.age.should == 100
52
- p2.age.should == 100
53
- end
54
- end
55
- end
56
-
57
- describe "born" do
58
- it "returns the correct date" do
59
- Timecop.freeze(Date.parse('2010-01-01')) do
60
- Personnummer.new('900101-001').born.should == Date.parse('1990-01-01')
61
- Personnummer.new('010101-001').born.should == Date.parse('2001-01-01')
62
- Personnummer.new('100101+001').born.should == Date.parse('1810-01-01')
63
- Personnummer.new('19900101-001').born.should == Date.parse('1990-01-01')
64
- Personnummer.new('18900101-001').born.should == Date.parse('1890-01-01')
65
- end
66
- end
67
- end
68
-
69
- describe "region" do
70
- it "returns '' for persons born on or after 1991-01-01" do
71
- Personnummer.new('910101-001').region.should == ''
72
- end
73
-
74
- it "returns the correct region for persons born before 1991-01-01" do
75
- it_maps_range_to_region 000..139, 'Stockholms Län'
76
- it_maps_range_to_region 140..159, 'Uppsala län'
77
- it_maps_range_to_region 160..189, 'Södermanlands län'
78
- it_maps_range_to_region 190..239, 'Östergötlands län'
79
- it_maps_range_to_region 240..269, 'Jönköpings län'
80
- it_maps_range_to_region 270..289, 'Kronobergs län'
81
- it_maps_range_to_region 290..319, 'Kalmar län'
82
- it_maps_range_to_region 320..329, 'Gotlands län'
83
- it_maps_range_to_region 330..349, 'Blekinge län'
84
- it_maps_range_to_region 350..389, 'Kristianstads län'
85
- it_maps_range_to_region 390..459, 'Malmöhus län'
86
- it_maps_range_to_region 460..479, 'Hallands län'
87
- it_maps_range_to_region 480..549, 'Göteborgs och Bohus län'
88
- it_maps_range_to_region 550..589, 'Älvsborgs län'
89
- it_maps_range_to_region 590..619, 'Skaraborgs län'
90
- it_maps_range_to_region 620..649, 'Värmlands län'
91
- it_maps_range_to_region 650..659, 'Födda utomlands'
92
- it_maps_range_to_region 660..689, 'Örebro län'
93
- it_maps_range_to_region 690..709, 'Västmanlands län'
94
- it_maps_range_to_region 710..739, 'Kopparbergs län'
95
- it_maps_range_to_region 750..779, 'Gävleborgs län'
96
- it_maps_range_to_region 780..819, 'Västernorrlands län'
97
- it_maps_range_to_region 820..849, 'Jämtlands län'
98
- it_maps_range_to_region 850..889, 'Västerbottens län'
99
- it_maps_range_to_region 890..929, 'Norrbottens län'
100
- it_maps_range_to_region 930..999, 'Födda utomlands eller utländska medborgare födda i Sverige'
101
- end
102
- end
103
-
104
- describe "control_digit" do
105
- it "responds with the right control digit" do
106
- Personnummer.new(9001010017).control_digit.should == 7
107
- Personnummer.new('900101-0017').control_digit.should == 7
108
- Personnummer.new('900101-001').control_digit.should == 7
109
- end
110
-
111
- it "should not alter existing control digit, even if it is invalid" do
112
- p = Personnummer.new("901010-0010")
113
- p.to_s.should == "901010-0010"
114
- p.should_not be_valid
115
- end
116
- end
117
-
118
- describe "to_s" do
119
- it "uses the standard string representation" do
120
- Personnummer.new(9001010017).to_s.should == '900101-0017'
121
- Personnummer.new('900101-0017').to_s.should == '900101-0017'
122
- Personnummer.new('900101-001').to_s.should == '900101-0017'
123
- Personnummer.new('19900101-001').to_s.should == '900101-0017'
124
- Personnummer.new('18900101-001').to_s.should == '900101-0017'
125
- Personnummer.new('7010632391').to_s.should == '701063-2391' # Samordningsnummer
126
- end
127
- end
128
-
129
- describe "valid?" do
130
- it "marks a valid personnummer with control digit as valid" do
131
- Personnummer.new('900101-0017').should be_valid
132
- end
133
-
134
- it "marks a valid personnummer without control digit as invalid" do
135
- Personnummer.new('900101-001').should_not be_valid
136
- end
137
-
138
- it "throws ArgumentError on an invalid personnummer" do
139
- expect{ Personnummer.new('112233-4455') }.to raise_error(ArgumentError)
140
- end
141
- end
142
-
143
- describe "gender" do
144
- it "recognizes males" do
145
- Personnummer.new('900101-001').should be_male
146
- Personnummer.new('900101-003').should be_male
147
- Personnummer.new('900101-005').should be_male
148
- Personnummer.new('900101-007').should be_male
149
- Personnummer.new('900101-009').should be_male
150
- end
151
-
152
- it "recognizes females" do
153
- Personnummer.new('900101-000').should be_female
154
- Personnummer.new('900101-002').should be_female
155
- Personnummer.new('900101-004').should be_female
156
- Personnummer.new('900101-006').should be_female
157
- Personnummer.new('900101-008').should be_female
158
- end
159
- end
160
-
161
- describe "co-ordination number" do
162
- it "should accept a samordningsnummer" do
163
- Personnummer.new('701063-2391').should be_valid
164
- end
165
- it "should not accept an invalid samordningsnummer with less than 62" do
166
- Personnummer.new('900161-0017').should_not be_valid
167
- end
168
- it "should not accept an invalid samordningsnummer with more than 90" do
169
- Personnummer.new('900191-0017').should_not be_valid
170
- end
171
- end
172
-
173
- def it_maps_range_to_region(range, region)
174
- Personnummer.new('901231-%03d' % range.first).region.should == region
175
- Personnummer.new('901231-%03d' % range.last).region.should == region
176
- end
177
- end
data/spec/spec_helper.rb DELETED
@@ -1,4 +0,0 @@
1
- $: << File.join(File.dirname(__FILE__), '..', 'lib')
2
-
3
- RSpec.configure do |config|
4
- end