Class: FipeApi::Vehicle

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

Constant Summary collapse

CAR =
1
MOTORCYCLE =
2
TRUCK =
3

Constants inherited from Base

Base::HEADERS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, name) ⇒ Vehicle

Returns a new instance of Vehicle.



10
11
12
13
# File 'lib/fipe_api/resources/vehicle.rb', line 10

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

Instance Attribute Details

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/fipe_api/resources/vehicle.rb', line 7

def id
  @id
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/fipe_api/resources/vehicle.rb', line 8

def name
  @name
end

Class Method Details

.allObject



15
16
17
18
19
# File 'lib/fipe_api/resources/vehicle.rb', line 15

def self.all
  [Vehicle.new(CAR, "Car"),
   Vehicle.new(MOTORCYCLE, "Motorcycle"),
   Vehicle.new(TRUCK, "Truck")]
end

Instance Method Details

#get_brands(table = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fipe_api/resources/vehicle.rb', line 25

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

  response = HTTP.post("http://veiculos.fipe.org.br/api/veiculos/ConsultarMarcas", headers: HEADERS, params: { codigoTabelaReferencia: table.id, codigoTipoVeiculo: self.id }, body: {}.to_json).to_s
  brands_hash = JSON.parse(response)
  brands_result = []
  brands_hash.each do |brand|
    brands_result << Brand.new(brand["Value"], brand["Label"], table, self)
  end

  brands_result
end

#get_tablesObject



21
22
23
# File 'lib/fipe_api/resources/vehicle.rb', line 21

def get_tables
  Table.all(self)
end

#name_idObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/fipe_api/resources/vehicle.rb', line 40

def name_id
  case id
  when CAR
    return "carro"
  when MOTORCYCLE
    return "moto"
  when TRUCK
    return "caminhao"
  end
end