fipextractor 0.0.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +50 -50
- data/.rspec +3 -0
- data/.rspec_status +31 -0
- data/.travis.yml +7 -0
- data/Gemfile +7 -3
- data/LICENSE +21 -0
- data/README.md +88 -118
- data/Rakefile +6 -10
- data/fipextractor.gemspec +29 -21
- data/lib/fipextractor.rb +9 -40
- data/lib/fipextractor/brand.rb +24 -0
- data/lib/fipextractor/brand_response.rb +7 -0
- data/lib/fipextractor/converter/vehicle_type.rb +16 -0
- data/lib/fipextractor/model.rb +26 -0
- data/lib/fipextractor/model_response.rb +15 -0
- data/lib/fipextractor/model_through_year.rb +30 -0
- data/lib/fipextractor/model_through_year_response.rb +7 -0
- data/lib/fipextractor/model_year.rb +28 -0
- data/lib/fipextractor/model_year_response.rb +7 -0
- data/lib/fipextractor/reference_table.rb +10 -0
- data/lib/fipextractor/reference_table_response.rb +7 -0
- data/lib/fipextractor/request.rb +53 -0
- data/lib/fipextractor/resource.rb +10 -0
- data/lib/fipextractor/response.rb +16 -0
- data/lib/fipextractor/vehicle.rb +39 -0
- data/lib/fipextractor/vehicle_response.rb +7 -0
- data/lib/fipextractor/version.rb +3 -0
- metadata +67 -33
- data/lib/fipextractor/api_parameter/parameter_converter.rb +0 -39
- data/lib/fipextractor/api_parameter/parameter_validator.rb +0 -46
- data/lib/fipextractor/api_response/brand_response_converter.rb +0 -10
- data/lib/fipextractor/api_response/model_response_converter.rb +0 -45
- data/lib/fipextractor/api_response/model_through_year_response_converter.rb +0 -10
- data/lib/fipextractor/api_response/response_converter.rb +0 -60
- data/lib/fipextractor/api_response/response_converter_builder.rb +0 -22
- data/lib/fipextractor/fipe_api/api_factory.rb +0 -12
- data/lib/fipextractor/fipe_api/api_requester.rb +0 -49
- data/lib/fipextractor/fipe_api/api_response.rb +0 -23
- data/test/parameter_converter_spec.rb +0 -23
- data/test/parameter_validator_spec.rb +0 -67
- data/test/response_converter_spec.rb +0 -35
@@ -1,39 +0,0 @@
|
|
1
|
-
module FipeApi
|
2
|
-
class ParameterConverter
|
3
|
-
|
4
|
-
CONVERSION_PARAMETER = {
|
5
|
-
reference_table_id: :codigoTabelaReferencia,
|
6
|
-
vehicle_type_id: :codigoTipoVeiculo,
|
7
|
-
brand_id: :codigoMarca,
|
8
|
-
model_id: :codigoModelo,
|
9
|
-
year: :ano,
|
10
|
-
model_year: :anoModelo,
|
11
|
-
fuel_type_id: :codigoTipoCombustivel,
|
12
|
-
search: :tipoConsulta,
|
13
|
-
vehicle_type: :codigoTipoVeiculo
|
14
|
-
}
|
15
|
-
|
16
|
-
VEHICLE_TYPE = {
|
17
|
-
car: 1,
|
18
|
-
motorcycle: 2,
|
19
|
-
truck: 3
|
20
|
-
}
|
21
|
-
|
22
|
-
def self.to_fipe_pattern(application_parameters)
|
23
|
-
converted_parameters = {}
|
24
|
-
application_parameters.each do |parameter, value|
|
25
|
-
converted_parameters[CONVERSION_PARAMETER[parameter]] = value
|
26
|
-
end
|
27
|
-
converted_parameters
|
28
|
-
end
|
29
|
-
|
30
|
-
def self.vehicle_type_to_id(vehicle)
|
31
|
-
VEHICLE_TYPE[vehicle]
|
32
|
-
end
|
33
|
-
|
34
|
-
def self.add_search_type
|
35
|
-
'tradicional'
|
36
|
-
end
|
37
|
-
|
38
|
-
end
|
39
|
-
end
|
@@ -1,46 +0,0 @@
|
|
1
|
-
module FipeApi
|
2
|
-
class ParameterValidator
|
3
|
-
|
4
|
-
attr_reader :status, :message
|
5
|
-
|
6
|
-
PARAMETERS = %w{reference_table_id vehicle_type brand_id model_id year model_year fuel_type_id vehicle_type}
|
7
|
-
|
8
|
-
METHOD_PARAMS = {
|
9
|
-
reference_table: {},
|
10
|
-
brand: PARAMETERS.select { |param| param.match(/reference_table_id|vehicle_type/) },
|
11
|
-
model: PARAMETERS.select { |param| param.match(/reference_table_id|vehicle_type|brand_id/) },
|
12
|
-
model_year: PARAMETERS.select { |param| param.match(/reference_table_id|vehicle_type|brand_id|model_id/) },
|
13
|
-
model_through_year: PARAMETERS.select { |param| param.match(/reference_table_id|vehicle_type|brand_id|model_id|year|model_year|fuel_type_id/) },
|
14
|
-
full: PARAMETERS
|
15
|
-
}
|
16
|
-
|
17
|
-
def initialize(method, params)
|
18
|
-
@method = method
|
19
|
-
@params = params
|
20
|
-
@message = []
|
21
|
-
validate
|
22
|
-
end
|
23
|
-
|
24
|
-
def is_ok?
|
25
|
-
@status
|
26
|
-
end
|
27
|
-
|
28
|
-
private
|
29
|
-
|
30
|
-
def validate
|
31
|
-
METHOD_PARAMS[@method].each do |param|
|
32
|
-
@message.push("#{param} does not exists, and is mandatory for #{@method} method!") unless param_exists?(param)
|
33
|
-
end
|
34
|
-
generate_status
|
35
|
-
end
|
36
|
-
|
37
|
-
def param_exists?(param)
|
38
|
-
@params.has_key?(param.to_sym)
|
39
|
-
end
|
40
|
-
|
41
|
-
def generate_status
|
42
|
-
@status = @message.empty?
|
43
|
-
end
|
44
|
-
|
45
|
-
end
|
46
|
-
end
|
@@ -1,45 +0,0 @@
|
|
1
|
-
module FipeApi
|
2
|
-
class ModelResponseConverter < ResponseConverter
|
3
|
-
|
4
|
-
CONVERSION_TEMPLATE = {
|
5
|
-
Label: :model,
|
6
|
-
Value: :model_id
|
7
|
-
}
|
8
|
-
|
9
|
-
YEAR_CONVERSION_TEMPLATE = {
|
10
|
-
Label: :year,
|
11
|
-
Value: :year_fuel
|
12
|
-
}
|
13
|
-
|
14
|
-
def convert
|
15
|
-
converted_hash_collection = {}
|
16
|
-
converted_hash_collection[:models] = convert_by_object_type(@fipe_response['Modelos'])
|
17
|
-
converted_hash_collection[:year] = convert_fuel_collection(@fipe_response['Anos'])
|
18
|
-
converted_hash_collection
|
19
|
-
end
|
20
|
-
|
21
|
-
private
|
22
|
-
|
23
|
-
def convert_fuel_collection(fipe_fuel_collection)
|
24
|
-
converted_collection = []
|
25
|
-
fipe_fuel_collection.each do |item|
|
26
|
-
converted_collection.push(convert_by_fuel_pattern(item))
|
27
|
-
end
|
28
|
-
converted_collection
|
29
|
-
end
|
30
|
-
|
31
|
-
def convert_by_fuel_pattern(fipe_fuel_response)
|
32
|
-
converted_item = {}
|
33
|
-
fipe_fuel_response.each do |parameter, value|
|
34
|
-
converted_item[:fuel_type_id] = extract_fuel_id(value) if (parameter.to_sym == :Value)
|
35
|
-
converted_item[YEAR_CONVERSION_TEMPLATE[parameter.to_sym]] = value
|
36
|
-
end
|
37
|
-
converted_item
|
38
|
-
end
|
39
|
-
|
40
|
-
def extract_fuel_id(fuel_item)
|
41
|
-
fuel_item.split('-').last
|
42
|
-
end
|
43
|
-
|
44
|
-
end
|
45
|
-
end
|
@@ -1,60 +0,0 @@
|
|
1
|
-
module FipeApi
|
2
|
-
class ResponseConverter
|
3
|
-
|
4
|
-
CONVERSION_TEMPLATE = {
|
5
|
-
Codigo: :reference_table_id,
|
6
|
-
Mes: :reference_table_month,
|
7
|
-
Label: :label,
|
8
|
-
Value: :value_id,
|
9
|
-
Valor: :cost ,
|
10
|
-
Marca: :brand,
|
11
|
-
Modelo: :model,
|
12
|
-
AnoModelo: :model_year,
|
13
|
-
Combustivel: :fuel,
|
14
|
-
CodigoFipe: :fipe_id,
|
15
|
-
MesReferencia: :reference_month,
|
16
|
-
Autenticacao: :authentication_hash,
|
17
|
-
TipoVeiculo: :vehicle_type,
|
18
|
-
SiglaCombustivel: :fuel_symbol,
|
19
|
-
DataConsulta: :query_date,
|
20
|
-
codigo: :request_fipe_code,
|
21
|
-
erro: :fipe_error_message
|
22
|
-
}
|
23
|
-
|
24
|
-
def initialize(method, fipe_response)
|
25
|
-
@method = method
|
26
|
-
@fipe_response = fipe_response
|
27
|
-
end
|
28
|
-
|
29
|
-
def convert
|
30
|
-
convert_by_object_type(@fipe_response)
|
31
|
-
end
|
32
|
-
|
33
|
-
private
|
34
|
-
|
35
|
-
def convert_by_object_type(response_object)
|
36
|
-
if response_object.is_a?(Array)
|
37
|
-
collection_of_items(response_object)
|
38
|
-
else
|
39
|
-
convert_item(response_object)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
def convert_item(item)
|
44
|
-
converted_item = {}
|
45
|
-
item.each do |parameter, value|
|
46
|
-
converted_item[self.class::CONVERSION_TEMPLATE[parameter.to_sym]] = value
|
47
|
-
end
|
48
|
-
converted_item
|
49
|
-
end
|
50
|
-
|
51
|
-
def collection_of_items(response_collection)
|
52
|
-
converted_collection = []
|
53
|
-
response_collection.each do |item|
|
54
|
-
converted_collection.push(convert_item(item))
|
55
|
-
end
|
56
|
-
converted_collection
|
57
|
-
end
|
58
|
-
|
59
|
-
end
|
60
|
-
end
|
@@ -1,22 +0,0 @@
|
|
1
|
-
module FipeApi
|
2
|
-
require_relative 'response_converter'
|
3
|
-
require_relative 'brand_response_converter'
|
4
|
-
require_relative 'model_response_converter'
|
5
|
-
require_relative 'model_through_year_response_converter'
|
6
|
-
class ResponseConverterBuilder
|
7
|
-
|
8
|
-
def self.build(method, fipe_response)
|
9
|
-
case(method)
|
10
|
-
when :brand
|
11
|
-
FipeApi::BrandResponseConverter.new(method, fipe_response)
|
12
|
-
when :model
|
13
|
-
FipeApi::ModelResponseConverter.new(method, fipe_response)
|
14
|
-
when :model_through_year
|
15
|
-
FipeApi::ModelThroughYearResponseConverter.new(method, fipe_response)
|
16
|
-
else
|
17
|
-
FipeApi::ResponseConverter.new(method, fipe_response)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
end
|
22
|
-
end
|
@@ -1,49 +0,0 @@
|
|
1
|
-
require 'unirest'
|
2
|
-
require 'net/http'
|
3
|
-
require 'json'
|
4
|
-
|
5
|
-
module FipeApi
|
6
|
-
class ApiRequester
|
7
|
-
|
8
|
-
API_METHOD = {
|
9
|
-
reference_table: "/ConsultarTabelaDeReferencia",
|
10
|
-
brand: "/ConsultarMarcas",
|
11
|
-
model: "/ConsultarModelos",
|
12
|
-
model_year: "/ConsultarAnoModelo",
|
13
|
-
model_through_year: "/ConsultarModelosAtravesDoAno",
|
14
|
-
full: "/ConsultarValorComTodosParametros" }
|
15
|
-
|
16
|
-
FIPE_URL = "http://veiculos.fipe.org.br"
|
17
|
-
FIPE_API = "/api/veiculos"
|
18
|
-
|
19
|
-
def initialize(method, parameters)
|
20
|
-
@method = method
|
21
|
-
@parameters = parameters
|
22
|
-
end
|
23
|
-
|
24
|
-
def send
|
25
|
-
response = Unirest.post(api_request_url, headers: configure_header, parameters: @parameters)
|
26
|
-
FipeApi::ApiResponse.new(@method, response.body)
|
27
|
-
end
|
28
|
-
|
29
|
-
private
|
30
|
-
|
31
|
-
def api_request_url
|
32
|
-
"#{FIPE_URL + FIPE_API + API_METHOD[@method]}"
|
33
|
-
end
|
34
|
-
|
35
|
-
def configure_header
|
36
|
-
{ 'Host' => FIPE_URL.gsub("http://", ""),
|
37
|
-
'User-Agent' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:41.0) Gecko/20100101 Firefox/41.0',
|
38
|
-
'Accept' => 'application/json, text/javascript, */*; q=0.01',
|
39
|
-
'Accept-Language' => 'pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3',
|
40
|
-
'Accept-Encoding' => 'gzip, deflate',
|
41
|
-
'Content-Type' => 'text/plain; charset=UTF-8',
|
42
|
-
'X-Requested-With' => 'XMLHttpRequest',
|
43
|
-
'Pragma' => 'no-cache',
|
44
|
-
'Cache-Control' => 'max-age=0',
|
45
|
-
'Referer' => "#{FIPE_URL}/",
|
46
|
-
'Connection' => 'keep-alive' }
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
module FipeApi
|
2
|
-
class ApiResponse
|
3
|
-
|
4
|
-
attr_reader :response
|
5
|
-
|
6
|
-
def initialize(method, response)
|
7
|
-
@method = method
|
8
|
-
@response = convert_response_receipt(response)
|
9
|
-
end
|
10
|
-
|
11
|
-
def valid_response?
|
12
|
-
!@response.has_key?(:fipe_error_message)
|
13
|
-
end
|
14
|
-
|
15
|
-
private
|
16
|
-
|
17
|
-
def convert_response_receipt(response)
|
18
|
-
converter = FipeApi::ResponseConverterBuilder.build(@method, response)
|
19
|
-
converter.convert
|
20
|
-
end
|
21
|
-
|
22
|
-
end
|
23
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
require 'minitest/autorun'
|
2
|
-
require_relative '../lib/fipextractor/api_parameter/parameter_converter'
|
3
|
-
|
4
|
-
describe FipeApi::ParameterConverter do
|
5
|
-
|
6
|
-
it "should parameters of full method would be converted" do
|
7
|
-
parameters = { reference_table_id: 189, vehicle_type: 1, brand_id: 3, model_id: 7, model_year: "1999", year: "1999", fuel_type_id: 1 }
|
8
|
-
FipeApi::ParameterConverter.to_fipe_pattern(parameters).must_equal({
|
9
|
-
codigoTabelaReferencia: 189,
|
10
|
-
codigoTipoVeiculo: 1,
|
11
|
-
codigoMarca: 3,
|
12
|
-
codigoModelo: 7,
|
13
|
-
anoModelo: "1999",
|
14
|
-
ano: "1999",
|
15
|
-
codigoTipoCombustivel: 1 })
|
16
|
-
end
|
17
|
-
|
18
|
-
it "should vehicle_type :car converted to value 1" do
|
19
|
-
FipeApi::ParameterConverter.vehicle_type_to_id(:car).must_equal 1
|
20
|
-
end
|
21
|
-
|
22
|
-
|
23
|
-
end
|
@@ -1,67 +0,0 @@
|
|
1
|
-
require 'minitest/autorun'
|
2
|
-
require_relative '../lib/fipextractor/api_parameter/parameter_validator'
|
3
|
-
|
4
|
-
describe FipeApi::ParameterValidator do
|
5
|
-
|
6
|
-
it "should reference_table pass through validation" do
|
7
|
-
validator = FipeApi::ParameterValidator.new(:reference_table, {})
|
8
|
-
validator.is_ok?.must_equal true
|
9
|
-
end
|
10
|
-
|
11
|
-
it "should have valid parameters for brand method" do
|
12
|
-
validator = FipeApi::ParameterValidator.new(:brand, {reference_table_id: 189, vehicle_type: :truck})
|
13
|
-
validator.is_ok?.must_equal true
|
14
|
-
end
|
15
|
-
|
16
|
-
it "should have invalid parameters for brand method" do
|
17
|
-
validator = FipeApi::ParameterValidator.new(:brand, {reference_table_id: 189})
|
18
|
-
validator.is_ok?.must_equal false
|
19
|
-
end
|
20
|
-
|
21
|
-
it "should have valid parameters for model method" do
|
22
|
-
validator = FipeApi::ParameterValidator.new(:model, {reference_table_id: 189, vehicle_type: :truck, brand_id: 102})
|
23
|
-
validator.is_ok?.must_equal true
|
24
|
-
end
|
25
|
-
|
26
|
-
it "should have invalid parameters for model method" do
|
27
|
-
validator = FipeApi::ParameterValidator.new(:model, {reference_table_id: 189, vehicle_type: :truck})
|
28
|
-
validator.is_ok?.must_equal false
|
29
|
-
end
|
30
|
-
|
31
|
-
it "should have valid parameters for model_year method" do
|
32
|
-
validator = FipeApi::ParameterValidator.new(:model_year, {reference_table_id: 189, vehicle_type: :truck, brand_id: 102, model_id: 6704})
|
33
|
-
validator.is_ok?.must_equal true
|
34
|
-
end
|
35
|
-
|
36
|
-
it "should have invalid parameters for model_year method" do
|
37
|
-
validator = FipeApi::ParameterValidator.new(:model_year, {reference_table_id: 189, vehicle_type: :truck, brand_id: 102})
|
38
|
-
validator.is_ok?.must_equal false
|
39
|
-
end
|
40
|
-
|
41
|
-
it "should have valid parameters for model_through_year method" do
|
42
|
-
validator = FipeApi::ParameterValidator.new(:model_through_year, {reference_table_id: 205, vehicle_type: :car, brand_id: 3, model_id: 7, year: '1997-1', model_year: '1997', fuel_type_id: 1})
|
43
|
-
validator.is_ok?.must_equal true
|
44
|
-
end
|
45
|
-
|
46
|
-
it "should have invalid parameters for model_through_year method" do
|
47
|
-
validator = FipeApi::ParameterValidator.new(:model_through_year, {reference_table_id: 205, vehicle_type: :car, brand_id: 3, model_id: 7, year: '1997-1'})
|
48
|
-
validator.is_ok?.must_equal false
|
49
|
-
end
|
50
|
-
|
51
|
-
it "should have valid parameters for full method" do
|
52
|
-
validator = FipeApi::ParameterValidator.new(:full, {reference_table_id: 189, vehicle_type: :car, brand_id: 3, model_id: 7, model_year: "1999", year: "1999", fuel_type_id: 1})
|
53
|
-
validator.is_ok?.must_equal true
|
54
|
-
end
|
55
|
-
|
56
|
-
it "should have invalid parameters for full method" do
|
57
|
-
validator = FipeApi::ParameterValidator.new(:full, {reference_table_id: 189, vehicle_type: :car, brand_id: 3, model_id: 7, model_year: "1999", year: "1999"})
|
58
|
-
validator.is_ok?.must_equal false
|
59
|
-
end
|
60
|
-
|
61
|
-
it "should have invalid parameters reports with a array of messages" do
|
62
|
-
validator = FipeApi::ParameterValidator.new(:model_through_year, {reference_table_id: 205, vehicle_type: :car, brand_id: 3, model_id: 7, year: '1997-1'})
|
63
|
-
validator.message.must_equal ["model_year does not exists, and is mandatory for model_through_year method!", "fuel_type_id does not exists, and is mandatory for model_through_year method!"]
|
64
|
-
end
|
65
|
-
|
66
|
-
|
67
|
-
end
|
@@ -1,35 +0,0 @@
|
|
1
|
-
require 'minitest/autorun'
|
2
|
-
require_relative '../lib/fipextractor/api_response/response_converter_builder'
|
3
|
-
|
4
|
-
describe FipeApi::ResponseConverter do
|
5
|
-
|
6
|
-
it "should response of full method converted to application pattern" do
|
7
|
-
response ={
|
8
|
-
"Valor"=>"R$ 15.577,00",
|
9
|
-
"Marca"=>"Alfa Romeo",
|
10
|
-
"Modelo"=>"145 Quadrifoglio 2.0",
|
11
|
-
"AnoModelo"=>1999,
|
12
|
-
"Combustivel"=>"Gasolina",
|
13
|
-
"CodigoFipe"=>"006002-0",
|
14
|
-
"MesReferencia"=>"março de 2016 ",
|
15
|
-
"Autenticacao"=>"fictional_hash",
|
16
|
-
"TipoVeiculo"=>1,
|
17
|
-
"SiglaCombustivel"=>"G",
|
18
|
-
"DataConsulta"=>"quarta-feira, 22 de fevereiro de 2017 22:35" }
|
19
|
-
converter = FipeApi::ResponseConverterBuilder.build(:full, response)
|
20
|
-
converter.convert.must_equal({
|
21
|
-
cost: "R$ 15.577,00",
|
22
|
-
brand: "Alfa Romeo",
|
23
|
-
model: "145 Quadrifoglio 2.0",
|
24
|
-
model_year: 1999,
|
25
|
-
fuel: "Gasolina",
|
26
|
-
fipe_id: "006002-0",
|
27
|
-
reference_month: "março de 2016 ",
|
28
|
-
authentication_hash: "fictional_hash",
|
29
|
-
vehicle_type: 1,
|
30
|
-
fuel_symbol: "G",
|
31
|
-
query_date: "quarta-feira, 22 de fevereiro de 2017 22:35" })
|
32
|
-
end
|
33
|
-
|
34
|
-
|
35
|
-
end
|