Class: NhtsaVin::Query
- Inherits:
-
Object
- Object
- NhtsaVin::Query
- 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
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#error_code ⇒ Object
readonly
Returns the value of attribute error_code.
-
#raw_response ⇒ Object
readonly
Returns the value of attribute raw_response.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
-
#vin ⇒ Object
readonly
Returns the value of attribute vin.
Instance Method Summary collapse
- #get ⇒ Object
-
#initialize(vin, options = {}) ⇒ Query
constructor
A new instance of Query.
- #parse(json) ⇒ Object
- #valid? ⇒ Boolean
- #vehicle_type(body_class, type) ⇒ Object
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, ={}) @vin = vin.strip.upcase @http_options = [:http] || {} build_url end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
9 10 11 |
# File 'lib/nhtsa_vin/query.rb', line 9 def data @data end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
9 10 11 |
# File 'lib/nhtsa_vin/query.rb', line 9 def error @error end |
#error_code ⇒ Object (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_response ⇒ Object (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 |
#response ⇒ Object (readonly)
Returns the value of attribute response.
9 10 11 |
# File 'lib/nhtsa_vin/query.rb', line 9 def response @response end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
9 10 11 |
# File 'lib/nhtsa_vin/query.rb', line 9 def url @url end |
#vin ⇒ Object (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
#get ⇒ Object
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
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 |