lib/vindetta/validator.rb in vindetta-0.16.0 vs lib/vindetta/validator.rb in vindetta-0.17.0

- old
+ new

@@ -1,20 +1,22 @@ module Vindetta class Validator LENGTH = 17 - CHECK_DIGIT_INDEX = 8 MAP = "0123456789X".chars - WEIGHTS = "8765432X098765432".chars + WEIGHTS = "8765432X98765432".chars - def self.run(value) - return false unless value.length == LENGTH + def self.vin(vin) + return false unless vin.length == LENGTH - check_digit(value) == value[CHECK_DIGIT_INDEX] + check_digit(vin) == Decoder.check_digit(vin) end - def self.check_digit(value) - check_digit = value.chars[8] - calculated = value.chars.map.with_index do |c, i| + def self.check_digit(vin) + wmi = Decoder.wmi(vin).chars + vds = Decoder.vds(vin, :check_digit => false).chars + vis = Decoder.vis(vin).chars + + calculated = [wmi, vds, vis].flatten.map.with_index do |c, i| Transliterator::run(c) * MAP.find_index(WEIGHTS[i]) end.sum MAP[calculated % 11] end