Class: VinDecoder::Vehicle

Inherits:
Object
  • Object
show all
Defined in:
lib/vin_decoder/vehicle.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, vin: nil) ⇒ Vehicle

Returns a new instance of Vehicle.



7
8
9
10
# File 'lib/vin_decoder/vehicle.rb', line 7

def initialize(data, vin: nil)
  @raw_data = data || {}
  @vin = vin
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/vin_decoder/vehicle.rb', line 86

def method_missing(method_name, *args, &block)
  return super unless args.empty?

  key = lookup_key(method_name)
  return super unless key

  raw_data[key]
end

Instance Attribute Details

#raw_dataObject (readonly)

Returns the value of attribute raw_data.



5
6
7
# File 'lib/vin_decoder/vehicle.rb', line 5

def raw_data
  @raw_data
end

#vinObject (readonly)

Returns the value of attribute vin.



5
6
7
# File 'lib/vin_decoder/vehicle.rb', line 5

def vin
  @vin
end

Instance Method Details

#[](key) ⇒ Object



60
61
62
# File 'lib/vin_decoder/vehicle.rb', line 60

def [](key)
  raw_data[key.to_s]
end

#body_styleObject



28
29
30
# File 'lib/vin_decoder/vehicle.rb', line 28

def body_style
  raw_data['BodyClass']
end

#drivetrainObject



48
49
50
# File 'lib/vin_decoder/vehicle.rb', line 48

def drivetrain
  raw_data['DriveType']
end

#engineObject



36
37
38
39
40
41
42
# File 'lib/vin_decoder/vehicle.rb', line 36

def engine
  [
    raw_data['EngineManufacturer'],
    raw_data['EngineModel'],
    raw_data['EngineHP']
  ].compact.join(' ').presence
end

#engine_cylindersObject



32
33
34
# File 'lib/vin_decoder/vehicle.rb', line 32

def engine_cylinders
  raw_data['EngineCylinders']
end

#fuel_typeObject



52
53
54
# File 'lib/vin_decoder/vehicle.rb', line 52

def fuel_type
  raw_data['FuelTypePrimary']
end

#makeObject



12
13
14
# File 'lib/vin_decoder/vehicle.rb', line 12

def make
  raw_data['Make']
end

#modelObject



16
17
18
# File 'lib/vin_decoder/vehicle.rb', line 16

def model
  raw_data['Model']
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/vin_decoder/vehicle.rb', line 95

def respond_to_missing?(method_name, include_private = false)
  lookup_key(method_name) || super
end

#submodelObject



24
25
26
# File 'lib/vin_decoder/vehicle.rb', line 24

def submodel
  raw_data['Series']
end

#to_hObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/vin_decoder/vehicle.rb', line 68

def to_h
  {
    vin: vin,
    year: year,
    make: make,
    model: model,
    submodel: submodel,
    body_style: body_style,
    engine: engine,
    transmission: transmission,
    drivetrain: drivetrain,
    fuel_type: fuel_type,
    raw_data: raw_data,
    trim: trim,
    engine_cylinders: engine_cylinders
  }
end

#transmissionObject



44
45
46
# File 'lib/vin_decoder/vehicle.rb', line 44

def transmission
  raw_data['TransmissionDescriptor']
end

#trimObject



56
57
58
# File 'lib/vin_decoder/vehicle.rb', line 56

def trim
  raw_data['Trim']
end

#valid?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/vin_decoder/vehicle.rb', line 64

def valid?
  raw_data['ErrorCode'] == '0'
end

#yearObject



20
21
22
# File 'lib/vin_decoder/vehicle.rb', line 20

def year
  raw_data['ModelYear']
end