Class: VinValidator::Api

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

Class Method Summary collapse

Class Method Details

.vin_info(vins) ⇒ Hash

Fetches info about vins. GET request if single VIN is passed in. More details can be found at https://vpic.nhtsa.dot.gov/api/Home



13
14
15
16
17
18
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
# File 'lib/vin_validator/api.rb', line 13

def vin_info(vins)
  vins_to_check = [vins]
  vins_to_check.flatten!
  vins_to_check = vins_to_check.flat_map do |vin|
    next if vin.nil?

    vin.split('/').map { |v| v.gsub(/[^0-9a-z]/i, '') }
  end
  vins_to_check.compact!
  vins_to_check.uniq!

  api_results =
    if vins_to_check.size == 1
      # 1E9AA5325L1588827
      [single_vin_info(vins_to_check.first)]
    else
      # 13N153201K1533942
      # 1E9AA5325L1588827
      # 1RND48A27GR039983
      # 1RND53A33KR049282
      multiple_vin_infos(vins_to_check)
    end

  results = {}
  api_results.each do |result|
    result[:Results].each do |details|
      results[details[:VIN]] = details
    end
  end

  # '1R1F24822YK501231' => {
  #   ...
  #   "TrailerBodyType": "",
  #   ...
  #   "VIN": "1R1F24822YK501231",
  #   ...
  #   }

  return results
end