identitycode 0.1.0 → 0.1.1

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
2
  SHA1:
3
- metadata.gz: d0abbca12ef7f4e0b66b94337365ae1cc549635e
4
- data.tar.gz: f58e83b7344b0a3119575b97bf05a0aff83cfcf7
3
+ metadata.gz: 7196156cbecfcb993afb71a46f6b48d6f294a641
4
+ data.tar.gz: f2b1f7f8197330faa25c29524901074ccbf1e602
5
5
  SHA512:
6
- metadata.gz: d1e9f42fcd99876446d2c7d76bcbc6ea0d914561f0b9991cc378108b786a4a15ddd0e8774fcf5b8a0cece391e1550311f8ea471847132387edde6ef3e3e55eda
7
- data.tar.gz: 29d024d617b2db4da1b1edcc72a5c5338644680936c5dbd25a58e8eede8de351fbbb0f8511b16794b4a6f0651a8795d56f98a8259ef3bcad626dd0faadafaa9a
6
+ metadata.gz: 8da6d8d81b53539c9f7368025f99165e22093ce799354b97e6f20e799c92fc1850e6d66dff39b422e39bb3f6b787f746025a9bbe16d867b286bce214a7a29b37
7
+ data.tar.gz: 59077ea20372e671faa5cd5a5b3cbcf5475670858b69a4fb393740a8593a6bbb94ff60cf26ca1e4d9e338319b008be3c769c0f89511f3114cb6e105827fd2926
data/README.md CHANGED
@@ -22,6 +22,8 @@ Or install it yourself as:
22
22
  > require 'identity_code'
23
23
  > code = IdentityCode::Isikukood.new('38312203720')
24
24
  > code.valid?
25
+ # or
26
+ > IdentityCode::Isikukood.valid?('38312203720')
25
27
  => true
26
28
  > code.sex
27
29
  => 'M'
@@ -29,6 +31,11 @@ Or install it yourself as:
29
31
  => '1983-12-20'
30
32
  > code.age
31
33
  => 31
34
+ # Generate random valid identity code
35
+ > IdentityCode::Isikukood.generate
36
+ => '37504163700'
37
+ > IdentityCode::Isikukood.generate(sex: 'M', year: 1983, month: 12, day: 20)
38
+ => '38312209528'
32
39
  ```
33
40
 
34
41
  ## Development
data/Rakefile CHANGED
@@ -1 +1 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
data/identitycode.gemspec CHANGED
@@ -4,21 +4,21 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'identity_code/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "identitycode"
7
+ spec.name = 'identitycode'
8
8
  spec.version = IdentityCode::VERSION
9
- spec.authors = ["Artem Pakk"]
10
- spec.email = ["apakk@me.com"]
9
+ spec.authors = ['Artem Pakk']
10
+ spec.email = ['apakk@me.com']
11
11
 
12
12
  spec.summary = %q{Ruby gem to generate and validate Estonian identity codes}
13
13
  spec.description = %q{Ruby gem to generate and validate Estonian identity codes}
14
- spec.homepage = "https://github.com/defeed/identitycode"
14
+ spec.homepage = 'https://github.com/defeed/identitycode'
15
15
  spec.license = "MIT"
16
16
 
17
17
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
- spec.bindir = "exe"
18
+ spec.bindir = 'exe'
19
19
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
- spec.require_paths = ["lib"]
20
+ spec.require_paths = ['lib']
21
21
 
22
- spec.add_development_dependency "bundler", "~> 1.10"
23
- spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency 'bundler', '~> 1.10'
23
+ spec.add_development_dependency 'rake', "~> 10.0"
24
24
  end
data/lib/identity_code.rb CHANGED
@@ -3,22 +3,79 @@ require 'date'
3
3
 
4
4
  module IdentityCode
5
5
  class Isikukood
6
- attr_reader :code
6
+ HOSPITALS = [
7
+ '00', # Kuressaare Haigla (järjekorranumbrid 001 kuni 020)
8
+ '01', # Tartu Ülikooli Naistekliinik, Tartumaa, Tartu (011...019)
9
+ '02', # Ida-Tallinna Keskhaigla, Hiiumaa, Keila, Rapla haigla (021...220)
10
+ '22', # Ida-Viru Keskhaigla (Kohtla-Järve, endine Jõhvi) (221...270)
11
+ '27', # Maarjamõisa Kliinikum (Tartu), Jõgeva Haigla (271...370)
12
+ '37', # Narva Haigla (371...420)
13
+ '42', # Pärnu Haigla (421...470)
14
+ '47', # Pelgulinna Sünnitusmaja (Tallinn), Haapsalu haigla (471...490)
15
+ '49', # Järvamaa Haigla (Paide) (491...520)
16
+ '52', # Rakvere, Tapa haigla (521...570)
17
+ '57', # Valga Haigla (571...600)
18
+ '60', # Viljandi Haigla (601...650)
19
+ '65', # Lõuna-Eesti Haigla (Võru), Pälva Haigla (651...710?)
20
+ '70', # All other hospitals
21
+ '95' # Foreigners who are born in Estonia
22
+ ]
23
+
24
+ NUM_DAYS = {
25
+ 1 => 31,
26
+ 2 => 28,
27
+ 3 => 31,
28
+ 4 => 30,
29
+ 5 => 31,
30
+ 6 => 30,
31
+ 7 => 31,
32
+ 8 => 31,
33
+ 9 => 30,
34
+ 10 => 31,
35
+ 11 => 30,
36
+ 12 => 31
37
+ }
38
+
39
+ def self.generate(opts = {})
40
+ first_digit = 0
41
+
42
+ sex = opts[:sex] || rand.round == 0 ? 'M' : 'F'
43
+ year = opts[:year] || rand(Date.today.year - 90..Date.today.year - 1)
44
+ month = opts[:month] || rand(1..12)
45
+ day = opts[:day] || rand(1..NUM_DAYS[month])
46
+
47
+ first_digit += 1 if (1800..1899).include?(year)
48
+ first_digit += 3 if (1900..1999).include?(year)
49
+ first_digit += 5 if year >= 2000
50
+ first_digit += 1 if sex == 'F'
51
+
52
+ result = first_digit.to_s
53
+ result += "%02d" % year.to_s[2..3].to_i
54
+ result += "%02d" % month
55
+ result += "%02d" % day
56
+ result += HOSPITALS[(rand * HOSPITALS.size - 1).round]
57
+ result += rand(0..9).to_s
58
+ result += new(result).control_code.to_s
59
+ end
60
+
61
+ def self.valid?(code)
62
+ new(code).valid?
63
+ end
7
64
 
8
65
  def initialize(code)
9
66
  @code = code.to_s
10
67
  end
11
68
 
12
69
  def valid?
13
- code.length == 11 &&
14
- code[10].chr.to_i == control_code
70
+ @code.length == 11 &&
71
+ @code[10].chr.to_i == control_code
15
72
  end
16
73
 
17
74
  def birth_date
18
75
  return unless valid?
19
- year = century + code[1..2].to_i
20
- month = code[3..4].to_i
21
- day = code[5..6].to_i
76
+ year = century + @code[1..2].to_i
77
+ month = @code[3..4].to_i
78
+ day = @code[5..6].to_i
22
79
  return unless Date.valid_date?(year, month, day)
23
80
  Date.new(year, month, day)
24
81
  end
@@ -31,26 +88,26 @@ module IdentityCode
31
88
 
32
89
  def sex
33
90
  return unless valid?
34
- code[0].to_i.odd? ? 'M' : 'F'
91
+ @code[0].to_i.odd? ? 'M' : 'F'
35
92
  end
36
93
 
37
- private
38
-
39
94
  def control_code
40
95
  scales1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1]
41
96
  checknum = scales1.each_with_index.map do |scale, i|
42
- code[i].chr.to_i * scale
97
+ @code[i].chr.to_i * scale
43
98
  end.inject(0, :+) % 11
44
99
  return checknum unless checknum == 10
45
100
 
46
101
  scales2 = [3, 4, 5, 6, 7, 8, 9, 1, 2, 3]
47
102
  checknum = scales2.each_with_index.map do |scale, i|
48
- code[i].chr.to_i * scale
103
+ @code[i].chr.to_i * scale
49
104
  end.inject(0, :+) % 11
50
105
 
51
106
  checknum == 10 ? 0 : checknum
52
107
  end
53
108
 
109
+ private
110
+
54
111
  def age_correction
55
112
  now = Time.now.utc.to_date
56
113
  return 0 if now.month > birth_date.month
@@ -59,7 +116,7 @@ module IdentityCode
59
116
  end
60
117
 
61
118
  def century
62
- case code[0].chr.to_i
119
+ case @code[0].chr.to_i
63
120
  when 1..2 then 1800
64
121
  when 3..4 then 1900
65
122
  when 5..6 then 2000
@@ -1,3 +1,3 @@
1
1
  module IdentityCode
2
- VERSION = "0.1.0"
2
+ VERSION = '0.1.1'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: identitycode
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artem Pakk