Method: FipeApi::Brand#get_models

Defined in:
lib/fipe_api/resources/brand.rb

#get_models(table = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fipe_api/resources/brand.rb', line 15

def get_models(table = nil)
  if table.nil?
    table = Table.latest(self.vehicle)
  end

  response = HTTP.post("http://veiculos.fipe.org.br/api/veiculos/ConsultarModelos",
             headers: HEADERS,
             params: {
                codigoTabelaReferencia: table.id,
                codigoTipoVeiculo: self.vehicle.id,
                codigoMarca: self.id
             },
             body: {}.to_json).to_s
  models_hash = JSON.parse(response)
  models_result = []
  models_hash["Modelos"].each do |model|
    models_result << Model.new(model["Value"], model["Label"], self)
  end

  models_result
end