Class: FipeApi::Brand

Inherits:
Base
  • Object
show all
Defined in:
lib/fipe_api/resources/brand.rb

Constant Summary

Constants inherited from Base

FipeApi::Base::HEADERS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, name, table, vehicle) ⇒ Brand

Returns a new instance of Brand.



8
9
10
11
12
13
# File 'lib/fipe_api/resources/brand.rb', line 8

def initialize(id, name, table, vehicle)
  self.id = id
  self.name = name
  self.table = table
  self.vehicle = vehicle
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/fipe_api/resources/brand.rb', line 3

def id
  @id
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/fipe_api/resources/brand.rb', line 4

def name
  @name
end

#tableObject

Returns the value of attribute table.



6
7
8
# File 'lib/fipe_api/resources/brand.rb', line 6

def table
  @table
end

#vehicleObject

Returns the value of attribute vehicle.



5
6
7
# File 'lib/fipe_api/resources/brand.rb', line 5

def vehicle
  @vehicle
end

Instance Method Details

#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