|
|
@@ -25,7 +25,7 @@ module Agents
|
25
|
25
|
Events look like this:
|
26
|
26
|
|
27
|
27
|
{
|
28
|
|
- "location": 12345,
|
|
28
|
+ "location": "12345",
|
29
|
29
|
"date": {
|
30
|
30
|
"epoch": "1357959600",
|
31
|
31
|
"pretty": "10:00 PM EST on January 11, 2013"
|
|
|
@@ -53,7 +53,7 @@ module Agents
|
53
|
53
|
end
|
54
|
54
|
|
55
|
55
|
def key_setup?
|
56
|
|
- options['api_key'] && options['api_key'] != "your-key"
|
|
56
|
+ options['api_key'].present? && options['api_key'] != "your-key"
|
57
|
57
|
end
|
58
|
58
|
|
59
|
59
|
def default_options
|
|
|
@@ -61,16 +61,24 @@ module Agents
|
61
|
61
|
'service' => 'wunderground',
|
62
|
62
|
'api_key' => 'your-key',
|
63
|
63
|
'location' => '94103',
|
64
|
|
- 'which_day' => '0'
|
|
64
|
+ 'which_day' => '1'
|
65
|
65
|
}
|
66
|
66
|
end
|
67
|
67
|
|
|
68
|
+ def service
|
|
69
|
+ options["service"].presence || "wunderground"
|
|
70
|
+ end
|
|
71
|
+
|
|
72
|
+ def which_day
|
|
73
|
+ (options["which_day"].presence || 1).to_i
|
|
74
|
+ end
|
|
75
|
+
|
68
|
76
|
def validate_options
|
69
|
|
- errors.add(:base, "service is required") unless options['service'].present?
|
70
|
|
- errors.add(:base, "service must be set to 'forecastio' or 'wunderground'") unless ["forecastio", "wunderground"].include?(options['service'])
|
|
77
|
+ errors.add(:base, "service is required") unless service.present?
|
|
78
|
+ errors.add(:base, "service must be set to 'forecastio' or 'wunderground'") unless ["forecastio", "wunderground"].include?(service)
|
71
|
79
|
errors.add(:base, "location is required") unless options['location'].present?
|
72
|
|
- errors.add(:base, "api_key is required") unless options['api_key'].present?
|
73
|
|
- errors.add(:base, "which_day selection is required") unless options['which_day'].present?
|
|
80
|
+ errors.add(:base, "api_key is required") unless key_setup?
|
|
81
|
+ errors.add(:base, "which_day selection is required") unless which_day.present?
|
74
|
82
|
end
|
75
|
83
|
|
76
|
84
|
def wunderground
|
|
|
@@ -80,18 +88,18 @@ module Agents
|
80
|
88
|
|
81
|
89
|
def forecastio
|
82
|
90
|
ForecastIO.api_key = options['api_key'] if key_setup?
|
83
|
|
- data = ForecastIO.forecast(options['location'].split(',')[0],options['location'].split(',')[1])['daily']['data']
|
|
91
|
+ data = ForecastIO.forecast(options['location'].split(',')[0], options['location'].split(',')[1])['daily']['data']
|
84
|
92
|
return data
|
85
|
93
|
end
|
86
|
94
|
|
87
|
95
|
def model(data,service,which_day)
|
88
|
96
|
day = Hash.new
|
89
|
97
|
if service == "wunderground"
|
90
|
|
- day = data[which_day.to_i]
|
|
98
|
+ day = data[which_day]
|
91
|
99
|
elsif service == "forecastio"
|
92
|
100
|
data.each do |value|
|
93
|
101
|
timestamp = Time.at(value.time)
|
94
|
|
- if (timestamp.to_date - Time.now.to_date).to_i == which_day.to_i
|
|
102
|
+ if (timestamp.to_date - Time.now.to_date).to_i == which_day
|
95
|
103
|
day = {
|
96
|
104
|
'date' => {
|
97
|
105
|
'epoch' => value.time.to_s,
|
|
|
@@ -158,10 +166,10 @@ module Agents
|
158
|
166
|
|
159
|
167
|
def check
|
160
|
168
|
if key_setup?
|
161
|
|
- if options['service'] == 'forecastio'
|
162
|
|
- weather = model(forecastio,options['service'],options['which_day'])
|
163
|
|
- elsif options['service'] == 'wunderground'
|
164
|
|
- weather = model(wunderground,options['service'],options['which_day'])
|
|
169
|
+ if service == 'forecastio'
|
|
170
|
+ weather = model(forecastio, service, which_day)
|
|
171
|
+ elsif service == 'wunderground'
|
|
172
|
+ weather = model(wunderground, service, which_day)
|
165
|
173
|
end
|
166
|
174
|
create_event :payload => weather
|
167
|
175
|
end
|