Sha256: b58168c596088c217990c63fdb57fd5594de7ecd86ede58b9a08e0d9e7c0918d

Contents?: true

Size: 753 Bytes

Versions: 3

Compression:

Stored size: 753 Bytes

Contents

module Egn
  class Validator
    attr_reader :egn

    # Convinience method
    def self.validate(egn)
      Validator.new(egn).validate
    end

    def initialize(egn)
      @egn = egn

      @year, @month, @day = explode_date(egn)
    end

    # Checks if a given EGN is valid
    def validate
      return false unless @egn.length == 10
      return false unless Date.valid_date?(@year, @month, @day)

      # Calculate the checksum and check if the given one is correct
      checksum = Util.egn_checksum(@egn[0,9])
      checksum == @egn[9].to_i
    end

    private

    def explode_date(egn)
      year, month, day = egn.scan(/.{1,2}/).map(&:to_i)
      year, month = Util.determine_date(year, month)
      [year, month, day]
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
egn-1.3.1 lib/egn/validator.rb
egn-1.3.0 lib/egn/validator.rb
egn-1.2.2 lib/egn/validator.rb