Sha256: 50bcca75372a94f680e372c25b3e145f1faee0bdecc6610dea3ad1ab7c99c0a3

Contents?: true

Size: 597 Bytes

Versions: 1

Compression:

Stored size: 597 Bytes

Contents

module Vindetta
  class Validator
    LENGTH = 17
    MAP = "0123456789X".chars
    WEIGHTS = "8765432X98765432".chars

    def self.vin(vin)
      return false unless vin.length == LENGTH

      check_digit(vin) == Decoder.check_digit(vin)
    end

    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vindetta-0.17.0 lib/vindetta/validator.rb