Sha256: dd78dd6079d536732ee710edab3b86a41bcbb6efe3c5c8b3ffeabf204ef77d97

Contents?: true

Size: 1.19 KB

Versions: 2

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

module Eid
  class Finland < Core
    # NOTE: identity DDMMYYCZZZQ
    # DDMMYY - date of birth
    # C - century sign (+, -, U, V, W, X, Y, A, B, C, D, E, F)
    # ZZZ - individual number (002-899 for permanent residents and Finnish citizens, 900-999 for temporary IDs)
    # Q - control character (checksum)

    VALID_CENTURY_SIGNS = %w[+ - U V W X Y A B C D E F].freeze
    VALID_CONTROL_CHARACTERS = '0123456789ABCDEFHJKLMNPRSTUVWXY'

    def valid?
      return false unless identity.size == 11

      birth_date.is_a?(Date) &&
        VALID_CENTURY_SIGNS.include?(identity[6]) &&
        VALID_CONTROL_CHARACTERS.include?(identity[10])
    end

    def female?
      identity[7..9].to_i.even?
    end

    def male?
      identity[7..9].to_i.odd?
    end

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

    private

    def valid_format?
      identity.size == 11
    end

    def century
      case identity[6]
      when '+' then 1800
      when '-', 'U', 'V', 'W', 'X', 'Y' then 1900
      when 'A', 'B', 'C', 'D', 'E', 'F' then 2000
      else 2100
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
e-id-0.3.0 lib/eid/finland.rb
e-id-0.2.0 lib/eid/finland.rb