class PersonPin::Egn

Public Class Methods

calculate_control_digit(egn) click to toggle source
# File lib/person_pin/egn.rb, line 34
def self.calculate_control_digit(egn)
  weights = [2, 4, 8, 5, 10, 9, 7, 3, 6]
  sum = 0
  egn.split('').each_with_index do |digit, index|
    sum += digit.to_i * weights[index]
  end
  control_digit = sum % 11
  control_digit == 10 ? 0 : control_digit
end
daily(year, month, day) click to toggle source
# File lib/person_pin/egn.rb, line 19
def self.daily(year, month, day)
  month += 40 if year >= 2000
  year_s = (year % 100).to_s.rjust(2, '0')
  month_s = month.to_s.rjust(2, '0')
  day_s = day.to_s.rjust(2, '0')
  base = year_s + month_s + day_s
  results = []
  1000.times do |sequence|
    egn = base + sequence.to_s.rjust(3, '0')
    egn[9] = calculate_control_digit(egn).to_s
    results << egn
  end
  results
end
gender(egn) click to toggle source
# File lib/person_pin/egn.rb, line 7
def self.gender(egn)
  gender_digit = egn.split('')[8].to_i % 2
  gender_digit.zero? ? 'male' : 'female'
end
random() click to toggle source
# File lib/person_pin/egn.rb, line 12
def self.random
  year = rand(1900..1920)
  month = rand(1..12)
  day = rand(1..28)
  daily(year, month, day)[rand(0..999)]
end
valid?(egn) click to toggle source
# File lib/person_pin/egn.rb, line 3
def self.valid?(egn)
  (egn.length == 10) && (calculate_control_digit(egn[0..8]) == egn[9].to_i)
end