-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumptio.rb
86 lines (78 loc) · 2.84 KB
/
consumptio.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
require "cuba"
require "cuba/sugar/as"
require "nokogiri"
require "httparty"
require "json"
module ParseHelper
def parse_engines(doc)
engines = []
doc.css('.aclist_raport .bgl2navy, .aclist_raport tr.bgl1navy').each do |data|
engines << {
fuel: data.css('a.underline').attr('href').value.scan(/-([a-z]*)-/)[0][0] ,
model: data.css('a.underline b').text ,
avg: data.css('td:nth-child(2) span:first-child').text.sub(',','.') ,
r_no: data.css('td:nth-child(3)').text ,
cost: data.css('td:last-child span:first-child').text.scan(/[0-9]+,[0-9]*/)[0] ,
details: data.css('a.underline').attr('href').value
}
end
return engines
end
end
Cuba.plugin ParseHelper
Cuba.plugin Cuba::Sugar::As
Cuba.define do
on get do
on root do
doc = Nokogiri::HTML(HTTParty.get("http://www.autocentrum.pl/spalanie"))
collection = []
doc = doc.css('.m_4lst li a').each do |item|
collection << {
short: item.attr('href').split('/')[2],
name: item.css('span').text
}
end
as_json do
{ make: collection }
end
end
on ":make" do |make|
doc = Nokogiri::HTML(HTTParty.get("http://www.autocentrum.pl/spalanie/#{make}"))
collection = []
doc.css('.m_3lst li a').each do |item|
collection << {
short: item.attr('href').split('/')[3],
name: item.css('span').text
}
end
models = collection
on ":model" do |model|
doc = Nokogiri::HTML(HTTParty.get("http://www.autocentrum.pl/spalanie/#{make}/#{model}"))
engines = parse_engines(doc)
generations = false
if engines.empty?
doc.css('ul.gens_list p a').each do |item|
engines << {
name: item.text,
short: item.attr('href').split('/')[4]
}
generations = true
end
end
on ":gene" do |gene|
doc = Nokogiri::HTML(HTTParty.get("http://www.autocentrum.pl/spalanie/#{make}/#{model}/#{gene}"))
engines = parse_engines(doc)
as_json do
{ engines: engines, generations: false }
end
end
as_json do
{ engines: engines , generations: generations }
end
end
as_json do
{ model: collection }
end
end
end
end