Class: Hachioji::Taikikanshi::WdClient

Inherits:
BaseClient
  • Object
show all
Defined in:
lib/hachioji/taikikanshi/wd_client.rb

Constant Summary

Constants inherited from BaseClient

BaseClient::ENDPOINT_HOST

Instance Attribute Summary

Attributes inherited from BaseClient

#values, #values_previous

Instance Method Summary collapse

Methods inherited from BaseClient

available_average, endpoint_path, #latest_values, measured_value_type

Instance Method Details

#parseObject



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
# File 'lib/hachioji/taikikanshi/wd_client.rb', line 9

def parse
  html = open(self.endpoint_url).read
  @doc = Nokogiri::HTML(html.toutf8)
  @areas = []
  @values = {}
  @values_previous = {}
  got_date = false
  parsing_previous = false

  @doc.xpath("/html/body/table/tr").each_with_index do |tr,tri|
    if tri == 0
      tr.xpath("./td/font").each_with_index do |font,fonti|
        next if fonti == 0
        @areas << font.text
        @values[font.text] = MeasuredValue.new(area_name: font.text, value_type: measured_value_type)
        @values_previous[font.text] = MeasuredValue.new(area_name: font.text, value_type: measured_value_type)
      end
    else
      tdidx = 0
      tr.xpath("./td").each do |td|
        if td["rowspan"]
          month, day = td.xpath("./font").first.text.scan(/(\d+)/(\d+)/).first
          if !got_date
            @areas.each do |area|
              @values[area].date = Date.new(Date.today.year, month.to_i, day.to_i)
            end
            got_date = true
          else
            @areas.each do |area|
              @values_previous[area].date = Date.new(Date.today.year, month.to_i, day.to_i)
            end
            parsing_previous = true
          end
        else
          val = td.xpath("./font").first.text.strip
          next if val.match(/\d+時/)
          value = measured_value(val)
          if parsing_previous
            @values_previous[@areas[tdidx]].values.insert(0, value)
          else
            @values[@areas[tdidx]].values.insert(0, value)
          end
          tdidx += 1
        end
      end
    end
  end
end