Sha256: ccdb635360b50fc9943b6cee866d0b5aa8d1a2a5b200d8a968f20211dca78228

Contents?: true

Size: 1.13 KB

Versions: 8

Compression:

Stored size: 1.13 KB

Contents

# Contains some utility methods that are used by the other classes
module Egn
  module Util
    WEIGHTS = [2,4,8,5,10,9,7,3,6]

    # 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 +20 (e.g January is 21)
    # * For people born in 2000..2099 the month is +40 (e.g December is 52)
    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

    # The checksum is calculated from the first 9 digits.
    def self.egn_checksum(egn)
      sum = egn.split('').map(&:to_i).zip(WEIGHTS).map { |n| n.reduce(:*) }.reduce(:+)

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

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

  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
egn-1.3.1 lib/egn/util.rb
egn-1.3.0 lib/egn/util.rb
egn-1.2.2 lib/egn/util.rb
egn-1.2.1 lib/egn/util.rb
egn-1.2.0 lib/egn/util.rb
egn-1.1.0 lib/egn/util.rb
egn-1.0.0 lib/egn/util.rb
egn-0.4.0 lib/egn/util.rb