lib/egn/validator.rb in egn-0.2.0 vs lib/egn/validator.rb in egn-0.4.0

- old
+ new

@@ -1,30 +1,19 @@ module Egn - module Validator + class Validator - def self.egn(egn) + # Checks if a given EGN is valid + def self.validate(egn) return false unless egn.length == 10 - year, month, day = egn.scan(/.{1,2}/) - month = month.to_i - day = day.to_i + # Extract the correct year and month + year, month, day = egn.scan(/.{1,2}/).map(&:to_i) + year, month = Util.determine_date(year, month) - case month - when (1..12) - year = "19#{year}" - when (21..32) - month -= 20 - year = "18#{year}" - when (41..52) - month -= 40 - year = "20#{year}" - end - year = year.to_i - return false unless Date.valid_date? year, month, day - checksum = Generator.egn_checksum egn[0,9] - + # 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