Class: FipeApi::Year

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

Constant Summary

Constants inherited from Base

Base::HEADERS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, name, model) ⇒ Year

Returns a new instance of Year.



9
10
11
12
13
14
15
# File 'lib/fipe_api/resources/year.rb', line 9

def initialize(id, name, model)
  self.id = id.split("-")[0]
  self.year = self.id == '32000' ? Date.current.year : self.id
  self.fuel = id.split("-")[1]
  self.name = name
  self.model = model
end

Instance Attribute Details

#fuelObject

Returns the value of attribute fuel.



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

def fuel
  @fuel
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#modelObject

Returns the value of attribute model.



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

def model
  @model
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#yearObject

Returns the value of attribute year.



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

def year
  @year
end

Instance Method Details

#get_result(table = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fipe_api/resources/year.rb', line 17

def get_result(table = nil)
  if table.nil?
    table = Table.latest(self.model.brand.vehicle)
  end

  response = HTTP.post("http://veiculos.fipe.org.br/api/veiculos/ConsultarValorComTodosParametros",
             headers: HEADERS,
             params: {
                codigoTabelaReferencia: table.id,
                codigoTipoVeiculo: self.model.brand.vehicle.id,
                codigoMarca: self.model.brand.id,
                codigoModelo: self.model.id,
                anoModelo: self.id,
                codigoTipoCombustivel: self.fuel,
                tipoVeiculo: self.model.brand.vehicle.name_id,
                tipoConsulta: "tradicional"
             },
             body: {}.to_json).to_s
  result_json = JSON.parse(response)
  fipe_result = nil
  if !result_json.nil?
    fipe_result = FipeResult.new(result_json["CodigoFipe"].strip,
                                 self,
                                 result_json["Combustivel"],
                                 result_json["Autenticacao"],
                                 result_json["Valor"],
                                 result_json["DataConsulta"])
  end

  fipe_result
end