Sha256: 25bf3813ea436360526d76642715e79144db0e07d7148c0713a631a07ec5dfe2

Contents?: true

Size: 670 Bytes

Versions: 4

Compression:

Stored size: 670 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
    end

    # Checks if a given EGN is valid
    def validate
      return false unless egn.length == 10

      # Extract the correct year and month
      year, month, day = egn.scan(/.{1,2}/).map(&:to_i)
      year, month = Util.determine_date(year, month)

      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

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
egn-1.2.1 lib/egn/validator.rb
egn-1.2.0 lib/egn/validator.rb
egn-1.1.0 lib/egn/validator.rb
egn-1.0.0 lib/egn/validator.rb