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
|
# File 'lib/tottori-opendata-pm25-api/client.rb', line 8
def get(t = Time.now)
time = t.in_time_zone('Asia/Tokyo')
date = time.to_date
uri = 'http://tottori-taiki.users.tori-info.co.jp/taiki/pc/graph/?'
uri << [
'mode=table',
'graph=hour',
'item=021',
"date=#{date.strftime('%Y%m%d')}",
(1..6).map{ |i| 'term[]=%03d' % i }.join('&')
].join('&')
content = HTTPClient.get_content(uri)
doc = Nokogiri::HTML.parse(content)
data = []
doc.xpath('//table[@class="commonTable"]/tbody/tr').each do |tr|
row = tr.children
name = row.shift.text.strip
values = row.map{ |c|
s = c.text.strip
s.empty? ? nil : s.to_i
}.map.with_index{ |value, i|
iso8601_time = ('%4d-%02d-%02dT%02d:00:00+09:00' % [date.year, date.month, date.day, i]).strip
{
time: Time.iso8601(iso8601_time),
value: value
}
}
record = {
name: name,
values: values
}
data << record
end
return data
end
|