Class: NhtsaVin::Validation

Inherits:
Object
  • Object
show all
Defined in:
lib/nhtsa_vin/validation.rb

Constant Summary collapse

TRANSLITERATIONS =
{ 'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5, 'F': 6, 'G': 7,
'H': 8, 'J': 1, 'K': 2, 'L': 3, 'M': 4, 'N': 5, 'P': 7,
'R': 9, 'S': 2, 'T': 3, 'U': 4, 'V': 5, 'W': 6, 'X': 7,
'Y': 8, 'Z': 9 }.freeze
WEIGHTS =
[8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vin) ⇒ Validation

Validates a VIN that it fits both the definition, and that the checksum is valid.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/nhtsa_vin/validation.rb', line 18

def initialize(vin)
  @valid = false
  if vin.nil?
    @error = 'Blank VIN provided'
    return
  end
  @vin = vin.strip.upcase
  if !regex
    @error = 'Invalid VIN format'
    return
  elsif checksum != @check
    @error = "VIN checksum digit #{@check} failed "\
             "to calculate (expected #{checksum})"
    return
  else
    @valid = true
  end
end

Instance Attribute Details

#checkObject (readonly)

Returns the value of attribute check.



12
13
14
# File 'lib/nhtsa_vin/validation.rb', line 12

def check
  @check
end

#errorObject (readonly)

Returns the value of attribute error.



12
13
14
# File 'lib/nhtsa_vin/validation.rb', line 12

def error
  @error
end

#plantObject (readonly)

Returns the value of attribute plant.



12
13
14
# File 'lib/nhtsa_vin/validation.rb', line 12

def plant
  @plant
end

#seqObject (readonly)

Returns the value of attribute seq.



12
13
14
# File 'lib/nhtsa_vin/validation.rb', line 12

def seq
  @seq
end

#validObject (readonly)

Returns the value of attribute valid.



12
13
14
# File 'lib/nhtsa_vin/validation.rb', line 12

def valid
  @valid
end

#vdsObject (readonly)

Returns the value of attribute vds.



12
13
14
# File 'lib/nhtsa_vin/validation.rb', line 12

def vds
  @vds
end

#vinObject (readonly)

Returns the value of attribute vin.



12
13
14
# File 'lib/nhtsa_vin/validation.rb', line 12

def vin
  @vin
end

#wmiObject (readonly)

Returns the value of attribute wmi.



12
13
14
# File 'lib/nhtsa_vin/validation.rb', line 12

def wmi
  @wmi
end

Instance Method Details

#valid?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/nhtsa_vin/validation.rb', line 37

def valid?
  @valid
end