diff --git a/Gemfile b/Gemfile index ff6b23a..a28724a 100644 --- a/Gemfile +++ b/Gemfile @@ -5,7 +5,7 @@ gem 'quickbooks-ruby-base' gem 'oauth-plugin' gem 'oauth' gem 'roxml' -gem 'edmunds_vin' +gem 'nhtsa_vin' gem 'will_paginate' gem 'rails-jquery-autocomplete' gem 'jquery-rails', '~> 3.1.4' diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index 36f6f41..ae595c5 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -33,7 +33,7 @@ class CustomersController < ApplicationController default_search_scope :names - autocomplete :customer, :name, :full => false, :extra_data => [:id] + autocomplete :customer, :name, :full => true, :extra_data => [:id] def filter_vehicles_by_customer @filtered_vehicles = Vehicle.all.where(customer_id: params[:selected_customer]) diff --git a/app/models/vehicle.rb b/app/models/vehicle.rb index 0e5d771..17d2e15 100644 --- a/app/models/vehicle.rb +++ b/app/models/vehicle.rb @@ -29,7 +29,8 @@ class Vehicle < ActiveRecord::Base if year.nil? or make.nil? or model.nil? return "#{vin}" else - return "#{year} #{make} #{model}" + split_vin = vin.scan(/.{1,9}/) + return "#{year} #{make} #{model} - #{split_vin[1]}" end end @@ -83,15 +84,15 @@ class Vehicle < ActiveRecord::Base where("vin LIKE ?", "%#{search}%") end - private +private # init method to pull JSON details from Edmunds def get_details if self.vin? begin query = NhtsaVin.get(self.vin) - raise error if not @details.valid? - @details = query.response = NhtsaVin.get(self.vin) + raise RuntimeError, query.error unless query.valid? + @details = query.response rescue Exception => e errors.add(:vin, e.message) end @@ -103,9 +104,9 @@ class Vehicle < ActiveRecord::Base get_details if @details begin - self.year = @details.year - self.make = @details.make - self.model = @details.model + self.year = @details.year unless @details.year.nil? + self.make = @details.make unless @details.make.nil? + self.model = @details.model unless @details.model.nil? rescue Exception => e errors.add(:vin, e.message) end @@ -113,15 +114,4 @@ class Vehicle < ActiveRecord::Base self.name = to_s end - # makes a squishvin - # https://api.edmunds.com/api/vehicle/v2/squishvins/#{vin}/?fmt=json&api_key=#{ENV['edmunds_key']} - def vin_squish - if not self.vin? or self.vin.size < 11 - # this is to go ahead and query the API, letting them handle the error. :P - return '1000000000A' - end - v = self.vin[0,11] - return v.slice(0,8) + v.slice(9,11) - end - end diff --git a/app/views/vehicles/_details.html.erb b/app/views/vehicles/_details.html.erb index 058eb0b..50ae99a 100644 --- a/app/views/vehicles/_details.html.erb +++ b/app/views/vehicles/_details.html.erb @@ -21,16 +21,6 @@