fipextractor 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +50 -0
- data/Gemfile +3 -0
- data/README.md +118 -0
- data/Rakefile +10 -0
- data/fipextractor.gemspec +21 -0
- data/lib/fipextractor/api_parameter/parameter_converter.rb +39 -0
- data/lib/fipextractor/api_parameter/parameter_validator.rb +46 -0
- data/lib/fipextractor/api_response/brand_response_converter.rb +10 -0
- data/lib/fipextractor/api_response/model_response_converter.rb +45 -0
- data/lib/fipextractor/api_response/model_through_year_response_converter.rb +10 -0
- data/lib/fipextractor/api_response/response_converter.rb +60 -0
- data/lib/fipextractor/api_response/response_converter_builder.rb +22 -0
- data/lib/fipextractor/fipe_api/api_factory.rb +12 -0
- data/lib/fipextractor/fipe_api/api_requester.rb +49 -0
- data/lib/fipextractor/fipe_api/api_response.rb +23 -0
- data/lib/fipextractor.rb +40 -0
- data/test/parameter_converter_spec.rb +23 -0
- data/test/parameter_validator_spec.rb +67 -0
- data/test/response_converter_spec.rb +35 -0
- metadata +107 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 86bfa1930e1374a1c6e65b1b466f6103ae0da70a
|
4
|
+
data.tar.gz: 3bbd65e7bebeef4ce48afaa6a9e92a2d57fd2136
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ab6ba54eedef1e09c98ba6c4d51847252061dd313169eb15593eb53a4b6d616609a07128bc0541138a0970b2bc4fca34937e612af89b9013952512711658ebcb
|
7
|
+
data.tar.gz: 426aa8c17105c2a2c735f52b1b17cf7beee7efd7747dc029b6e566cf495e487d04fd83c570e35442e90947d7f8f5ba6762e73e4b82742f8455cc4331411872cb
|
data/.gitignore
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/spec/examples.txt
|
9
|
+
/test/tmp/
|
10
|
+
/test/version_tmp/
|
11
|
+
/tmp/
|
12
|
+
|
13
|
+
# Used by dotenv library to load environment variables.
|
14
|
+
# .env
|
15
|
+
|
16
|
+
## Specific to RubyMotion:
|
17
|
+
.dat*
|
18
|
+
.repl_history
|
19
|
+
build/
|
20
|
+
*.bridgesupport
|
21
|
+
build-iPhoneOS/
|
22
|
+
build-iPhoneSimulator/
|
23
|
+
|
24
|
+
## Specific to RubyMotion (use of CocoaPods):
|
25
|
+
#
|
26
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
27
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
28
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
29
|
+
#
|
30
|
+
# vendor/Pods/
|
31
|
+
|
32
|
+
## Documentation cache and generated files:
|
33
|
+
/.yardoc/
|
34
|
+
/_yardoc/
|
35
|
+
/doc/
|
36
|
+
/rdoc/
|
37
|
+
|
38
|
+
## Environment normalization:
|
39
|
+
/.bundle/
|
40
|
+
/vendor/bundle
|
41
|
+
/lib/bundler/man/
|
42
|
+
|
43
|
+
# for a library or gem, you might want to ignore these files since the code is
|
44
|
+
# intended to run in multiple environments; otherwise, check them in:
|
45
|
+
Gemfile.lock
|
46
|
+
# .ruby-version
|
47
|
+
# .ruby-gemset
|
48
|
+
|
49
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
50
|
+
.rvmrc
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
## FipExtractor
|
2
|
+
---
|
3
|
+
FipExtractor is a easy-to-use translator of the FIPE API.
|
4
|
+
|
5
|
+
Actually FIPE only release the information about their vehicle market search on
|
6
|
+
your website. To get information generally you need to do many DOM manipulations,
|
7
|
+
with your user common behaviour (clicking in selects, and buttons).
|
8
|
+
|
9
|
+
FipExtractor is here for you! We consumes the FIPE API directly, and
|
10
|
+
translates the some parameters that have non-sense labels. Making this gem more useful for you.
|
11
|
+
|
12
|
+
### Using FipExtractor
|
13
|
+
---
|
14
|
+
|
15
|
+
FipExtractor only provides a easy way to consume FIPE informations, we don't create any
|
16
|
+
conversions to generate SQL, or another way to insert data into databases.
|
17
|
+
You can do this with the FipExtractor application result data.
|
18
|
+
|
19
|
+
We use the FIPE API, and with this information, we clearly need to work under their
|
20
|
+
API rules. FipExtractor provides a validator that shows to you if your request parameters
|
21
|
+
are malformed.
|
22
|
+
|
23
|
+
FipExtractor creates a huge and simple abstraction around API requests.
|
24
|
+
|
25
|
+
The API routes are:
|
26
|
+
|
27
|
+
|Route|Parameters|
|
28
|
+
|------|----------|
|
29
|
+
|reference_table|none|
|
30
|
+
|brand|reference_table_id, vehicle_type|
|
31
|
+
|model|reference_table_id, vehicle_type, brand_id|
|
32
|
+
|model_through_year|reference_table_id, vehicle_type, brand_id, model_id|
|
33
|
+
|model_year|reference_table_id, vehicle_type, brand_id, model_id, year, model_year, fuel_type_id|
|
34
|
+
|full|reference_table_id, vehicle_type, brand_id, model_id, year, model_year, fuel_type_id, vehicle_type|
|
35
|
+
|
36
|
+
---
|
37
|
+
#### Building a request
|
38
|
+
|
39
|
+
````ruby
|
40
|
+
extractor = FipExtractor.new(:full, {reference_table_id: 189, vehicle_type: :car, brand_id: 3, model_id: 7, model_year: "1999", year: "1999", fuel_type_id: 1})
|
41
|
+
````
|
42
|
+
|
43
|
+
You only need to create your FipExtractor object, and fill the parameters ``method`` and ``parameters``
|
44
|
+
|
45
|
+
The ``method`` only accepts the Symbol reference.
|
46
|
+
|
47
|
+
The ``parameters`` only accepts a Hash. (optional on ``:reference_table`` method)
|
48
|
+
|
49
|
+
---
|
50
|
+
#### Checking the request parameters
|
51
|
+
|
52
|
+
FipExtractor allows to submit a request directly, or check your parameters. You can choose, if you will validate your parameters, or not.
|
53
|
+
|
54
|
+
Example of validation:
|
55
|
+
|
56
|
+
````ruby
|
57
|
+
extractor = FipExtractor.new(:brand, {reference_table_id: 189, vehicle_type: :truck})
|
58
|
+
validator = extractor.validate_parameters
|
59
|
+
if validator.is_ok?
|
60
|
+
api = extractor.request
|
61
|
+
api = api.send
|
62
|
+
puts api.response
|
63
|
+
else
|
64
|
+
puts validator.message
|
65
|
+
end
|
66
|
+
````
|
67
|
+
|
68
|
+
The validation layer is a object like everything in Ruby. Calling ``validate_parameters``, you automatic validate your parameters and generate a object with the ``is_ok?`` and ``message`` methods.
|
69
|
+
|
70
|
+
``is_ok?`` Returns a ``boolean``, if all parameters are OK, you get a ``true`` value here, otherwise a ``false`` value appears.
|
71
|
+
|
72
|
+
``message`` Returns an array of String with the parameters problems with the request method, if have one.
|
73
|
+
|
74
|
+
---
|
75
|
+
#### Getting the data
|
76
|
+
|
77
|
+
FipExtractor provides a friendly response data layout. All original API parameters are converted to a FipExtractor pattern. This pattern is more developer-like.
|
78
|
+
|
79
|
+
The original API response layout provides some JSON keys like ``Label`` and ``Value``. This patterns isn't a good pattern. FipExtractor converts these kind of parameters, in a specific Hash based on request method.
|
80
|
+
|
81
|
+
Getting data response:
|
82
|
+
````ruby
|
83
|
+
extractor = FipExtractor.new(:model, {reference_table_id: 189, vehicle_type: :truck, brand_id: 102})
|
84
|
+
api = extractor.request
|
85
|
+
api = api.send
|
86
|
+
puts api.response
|
87
|
+
````
|
88
|
+
|
89
|
+
Output:
|
90
|
+
|
91
|
+
````ruby
|
92
|
+
{:brand_name=>"AGRALE", :brand_id=>"102"}
|
93
|
+
{:brand_name=>"CHEVROLET", :brand_id=>"103"}
|
94
|
+
{:brand_name=>"CICCOBUS", :brand_id=>"121"}
|
95
|
+
{:brand_name=>"DAF", :brand_id=>"197"}
|
96
|
+
{:brand_name=>"EFFA-JMC", :brand_id=>"179"}
|
97
|
+
{:brand_name=>"FIAT", :brand_id=>"104"}
|
98
|
+
{:brand_name=>"FORD", :brand_id=>"105"}
|
99
|
+
{:brand_name=>"FOTON", :brand_id=>"191"}
|
100
|
+
{:brand_name=>"GMC", :brand_id=>"106"}
|
101
|
+
{:brand_name=>"HYUNDAI", :brand_id=>"181"}
|
102
|
+
{:brand_name=>"IVECO", :brand_id=>"122"}
|
103
|
+
{:brand_name=>"MAN", :brand_id=>"184"}
|
104
|
+
{:brand_name=>"MARCOPOLO", :brand_id=>"108"}
|
105
|
+
{:brand_name=>"MASCARELLO", :brand_id=>"196"}
|
106
|
+
{:brand_name=>"MAXIBUS", :brand_id=>"194"}
|
107
|
+
{:brand_name=>"MERCEDES-BENZ", :brand_id=>"109"}
|
108
|
+
{:brand_name=>"NAVISTAR", :brand_id=>"110"}
|
109
|
+
{:brand_name=>"NEOBUS", :brand_id=>"111"}
|
110
|
+
{:brand_name=>"PUMA-ALFA", :brand_id=>"112"}
|
111
|
+
{:brand_name=>"SAAB-SCANIA", :brand_id=>"113"}
|
112
|
+
{:brand_name=>"SCANIA", :brand_id=>"114"}
|
113
|
+
{:brand_name=>"SHACMAN", :brand_id=>"193"}
|
114
|
+
{:brand_name=>"SINOTRUK", :brand_id=>"166"}
|
115
|
+
{:brand_name=>"VOLKSWAGEN", :brand_id=>"115"}
|
116
|
+
{:brand_name=>"VOLVO", :brand_id=>"116"}
|
117
|
+
{:brand_name=>"WALKBUS", :brand_id=>"144"}
|
118
|
+
````
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'fipextractor'
|
3
|
+
s.version = '0.0.1'
|
4
|
+
s.summary = 'FipExtractor'
|
5
|
+
s.description = 'Consume, and extract data about vehicles from FIPE API'
|
6
|
+
s.authors = ["Rynaro"]
|
7
|
+
s.email = 'iam@henriquelavezzo.com.br'
|
8
|
+
s.homepage = 'https://github.com/Rynaro/fipextractor'
|
9
|
+
s.license = 'MIT'
|
10
|
+
|
11
|
+
s.add_dependency('unirest', '~> 1.1')
|
12
|
+
|
13
|
+
s.add_development_dependency('minitest', '~> 5.10')
|
14
|
+
s.add_development_dependency('rake')
|
15
|
+
|
16
|
+
s.required_ruby_version = '~> 2.0'
|
17
|
+
|
18
|
+
s.files = `git ls-files`.split("\n")
|
19
|
+
s.test_files = `git ls-files -- test/*`.split("\n")
|
20
|
+
s.require_paths = ['lib']
|
21
|
+
end
|
@@ -0,0 +1,39 @@
|
|
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
|
@@ -0,0 +1,46 @@
|
|
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
|
@@ -0,0 +1,45 @@
|
|
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
|
@@ -0,0 +1,60 @@
|
|
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
|
@@ -0,0 +1,22 @@
|
|
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
|
@@ -0,0 +1,49 @@
|
|
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
|
@@ -0,0 +1,23 @@
|
|
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
|
data/lib/fipextractor.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require_relative 'fipextractor/fipe_api/api_factory'
|
2
|
+
require_relative 'fipextractor/api_parameter/parameter_converter'
|
3
|
+
require_relative 'fipextractor/api_parameter/parameter_validator'
|
4
|
+
require_relative 'fipextractor/api_response/response_converter_builder'
|
5
|
+
|
6
|
+
class FipExtractor
|
7
|
+
|
8
|
+
attr_accessor :method, :parameters
|
9
|
+
|
10
|
+
def initialize(method, parameters = {})
|
11
|
+
@method = method
|
12
|
+
@parameters = parameters
|
13
|
+
end
|
14
|
+
|
15
|
+
def validate_parameters
|
16
|
+
FipeApi::ParameterValidator.new(@method, @parameters)
|
17
|
+
end
|
18
|
+
|
19
|
+
def request
|
20
|
+
@parameters = convert_parameters_to_api_pattern(@parameters)
|
21
|
+
FipeApi::ApiFactory.create_requester(@method, @parameters)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def convert_parameters_to_api_pattern(parameters)
|
27
|
+
parameters[:vehicle_type] = convert_humanized_vehicle_id(parameters[:vehicle_type])
|
28
|
+
parameters[:search] = add_non_obstrutive_search_type if @method == :full
|
29
|
+
FipeApi::ParameterConverter.to_fipe_pattern(parameters)
|
30
|
+
end
|
31
|
+
|
32
|
+
def convert_humanized_vehicle_id(vehicle_type)
|
33
|
+
FipeApi::ParameterConverter.vehicle_type_to_id(vehicle_type)
|
34
|
+
end
|
35
|
+
|
36
|
+
def add_non_obstrutive_search_type
|
37
|
+
'tradicional'
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
@@ -0,0 +1,23 @@
|
|
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
|
@@ -0,0 +1,67 @@
|
|
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
|
@@ -0,0 +1,35 @@
|
|
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
|
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fipextractor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rynaro
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-02-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: unirest
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.10'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.10'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Consume, and extract data about vehicles from FIPE API
|
56
|
+
email: iam@henriquelavezzo.com.br
|
57
|
+
executables: []
|
58
|
+
extensions: []
|
59
|
+
extra_rdoc_files: []
|
60
|
+
files:
|
61
|
+
- ".gitignore"
|
62
|
+
- Gemfile
|
63
|
+
- README.md
|
64
|
+
- Rakefile
|
65
|
+
- fipextractor.gemspec
|
66
|
+
- lib/fipextractor.rb
|
67
|
+
- lib/fipextractor/api_parameter/parameter_converter.rb
|
68
|
+
- lib/fipextractor/api_parameter/parameter_validator.rb
|
69
|
+
- lib/fipextractor/api_response/brand_response_converter.rb
|
70
|
+
- lib/fipextractor/api_response/model_response_converter.rb
|
71
|
+
- lib/fipextractor/api_response/model_through_year_response_converter.rb
|
72
|
+
- lib/fipextractor/api_response/response_converter.rb
|
73
|
+
- lib/fipextractor/api_response/response_converter_builder.rb
|
74
|
+
- lib/fipextractor/fipe_api/api_factory.rb
|
75
|
+
- lib/fipextractor/fipe_api/api_requester.rb
|
76
|
+
- lib/fipextractor/fipe_api/api_response.rb
|
77
|
+
- test/parameter_converter_spec.rb
|
78
|
+
- test/parameter_validator_spec.rb
|
79
|
+
- test/response_converter_spec.rb
|
80
|
+
homepage: https://github.com/Rynaro/fipextractor
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
metadata: {}
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - "~>"
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '2.0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 2.6.8
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: FipExtractor
|
104
|
+
test_files:
|
105
|
+
- test/parameter_converter_spec.rb
|
106
|
+
- test/parameter_validator_spec.rb
|
107
|
+
- test/response_converter_spec.rb
|