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 27 28 |
# File 'lib/nhtsa_vin/query.rb', line 17 def get @raw_response = fetch begin return if @raw_response.nil? || (json_response = JSON.parse(@raw_response)).nil? parse(json_response) rescue JSON::ParserError @valid = false @error = 'Response is not valid JSON' rescue StandardError => ex raise "#{ex.}: #{@raw_response.inspect}" end end |
#parse(json) ⇒ Object
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 |
# File 'lib/nhtsa_vin/query.rb', line 34 def parse(json) if json['Message']&.match(/execution error/i) @valid = false @error = json.dig('Results', 0, 'Message') return end @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
30 31 32 |
# File 'lib/nhtsa_vin/query.rb', line 30 def valid? @valid end |
#vehicle_type(body_class, type) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/nhtsa_vin/query.rb', line 69 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 |