Module: IdentityCode

Defined in:
lib/identity_code.rb,
lib/identity_code/ee.rb,
lib/identity_code/lv.rb,
lib/identity_code/version.rb

Defined Under Namespace

Classes: EE, LV

Constant Summary collapse

NUM_DAYS =
{
  1 => 31,
  2 => 28,
  3 => 31,
  4 => 30,
  5 => 31,
  6 => 30,
  7 => 31,
  8 => 31,
  9 => 30,
  10 => 31,
  11 => 30,
  12 => 31
}.freeze
SUPPORTED_COUNTRY_CODES =
%i(ee lv).freeze
VERSION =
'0.2.1'

Class Method Summary collapse

Class Method Details

.age_correction(birth_date) ⇒ Object



44
45
46
47
48
49
# File 'lib/identity_code.rb', line 44

def self.age_correction(birth_date)
  now = Time.now.utc.to_date
  return 0 if now.month > birth_date.month
  return 0 if now.month == birth_date.month && now.day >= birth_date.day
  1
end

.generate(opts = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/identity_code.rb', line 23

def self.generate(opts = {})
  country_code = opts[:country]
  raise 'Country param is missing or invalid (ee or lv)' unless begin
    country_code &&
    SUPPORTED_COUNTRY_CODES.include?(country_code.downcase.to_sym)
  end

  Object.const_get("IdentityCode::#{country_code.upcase}").generate(opts)
end

.valid?(opts = {}) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
# File 'lib/identity_code.rb', line 33

def self.valid?(opts = {})
  country_code = opts.delete(:country)
  raise 'Country param is missing or invalid (ee or lv)' unless begin
    country_code &&
    SUPPORTED_COUNTRY_CODES.include?(country_code.downcase.to_sym)
  end

  code = opts.delete(:code)
  Object.const_get("IdentityCode::#{country_code.upcase}").valid?(code)
end