Module: Egn::Util

Defined in:
lib/egn/util.rb

Constant Summary collapse

WEIGHTS =
[2, 4, 8, 5, 10, 9, 7, 3, 6]

Class Method Summary collapse

Class Method Details

.determine_date(year, month) ⇒ Object

The EGN can have three different formats depending on the century. It can be determined by examining the month. The rules are as follows:

  • For people born in 1900..1999 the month does not change

  • For people born in 1800..1899 the month is increased by 20 (e.g January is 21)

  • For people born in 2000..2099 the month is increased by 40 (e.g December is 52)



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/egn/util.rb', line 12

def self.determine_date(year, month)
  case month
  when (1..12)
    year = "19#{year}"
  when (21..32)
    month -= 20
    year = "18#{year}"
  when (41..52)
    month -= 40
    year = "20#{year}"
  end

  [year.to_i, month]
end

.egn_checksum(egn) ⇒ Object

More information on the formula: www.grao.bg/esgraon.html#section2



28
29
30
31
32
33
# File 'lib/egn/util.rb', line 28

def self.egn_checksum(egn)
  sum = egn.chars.map(&:to_i).zip(WEIGHTS).map { |n| n.reduce(:*) }.reduce(:+)

  rest = sum % 11
  rest < 10 ? rest : 0
end

.time_rand(from = 0.0, to = Time.now) ⇒ Object

Get a random date



36
37
38
# File 'lib/egn/util.rb', line 36

def self.time_rand(from = 0.0, to = Time.now)
  Time.at(from + rand * (to.to_f - from.to_f))
end