class EventMachine::Synchrony::DataoneVin::VinExploderAdapter

Constants

BRAKE_TYPES
DATA_PATHS
DRIVELINE_TYPES
FUEL_TYPES

Public Class Methods

new(options) click to toggle source
# File lib/em-synchrony/dataone-vin/vin_exploder_adapter.rb, line 70
def initialize(options)
  client_id, authorization_code = options.values_at(:client_id, :authorization_code)

  unless client_id && authorization_code
    raise Error.new "DataoneVin::VinExploderAdapter requires both a client_id and an authorization_code"
  end

  DataoneVin.configure client_id, authorization_code
end

Public Instance Methods

errors(response) click to toggle source
# File lib/em-synchrony/dataone-vin/vin_exploder_adapter.rb, line 96
def errors(response)
  query_error = response['query_responses']['Request-Sample']['query_error']['error_message']
  errors = response['decoder_messages']['decoder_errors']
  errors << query_error unless query_error.empty?
  errors
end
explode(vin) click to toggle source
# File lib/em-synchrony/dataone-vin/vin_exploder_adapter.rb, line 80
def explode(vin)
  format_response DataoneVin.get(vin), vin
end
explosion(data) click to toggle source
# File lib/em-synchrony/dataone-vin/vin_exploder_adapter.rb, line 103
def explosion(data)
  normalize Hash[DATA_PATHS.map do |(key, path)|
    [key, lookup_data(data, path)]
  end]
end
format_response(response, vin) click to toggle source
# File lib/em-synchrony/dataone-vin/vin_exploder_adapter.rb, line 84
def format_response(response, vin)
  errors = errors(response)
  return {:errors => errors} unless errors.empty?

  data = response['query_responses']['Request-Sample']
  explosion(data).merge \
    :errors        => [],
    :vin           => vin,
    :vin_key       => vin_key(vin),
    :vendor_result => data
end
lookup_data(data, path) click to toggle source
# File lib/em-synchrony/dataone-vin/vin_exploder_adapter.rb, line 109
def lookup_data(data, path)
  path.reduce(data) do |d, (k, v)|
    if v
      d.find{|h| h[k] == v}
    else
      d[k]
    end
  end
rescue
  nil
end
normalize(data) click to toggle source
# File lib/em-synchrony/dataone-vin/vin_exploder_adapter.rb, line 121
def normalize(data)
  data['driveline'] = normalize_driveline data['driveline']
  data['fuel_type'] = normalize_fuel_type data['fuel_type']
  data['vehicle_type'] = data['vehicle_type'].upcase
  data['has_turbo'] = !!(data['engine_type'] =~ /turbo/i)
  data['transmission-short'] = "#{data.delete('gears')}#{data.delete('transmission-type')}"
  data['anti-brake_system'] = normalize_brakes data.delete('abs_two_wheel'), data.delete('abs_four_wheel')
  data['gvwr_class'] = normalize_gvwr_class data['gvwr_class']

  data
end
normalize_brakes(abs_two_wheel, abs_four_wheel) click to toggle source
# File lib/em-synchrony/dataone-vin/vin_exploder_adapter.rb, line 141
def normalize_brakes(abs_two_wheel, abs_four_wheel)
  BRAKE_TYPES.fetch([abs_two_wheel, abs_four_wheel], 'No Data')
end
normalize_driveline(driveline) click to toggle source
# File lib/em-synchrony/dataone-vin/vin_exploder_adapter.rb, line 133
def normalize_driveline(driveline)
  DRIVELINE_TYPES.fetch(driveline, driveline)
end
normalize_fuel_type(fuel_type) click to toggle source
# File lib/em-synchrony/dataone-vin/vin_exploder_adapter.rb, line 137
def normalize_fuel_type(fuel_type)
  FUEL_TYPES.fetch(fuel_type, 'No data')
end
normalize_gvwr_class(weight_rating) click to toggle source
# File lib/em-synchrony/dataone-vin/vin_exploder_adapter.rb, line 145
def normalize_gvwr_class(weight_rating)
  case weight_rating.to_i
  when 0..6_000                then '1'
  when 6_001..10_000           then '2'
  when 10_001..14_000          then '3'
  when 14_001..16_000          then '4'
  when 16_001..19_500          then '5'
  when 19_501..26_000          then '6'
  when 26_001..33_000          then '7'
  when 33_001..Float::INFINITY then '8'
  else                              '1'
  end
end
vin_key(vin) click to toggle source
# File lib/em-synchrony/dataone-vin/vin_exploder_adapter.rb, line 159
def vin_key(vin)
  "#{vin[0,8]}#{vin[9,2]}"
end