Class: NhtsaVin::Query

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

Constant Summary collapse

NHTSA_URL =
'https://vpic.nhtsa.dot.gov/api/vehicles/decodevin/'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vin, options = {}) ⇒ Query

Returns a new instance of Query.



11
12
13
14
15
# File 'lib/nhtsa_vin/query.rb', line 11

def initialize(vin, options={})
  @vin = vin.strip.upcase
  @http_options = options[:http] || {}
  build_url
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



9
10
11
# File 'lib/nhtsa_vin/query.rb', line 9

def data
  @data
end

#errorObject (readonly)

Returns the value of attribute error.



9
10
11
# File 'lib/nhtsa_vin/query.rb', line 9

def error
  @error
end

#error_codeObject (readonly)

Returns the value of attribute error_code.



9
10
11
# File 'lib/nhtsa_vin/query.rb', line 9

def error_code
  @error_code
end

#raw_responseObject (readonly)

Returns the value of attribute raw_response.



9
10
11
# File 'lib/nhtsa_vin/query.rb', line 9

def raw_response
  @raw_response
end

#responseObject (readonly)

Returns the value of attribute response.



9
10
11
# File 'lib/nhtsa_vin/query.rb', line 9

def response
  @response
end

#urlObject (readonly)

Returns the value of attribute url.



9
10
11
# File 'lib/nhtsa_vin/query.rb', line 9

def url
  @url
end

#vinObject (readonly)

Returns the value of attribute vin.



9
10
11
# File 'lib/nhtsa_vin/query.rb', line 9

def vin
  @vin
end

Instance Method Details

#getObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/nhtsa_vin/query.rb', line 17

def get
  @raw_response = fetch
  return if @raw_response.nil?
  begin
    parse(JSON.parse(@raw_response))
  rescue JSON::ParserError
    @valid = false
    @error = 'Response is not valid JSON'
  end
end

#parse(json) ⇒ Object



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
# File 'lib/nhtsa_vin/query.rb', line 32

def parse(json)
  @data = json['Results']

  # 0 - Good
  # 1 - VIN decoded clean. Check Digit (9th position) does not calculate properly.
  # 3 - VIN corrected, error in one position (assuming Check Digit is correct).
  # 5 - VIN has errors in few positions.
  # 6 - Incomplete VIN.
  # 8 - No detailed data available currently.
  # 11- Incorrect Model Year, decoded data may not be accurate!
  @error_code = value_id_for('Error Code')&.to_i
  @valid = (@error_code < 4) ? true : false

  if @error_code != 0
    @error = value_for('Error Code')
  end
  return nil unless valid?

  make = value_for('Make').capitalize
  model = value_for('Model')
  trim = value_for('Trim')
  year = value_for('Model Year')
  style = value_for('Body Class')
  type = vehicle_type(style, value_for('Vehicle Type'))
  doors = value_for('Doors')&.to_i

  @response = Struct::NhtsaResponse.new(@vin, make, model, trim, type, year,
                                        style, value_for('Vehicle Type'), doors)
end

#valid?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/nhtsa_vin/query.rb', line 28

def valid?
  @valid
end

#vehicle_type(body_class, type) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/nhtsa_vin/query.rb', line 62

def vehicle_type(body_class, type)
  return case type
  when 'PASSENGER CAR'
    'Car'
  when 'TRUCK'
    if body_class =~ /van/i
      'Van'
    else
      'Truck'
    end
  when 'MULTIPURPOSE PASSENGER VEHICLE (MPV)'
    if body_class =~ /Sport Utility/i
      'SUV'
    else
      'Minivan'
    end
  end
end