Class: IdentityCode::EE

Inherits:
Object
  • Object
show all
Defined in:
lib/identity_code/ee.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
]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code) ⇒ EE

Returns a new instance of EE.



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

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

Class Method Details

.control_digit(base) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/identity_code/ee.rb', line 50

def self.control_digit(base)
  scales1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1]
  checknum = scales1.each_with_index.map do |scale, i|
    base[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|
    base[i].chr.to_i * scale
  end.inject(0, :+) % 11

  checknum == 10 ? 0 : checknum
end

.generate(opts = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/identity_code/ee.rb', line 23

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)
  year = rand(Date.today.year - 50..Date.today.year - 21) if opts[:safe_age]
  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.upcase.to_sym == :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 += control_digit(result).to_s
end

.valid?(code) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/identity_code/ee.rb', line 46

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

Instance Method Details

#ageObject



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

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

#birth_dateObject



74
75
76
77
78
79
80
81
# File 'lib/identity_code/ee.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

#sexObject



89
90
91
92
# File 'lib/identity_code/ee.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/ee.rb', line 69

def valid?
  @code.length == 11 &&
  @code[10].chr.to_i == self.class.control_digit(@code)
end