Class: Hachioji::Taikikanshi::BaseClient
- Inherits:
-
Object
- Object
- Hachioji::Taikikanshi::BaseClient
show all
- Defined in:
- lib/hachioji/taikikanshi/base_client.rb
Direct Known Subclasses
Ch4Client, CoClient, HumClient, NmhcClient, No2Client, NoClient, NoxClient, OxClient, Pm25Client, So2Client, SpmClient, TempClient, ThcClient, WdClient, WsClient
Constant Summary
collapse
- ENDPOINT_HOST =
"http://www.taikikansi-hachioji.jp"
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Instance Attribute Details
#values ⇒ Object
Returns the value of attribute values.
5
6
7
|
# File 'lib/hachioji/taikikanshi/base_client.rb', line 5
def values
@values
end
|
#values_previous ⇒ Object
Returns the value of attribute values_previous.
5
6
7
|
# File 'lib/hachioji/taikikanshi/base_client.rb', line 5
def values_previous
@values_previous
end
|
Class Method Details
.available_average ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/hachioji/taikikanshi/base_client.rb', line 75
def available_average
define_method("averages_24h") {
averages = {}
parse if !@values || !@values_previous
@areas.each do |area|
area_values = @values_previous[area].values + @values[area].values
area_values.compact!
base_values = area_values.last(24)
averages[area] = base_values.inject(0){ |sum,v| sum += v }.to_f / base_values.count
end
averages
}
define_method("averages_previous") {
averages = {}
parse if !@values || !@values_previous
@areas.each do |area|
area_values = @values_previous[area].values
area_values.compact!
base_values = area_values.last(24)
averages[area] = base_values.inject(0){ |sum,v| sum += v }.to_f / base_values.count
end
averages
}
end
|
.endpoint_path(path) ⇒ Object
69
70
71
72
73
|
# File 'lib/hachioji/taikikanshi/base_client.rb', line 69
def endpoint_path(path)
define_method("endpoint_url") {
"#{ENDPOINT_HOST}#{path}"
}
end
|
.measured_value_type(type) ⇒ Object
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/hachioji/taikikanshi/base_client.rb', line 105
def measured_value_type(type)
available_average if type == :float || type == :integer
define_method("measured_value_type") {
type
}
define_method("measured_value") { |read_value|
case type
when :float
read_value.to_f if read_value.match(/^[\d\.]+$/)
when :integer
read_value.to_i if read_value.match(/^[\d]+$/)
when :string
read_value.to_s
else
nil
end
}
end
|
Instance Method Details
#latest_values ⇒ Object
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/hachioji/taikikanshi/base_client.rb', line 57
def latest_values
values = {}
parse if !@values
@areas.each do |area|
values[area] = @values[area].values.last
end
values
end
|
#parse ⇒ 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
49
50
51
52
53
54
55
|
# File 'lib/hachioji/taikikanshi/base_client.rb', line 8
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[2]/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
|