henkilotunnus 1.0.0 → 1.0.2

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: fbebb9f1d0698bb8aaf48428483eb3346015a047
4
- data.tar.gz: 4f4374232f57e775aacb6c92a0e16ff2388b47c7
3
+ metadata.gz: 68a85f9b992b872492fe7f6375b94bcfe776da8f
4
+ data.tar.gz: fa98fb9885dd6bea7380a88ffd3c8e9912a394dd
5
5
  SHA512:
6
- metadata.gz: 269047b939ed54892b60a15799ed79fe314b5232f08c195c6836b6e95d852fe9a01b3415e69a532d26722c1fb0aea4f5b07c621d2f9e74672c83fd90927ef736
7
- data.tar.gz: 6fa2933fa72e3be7d396c3ff4975f7c8d95678a8096ceadb666d49a9982c60992988a38b4474b6f64ce0f91a9c0a70b1b61804b5289f384a41d94739962ada40
6
+ metadata.gz: e9d536638bfac999872c4b456eea519163c054d4f3f18060e263c9af2164703af2387bd9eaa2338f7427f9451cf4570589e95e7e081a6b813c75c7c1d36af1d8
7
+ data.tar.gz: e138e6c71c90df0ba38bd23eb4c511d1e50fb5f30c86ca6f0a14ddf6b9308c263a6fc34ef7ddf927c032b43a0a56ae970032e52c8549926cd58f98e71e808267
data/README.md CHANGED
@@ -22,9 +22,10 @@ Hetu is a gem for validating Finnish [personal identification numbers](http://en
22
22
 
23
23
  ```ruby
24
24
  Hetu.valid?("280264-051U")
25
+ Hetu.generate # Generate a random pin
25
26
 
26
27
  class User < ActiveRecord
27
- validates :pin, hetu: true
28
+ validates :pin, hetu: true
28
29
  end
29
30
  ```
30
31
 
@@ -20,8 +20,8 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.10"
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
- spec.add_development_dependency "minitest"
24
- spec.add_development_dependency "timecop"
23
+ spec.add_development_dependency "minitest", "~> 5.8"
24
+ spec.add_development_dependency "timecop", "~> 0.8.0"
25
25
 
26
- spec.add_runtime_dependency "activemodel"
26
+ spec.add_runtime_dependency "activemodel", "~> 4.2"
27
27
  end
@@ -2,12 +2,26 @@ module Henkilotunnus
2
2
  class Hetu
3
3
  GENDERS = ['female', 'male']
4
4
  CENTURIES = { '+' => 1800, '-' => 1900, 'A' => 2000 }
5
+ PERSON_NUMBER_RANGE = 2..899
5
6
  CHECKSUM_CHARS = '0123456789ABCDEFHJKLMNPRSTUVWXY'
6
7
 
7
8
  def self.valid?(pin)
8
9
  new(pin).valid?
9
10
  end
10
11
 
12
+ def self.generate(opts={})
13
+ dob = opts.fetch(:date, Time.at(rand(Date.new(1800, 1, 1).to_time.to_i...Date.today.to_time.to_i)).to_date)
14
+ raw_dob = dob.strftime("%d%m%y")
15
+ person_number = opts.fetch(:person_number, rand(PERSON_NUMBER_RANGE)).to_s.rjust(3, "0")
16
+ century_sign = CENTURIES.key(dob.year - (dob.year % 100))
17
+
18
+ new(raw_dob + century_sign + person_number + compute_checksum(raw_dob, person_number))
19
+ end
20
+
21
+ def self.compute_checksum(raw_dob, person_number)
22
+ CHECKSUM_CHARS[ (raw_dob + person_number).to_i % 31 ]
23
+ end
24
+
11
25
  attr_reader :pin
12
26
 
13
27
  def initialize(pin)
@@ -84,11 +98,11 @@ module Henkilotunnus
84
98
  end
85
99
 
86
100
  def valid_person_number?
87
- (2..899).cover?(person_number.to_i)
101
+ PERSON_NUMBER_RANGE.cover?(person_number.to_i)
88
102
  end
89
103
 
90
104
  def compute_checksum
91
- CHECKSUM_CHARS[ (raw_dob + person_number).to_i % 31 ]
105
+ self.class.compute_checksum(raw_dob, person_number)
92
106
  end
93
107
  end
94
- end
108
+ end
@@ -6,4 +6,4 @@ module Henkilotunnus
6
6
  record.errors.add(attribute, options[:message] || :invalid) unless Hetu.valid?(value)
7
7
  end
8
8
  end
9
- end
9
+ end
@@ -1,3 +1,3 @@
1
1
  module Henkilotunnus
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.2"
3
3
  end
data/lib/henkilotunnus.rb CHANGED
@@ -7,4 +7,4 @@ module Henkilotunnus
7
7
 
8
8
  end
9
9
 
10
- include Henkilotunnus
10
+ include Henkilotunnus
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: henkilotunnus
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Hanhikoski
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-10-08 00:00:00.000000000 Z
11
+ date: 2018-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -42,44 +42,44 @@ dependencies:
42
42
  name: minitest
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: '5.8'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: '5.8'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: timecop
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ">="
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: 0.8.0
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ">="
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0'
68
+ version: 0.8.0
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: activemodel
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ">="
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '0'
75
+ version: '4.2'
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ">="
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '0'
82
+ version: '4.2'
83
83
  description:
84
84
  email:
85
85
  - tech@pikasiirto.fi
@@ -121,9 +121,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
121
  version: '0'
122
122
  requirements: []
123
123
  rubyforge_project:
124
- rubygems_version: 2.4.8
124
+ rubygems_version: 2.6.13
125
125
  signing_key:
126
126
  specification_version: 4
127
127
  summary: Validates Finnish personal identification numbers (henkilötunnus).
128
128
  test_files: []
129
- has_rdoc: