Method: TecDoc::Vehicle.find

Defined in:
lib/tec_doc/vehicle.rb

.find(options = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/tec_doc/vehicle.rb', line 68

def self.find(options = {})
  id = options.delete(:id)
  options = {
    :car_ids => { :array => { :ids => [id] } },
    :countries_car_selection => TecDoc.client.country,
    :country_user_setting => TecDoc.client.country,
    :country => TecDoc.client.country,
    :lang => TecDoc.client.country,
    :axles => false,
    :cabs => false,
    :country_group_flag => false,
    :motor_codes => true,
    :vehicle_details_2 => false,
    :vehicle_terms => true
  }.merge(options)
  response = TecDoc.client.request(:get_vehicle_by_ids_2, options)
  if attrs = response.first
    details   = attrs[:vehicle_details]  || {}
    details2  = attrs[:vehicle_details2] || {}
    terms     = attrs[:vehicle_terms]    || {}
    vehicle  = new
    vehicle.id                        = attrs[:car_id].to_i
    vehicle.name                      = terms[:car_type].to_s
    vehicle.cylinder_capacity         = details[:ccm_tech].to_i
    vehicle.fuel_type                 = details2[:fuel_type].to_s
    vehicle.fuel_type_process         = details2[:fuel_type_process].to_s
    vehicle.power_hp_from             = details[:power_hp_from].to_i
    vehicle.power_hp_to               = details[:power_hp_to].to_i
    vehicle.power_kw_from             = details[:power_kw_from].to_i
    vehicle.power_kw_to               = details[:power_kw_to].to_i
    vehicle.date_of_construction_from = DateParser.new(details[:year_of_constr_from]).to_date
    vehicle.date_of_construction_to   = DateParser.new(details[:year_of_constr_to]).to_date
    vehicle.manu_id                   = details[:manu_id].to_i
    vehicle.mod_id                    = details[:mod_id].to_i
    vehicle.motor_codes = attrs[:motor_codes].map { |mc| mc[:motor_code] }
    vehicle
  else
    nil
  end
end