Class: Tottori::OpenData::PM25::API::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/tottori-opendata-pm25-api/client.rb

Class Method Summary collapse

Class Method Details

.get(t = Time.now) ⇒ Object



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)
  # Tottori Time
  time = t.in_time_zone('Asia/Tokyo')
  # Tottori Date
  date = time.to_date
  # URI
  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('&')
  # HTTP GET
  content = HTTPClient.get_content(uri)
  # Parse
  doc = Nokogiri::HTML.parse(content)
  # Scrap
  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