Class: EikValidator::Validator
- Inherits:
-
Object
- Object
- EikValidator::Validator
- Defined in:
- lib/eik_validator.rb
Instance Method Summary collapse
- #calculate_sum(eik, multipliers) ⇒ Object
- #check_validity(eik) ⇒ Object
- #eik_multipliers(eik) ⇒ Object
-
#initialize(eik) ⇒ Validator
constructor
A new instance of Validator.
- #length_check ⇒ Object
- #numeric_check ⇒ Object
- #validate ⇒ Object
Constructor Details
#initialize(eik) ⇒ Validator
Returns a new instance of Validator.
12 13 14 |
# File 'lib/eik_validator.rb', line 12 def initialize(eik) @eik = eik.to_s end |
Instance Method Details
#calculate_sum(eik, multipliers) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/eik_validator.rb', line 32 def calculate_sum(eik, multipliers) sum = 0 multipliers.each_with_index do |value, index| sum += eik[index].to_i * value end sum end |
#check_validity(eik) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/eik_validator.rb', line 40 def check_validity(eik) control_digit = eik.chars.last.to_i eik_multipliers(eik).each_with_index do |set, index| remainder = calculate_sum(eik, set) % 11 return true if not remainder == 10 and remainder == control_digit return true if index == 1 and remainder == 10 and control_digit == 0 end false end |
#eik_multipliers(eik) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/eik_validator.rb', line 24 def eik_multipliers(eik) if eik.length == 9 [[1, 2, 3, 4, 5, 6, 7, 8], [3, 4, 5, 6, 7, 8, 9, 10]] else [[2, 7, 3, 5], [4, 9, 5, 7]] end end |
#length_check ⇒ Object
20 21 22 |
# File 'lib/eik_validator.rb', line 20 def length_check raise EikLengthError unless @eik.length == 9 or @eik.length == 13 end |
#numeric_check ⇒ Object
16 17 18 |
# File 'lib/eik_validator.rb', line 16 def numeric_check raise EikArgumentError unless ((Float(@eik) != nil) rescue false) end |
#validate ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/eik_validator.rb', line 50 def validate numeric_check length_check if @eik.length == 9 check_validity(@eik) else return check_validity(@eik[-5..-1]) if check_validity(@eik[0..8]) false end end |