Sha256: 4681b916b3de39866d547d734ffc14b0aa2f3a37002de9bbf2d5dc2fa45f831e

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module Eid
  class Latvia < Core
    # NOTE: identity DDMMYY-XNNNZ
    # DDMYY - date of birth
    # X - century
    # NNN - birth serial number
    # Z - checksum digit
    #
    # 32XXXX-XXXXC (updated format since 01.07.2017)
    # 32 - updated format indicator
    # XXXX-XXXX - random numbers
    # C - checksum

    def legacy?
      !identity.start_with?('32')
    end

    def valid?
      legacy? ? (valid_format? && birth_date.is_a?(Date)) : valid_format?
    end

    def female?
      raise 'Not supported'
    end

    def male?
      raise 'Not supported'
    end

    def gender
      raise 'Not supported'
    end

    def birth_date
      Date.new(century + identity[4..5].to_i, identity[2..3].to_i, identity[0..1].to_i) if legacy?
    rescue StandardError
      nil
    end

    private

    def valid_format?
      identity.match?(/\d{6}-\d{5}/) && identity.size == 12
    end

    def century
      case identity[7].to_i
      when 1 then 1900
      when 2 then 2000
      else 2100
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
e-id-0.3.0 lib/eid/latvia.rb