Class: VinValidator::Knowledge

Inherits:
Object
  • Object
show all
Defined in:
lib/vin_validator/knowledge.rb

Class Method Summary collapse

Class Method Details

.validate(vins, hit_nhtsa = false) ⇒ Hash

Validates each vin provided

Parameters:

  • vins (String, Array<String>)
  • hit_nhtsa (Boolean) (defaults to: false)

    if ‘true`, query NHTSA. default: `false`

Returns:

  • (Hash)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/vin_validator/knowledge.rb', line 19

def validate(vins, hit_nhtsa = false)
  check_types = {
    body_type: 'Body type',
    trailer_type: 'Trailer type',
    vehicle_type: 'Vehicle type',
    gvw: 'GVW'
  }

  results = vin_info(vins, hit_nhtsa).map do |vin, simple_check|
    unless simple_check.fetch(:errors).empty?
      next [vin, {
        valid: false,
        wmi: [],
        errors: simple_check.fetch(:errors).map(&:value)
      }]
    end

    vin_split = vin.chars

    year = VinValidator::Year.find_by(letter: vin_split[9].to_s)

    possible_wmis = Array(simple_check[:make])

    errors = []

    if possible_wmis.empty?
      errors = [
        'The manufacturer is not a manufacturer of trucking equipment.',
        'If they are, contact the site admin to add them as a manufacturer.'
      ]
    end

    infos = []
    check_types.each do |type, human_type|
      next if simple_check.dig(type).nil?
      next if simple_check.fetch(type).value.nil?
      next if simple_check.fetch(type).value.empty?

      infos << "NHTSA says #{human_type} is #{simple_check.fetch(type).value}"
    end

    [vin, {
      valid: !year.nil? && !possible_wmis.empty?,
      year: year,
      errors: errors,
      wmi: possible_wmis.uniq(&:value),
      infos: infos
    }]
  end

  results.to_h
end

.vin_info(vins, hit_nhtsa = false) ⇒ Hash

Gathers/builds info for each vin provided

Parameters:

  • vins (String, Array<String>)
  • hit_nhtsa (Boolean) (defaults to: false)

    if ‘true`, query NHTSA. default: `false`

Returns:

  • (Hash)


79
80
81
82
83
# File 'lib/vin_validator/knowledge.rb', line 79

def vin_info(vins, hit_nhtsa = false)
  results = hit_nhtsa ? VinValidator::Api.vin_info(vins) : {}

  Array(vins).map { |vin| [vin, build_results(vin, results.fetch(vin, {}))] }.to_h
end