Class: FipeApi::Table

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

Overview

Describes a Fipe Table whichi is generated each month with updated values for some vehicles

Constant Summary

Constants inherited from Base

Base::HEADERS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, month, year) ⇒ Table

Constructor



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

def initialize(id, month, year)
  self.id = id
  self.month = month
  self.year = year
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#monthObject

Returns the value of attribute month.



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

def month
  @month
end

#yearObject

Returns the value of attribute year.



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

def year
  @year
end

Class Method Details

.all(vehicle) ⇒ Object

Gets all tables for an specific vehicle



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fipe_api/resources/table.rb', line 16

def self.all(vehicle)
  return [] if vehicle.nil?
  tables = []
  response = HTTP.post("http://veiculos.fipe.org.br/api/veiculos/ConsultarTabelaDeReferencia", headers: HEADERS, body: {}.to_json).to_s
  tables_hash = JSON.parse(response)
  tables_hash.each do |table|
    if table["Mes"] != ""
      parts = table["Mes"].strip.split("/")
      tables << Table.new(table["Codigo"], Utils.month_name_to_int(parts[0]), parts[1].to_i)
    end
  end

  tables
end

.find_by_month_and_year(vehicle, month, year) ⇒ Object

Gets a table for a certain month and year and for an specific vehicle



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/fipe_api/resources/table.rb', line 46

def self.find_by_month_and_year(vehicle, month, year)
  return nil if vehicle.nil? || !vehicle.kind_of?(FipeApi::Vehicle)
  result = nil
  tables = self.all(vehicle)

  tables.each do |table|
    if table.month == month && table.year == year
      result = table
      break
    end
  end

  result
end

.latest(vehicle) ⇒ Object

Gets the latest table, for current month and year, for an specific vehicle



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fipe_api/resources/table.rb', line 32

def self.latest(vehicle)
  return nil if vehicle.nil? || !vehicle.kind_of?(FipeApi::Vehicle)
  table = nil
  response = HTTP.post("http://veiculos.fipe.org.br/api/veiculos/ConsultarTabelaDeReferencia", headers: HEADERS, body: {}.to_json).to_s
  table = JSON.parse(response).first
  # first_option = doc.css("#selectTabelaReferencia#{vehicle.name_id} option").first
  if table["Mes"] != ""
    parts = table["Mes"].strip.split("/")
    table = Table.new(table["Codigo"], Utils.month_name_to_int(parts[0]), parts[1].to_i)
  end
  table
end