class IdentityCode::EE

Constants

HOSPITALS

Public Class Methods

control_digit(base) click to toggle source
# 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 = {}) click to toggle source
# 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
new(code) click to toggle source
# File lib/identity_code/ee.rb, line 65
def initialize(code)
  @code = code.to_s
end
valid?(code) click to toggle source
# File lib/identity_code/ee.rb, line 46
def self.valid?(code)
  new(code).valid?
end

Public Instance Methods

age() click to toggle source
# 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_date() click to toggle source
# 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
sex() click to toggle source
# File lib/identity_code/ee.rb, line 89
def sex
  return unless valid?
  @code[0].to_i.odd? ? :M : :F
end
valid?() click to toggle source
# File lib/identity_code/ee.rb, line 69
def valid?
  @code.length == 11 &&
  @code[10].chr.to_i == self.class.control_digit(@code)
end

Private Instance Methods

century() click to toggle source
# File lib/identity_code/ee.rb, line 96
def century
  case @code[0].chr.to_i
  when 1..2 then 1800
  when 3..4 then 1900
  when 5..6 then 2000
  else
    2100
  end
end