Module: IdentityCode

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

Defined Under Namespace

Classes: EE, LV, PL

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 pl).freeze
VERSION =
'0.2.5'

Class Method Summary collapse

Class Method Details

.age_correction(birth_date) ⇒ Object



56
57
58
59
60
61
# File 'lib/identity_code.rb', line 56

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



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

def self.generate(opts = {})
  country_code = opts[:country]
  raise 'Country param is missing or invalid (ee | lv | pl)' 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)


45
46
47
48
49
50
51
52
53
54
# File 'lib/identity_code.rb', line 45

def self.valid?(opts = {})
  country_code = opts.delete(:country)
  raise 'Country param is missing or invalid (ee | lv | pl)' 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

.validate(opts = {}) ⇒ Object



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

def self.validate(opts = {})
  country_code = opts.delete(:country)
  raise 'Country param is missing or invalid (ee | lv | pl)' 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}").new(code)
end