Class: EventMachine::Synchrony::DataoneVin::VinExploderAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/em-synchrony/dataone-vin/vin_exploder_adapter.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

DRIVELINE_TYPES =
{
  '4x4' => '4WD',
  '4X4' => '4WD',
  '4x2' => 'RWD',
  '4X2' => 'RWD',
}
FUEL_TYPES =
{
  'B' => 'DIESEL',
  'D' => 'DIESEL',
  'F' => 'FFV',
  'G' => 'GAS',
  'I' => 'HYBRID',
  'L' => 'ELECTRIC',
  'N' => 'NATURALGAS',
  'P' => 'PETROLEUM',
  'Y' => 'HYBRID',
}
BRAKE_TYPES =
{
  ['Not Available', 'Not Available'] => 'Non-ABS',
  ['Not Available', 'Standard']      => '4-Wheel ABS',
  ['Not Available', 'Optional']      => 'Non-ABS | 4-Wheel ABS',
  ['Standard', 'Not Available'] => '2-Wheel ABS',
  ['Standard', 'Standard']      => '2-Wheel ABS | 4-Wheel ABS',
  ['Standard', 'Optional']      => '2-Wheel ABS | 4-Wheel ABS',
  ['Optional', 'Not Available'] => 'Non-ABS | 2-Wheel ABS',
  ['Optional', 'Standard']      => '2-Wheel ABS | 4-Wheel ABS',
  ['Optional', 'Optional']      => 'Non-ABS | 2-Wheel ABS | 4-Wheel ABS',
}
DATA_PATHS =
{
  'year'                  => ['common_data', 'basic_data', 'year'],
  'make'                  => ['common_data', 'basic_data', 'make'],
  'model'                 => ['common_data', 'basic_data', 'model'],
  'trim_level'            => ['common_data', 'basic_data', 'trim'],
  'engine_type'           => ['styles', 0, 'engines', 0, 'name'],
  'engine_displacement'   => ['styles', 0, 'engines', 0, 'displacement'],
  'engine_shape'          => ['styles', 0, 'engines', 0, 'block_type'],
  'body_style'            => ['common_data', 'basic_data', 'body_type'],
  'manufactured_in'       => ['common_data', 'basic_data', 'country_of_manufacture'],
  'driveline'             => ['common_data', 'basic_data', 'drive_type'],
  'fuel_type'             => ['styles', 0, 'engines', 0, 'fuel_type'],
  'transmission-long'     => ['styles', 0,'transmissions', 0, 'name'],
  'gears'                 => ['styles', 0,'transmissions', 0, 'gears'],
  'transmission-type'     => ['styles', 0,'transmissions', 0, 'type'],
  'tank'                  => ['common_data', 'specifications', ['category', 'Fuel Tanks'], 'specifications', ['name', 'Fuel Tank 1 Capacity (Gallons)'], 'value'],
  'abs_two_wheel'         => ['common_data', 'safety_equipment', 'abs_two_wheel'],
  'abs_four_wheel'        => ['common_data', 'safety_equipment', 'abs_four_wheel'],
  'gvwr_class'            => ['common_data', 'specifications', ['category', 'Measurements of Weight'], 'specifications', ['name', 'Gross Vehicle Weight Rating'], 'value'],
  'vehicle_type'          => ['common_data', 'basic_data', 'vehicle_type'],
  'number_of_cylinders'   => ['styles', 0, 'engines', 0, 'cylinders'],
  'number_of_doors'       => ['common_data', 'basic_data', 'doors'],
  'standard_seating'      => ['common_data', 'specifications', ['category', 'Seating'], 'specifications', ['name', 'Standard Seating'], 'value'],
  'optional_seating'      => ['common_data', 'specifications', ['category', 'Seating'], 'specifications', ['name', 'Max Seating'], 'value'],
  'length'                => ['common_data', 'specifications', ['category', 'Measurements of Size and Shape'], 'specifications', ['name', 'Length'], 'value'],
  'width'                 => ['common_data', 'specifications', ['category', 'Measurements of Size and Shape'], 'specifications', ['name', 'Width'], 'value'],
  'height'                => ['common_data', 'specifications', ['category', 'Measurements of Size and Shape'], 'specifications', ['name', 'Height'], 'value'],
  'production_seq_number' => ['common_data', 'This will always be nil'],
}

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ VinExploderAdapter

Returns a new instance of VinExploderAdapter.



70
71
72
73
74
75
76
77
78
# 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

Instance Method Details

#errors(response) ⇒ Object



96
97
98
99
100
101
# 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) ⇒ Object



80
81
82
# File 'lib/em-synchrony/dataone-vin/vin_exploder_adapter.rb', line 80

def explode(vin)
  format_response DataoneVin.get(vin), vin
end

#explosion(data) ⇒ Object



103
104
105
106
107
# 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) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# 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) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
# 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) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
# 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) ⇒ Object



141
142
143
# 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) ⇒ Object



133
134
135
# 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) ⇒ Object



137
138
139
# 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) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
# 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) ⇒ Object



159
160
161
# File 'lib/em-synchrony/dataone-vin/vin_exploder_adapter.rb', line 159

def vin_key(vin)
  "#{vin[0,8]}#{vin[9,2]}"
end