Class: IdentityCode::Isikukood

Inherits:
Object
  • Object
show all
Defined in:
lib/identity_code.rb

Constant Summary collapse

HOSPITALS =
[
  '00', # Kuressaare Haigla (järjekorranumbrid 001 kuni 020)
  '01', # Tartu Ülikooli Naistekliinik, Tartumaa, Tartu (011...019)
  '02', # Ida-Tallinna Keskhaigla, Hiiumaa, Keila, Rapla haigla (021...220)
  '22', # Ida-Viru Keskhaigla (Kohtla-Järve, endine Jõhvi) (221...270)
  '27', # Maarjamõisa Kliinikum (Tartu), Jõgeva Haigla (271...370)
  '37', # Narva Haigla (371...420)
  '42', # Pärnu Haigla (421...470)
  '47', # Pelgulinna Sünnitusmaja (Tallinn), Haapsalu haigla (471...490)
  '49', # Järvamaa Haigla (Paide) (491...520)
  '52', # Rakvere, Tapa haigla (521...570)
  '57', # Valga Haigla (571...600)
  '60', # Viljandi Haigla (601...650)
  '65', # Lõuna-Eesti Haigla (Võru), Pälva Haigla (651...710?)
  '70', # All other hospitals
  '95'  # Foreigners who are born in Estonia
]
NUM_DAYS =
{
  1 => 31,
  2 => 28,
  3 => 31,
  4 => 30,
  5 => 31,
  6 => 30,
  7 => 31,
  8 => 31,
  9 => 30,
  10 => 31,
  11 => 30,
  12 => 31
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code) ⇒ Isikukood

Returns a new instance of Isikukood.



65
66
67
# File 'lib/identity_code.rb', line 65

def initialize(code)
  @code = code.to_s
end

Class Method Details

.generate(opts = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/identity_code.rb', line 39

def self.generate(opts = {})
  first_digit = 0

  sex = opts[:sex] || (rand.round == 0 ? 'M' : 'F')
  year = opts[:year] || rand(Date.today.year - 90..Date.today.year - 1)
  month = opts[:month] || rand(1..12)
  day = opts[:day] || rand(1..NUM_DAYS[month])

  first_digit += 1 if (1800..1899).include?(year)
  first_digit += 3 if (1900..1999).include?(year)
  first_digit += 5 if year >= 2000
  first_digit += 1 if sex == 'F'

  result = first_digit.to_s
  result += "%02d" % year.to_s[2..3].to_i
  result += "%02d" % month
  result += "%02d" % day
  result += HOSPITALS[(rand * HOSPITALS.size - 1).round]
  result += rand(0..9).to_s
  result += new(result).control_code.to_s
end

.valid?(code) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/identity_code.rb', line 61

def self.valid?(code)
  new(code).valid?
end

Instance Method Details

#ageObject



83
84
85
86
87
# File 'lib/identity_code.rb', line 83

def age
  return unless valid?
  now = Time.now.utc.to_date
  now.year - (birth_date.year + age_correction)
end

#birth_dateObject



74
75
76
77
78
79
80
81
# File 'lib/identity_code.rb', line 74

def birth_date
  return unless valid?
  year = century + @code[1..2].to_i
  month = @code[3..4].to_i
  day = @code[5..6].to_i
  return unless Date.valid_date?(year, month, day)
  Date.new(year, month, day)
end

#control_codeObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/identity_code.rb', line 94

def control_code
  scales1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1]
  checknum = scales1.each_with_index.map do |scale, i|
    @code[i].chr.to_i * scale
  end.inject(0, :+) % 11
  return checknum unless checknum == 10

  scales2 = [3, 4, 5, 6, 7, 8, 9, 1, 2, 3]
  checknum = scales2.each_with_index.map do |scale, i|
    @code[i].chr.to_i * scale
  end.inject(0, :+) % 11

  checknum == 10 ? 0 : checknum
end

#sexObject



89
90
91
92
# File 'lib/identity_code.rb', line 89

def sex
  return unless valid?
  @code[0].to_i.odd? ? 'M' : 'F'
end

#valid?Boolean

Returns:

  • (Boolean)


69
70
71
72
# File 'lib/identity_code.rb', line 69

def valid?
  @code.length == 11 &&
  @code[10].chr.to_i == control_code
end