Class: Robut::Plugin::Weather
- Inherits:
-
Object
- Object
- Robut::Plugin::Weather
show all
- Includes:
- Robut::Plugin
- Defined in:
- lib/robut/plugin/weather.rb
Overview
What’s the current weather forecast?
Class Attribute Summary collapse
#connection, #private_sender, #reply_to
Instance Method Summary
collapse
#at_nick, #fake_message, included, #initialize, #nick, #reply, #sent_to_me?, #store, #without_nick, #words
Class Attribute Details
.default_location ⇒ Object
Returns the value of attribute default_location.
9
10
11
|
# File 'lib/robut/plugin/weather.rb', line 9
def default_location
@default_location
end
|
Instance Method Details
#bad_location?(location = "") ⇒ Boolean
122
123
124
|
# File 'lib/robut/plugin/weather.rb', line 122
def bad_location?(location = "")
weather_data(location).search("forecast_information").empty?
end
|
#current_conditions(location) ⇒ Object
93
94
95
96
97
98
99
|
# File 'lib/robut/plugin/weather.rb', line 93
def current_conditions(location)
doc = weather_data(location)
condition = doc.search("current_conditions condition").first["data"]
temperature = doc.search("current_conditions temp_f").first["data"]
normalized_location = doc.search("forecast_information city").first["data"]
"Weather for #{normalized_location}: #{condition}, #{temperature}F"
end
|
#forecast(location, day_of_week) ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/robut/plugin/weather.rb', line 101
def forecast(location, day_of_week)
doc = weather_data(location)
forecast = doc.search("forecast_conditions").detect{|el| c = el.children.detect{|c| c.name == "day_of_week"}; c && c["data"] == day_of_week}
return "Can't find a forecast for #{day_of_week}" if forecast.nil?
condition = forecast.children.detect{|c| c.name == "condition"}["data"]
high = forecast.children.detect{|c| c.name == "high"}["data"]
low = forecast.children.detect{|c| c.name == "low"}["data"]
normalized_location = doc.search("forecast_information city").first["data"]
"Forecast for #{normalized_location} on #{day_of_week}: #{condition}, High: #{high}F, Low: #{low}F"
end
|
#handle(time, sender_nick, message) ⇒ Object
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
57
58
59
|
# File 'lib/robut/plugin/weather.rb', line 22
def handle(time, sender_nick, message)
return unless message[message.length - 1, 1] == "?"
message = message[0..message.length - 2]
words = words(message)
i = words.index("weather")
return if i.nil?
location = i.zero? ? self.class.default_location : words[0..i-1].join(" ")
if location.nil?
reply "I don't have a default location!"
return
end
day_of_week = nil
day_string = words[i+1..-1].join(" ").downcase
if day_string != ""
day_of_week = parse_day(day_string)
if day_of_week.nil?
reply "I don't recognize the date: \"#{day_string}\""
return
end
end
if bad_location?(location)
reply "I don't recognize the location: \"#{location}\""
return
end
if day_of_week
reply forecast(location, day_of_week)
else
reply current_conditions(location)
end
end
|
#parse_day(day) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/robut/plugin/weather.rb', line 61
def parse_day(day)
day_map = {
"monday" => "Mon",
"mon" => "Mon",
"tuesday" => "Tue",
"tue" => "Tue",
"tues" => "Tue",
"wed" => "Wed",
"wednesday" => "Wed",
"thurs" => "Thu",
"thu" => "Thu",
"thursday" => "Thu",
"friday" => "Fri",
"fri" => "Fri",
"saturday" => "Sat",
"sat" => "Sat",
"sunday" => "Sun",
"sun" => "Sun",
}
if day_map.has_key?(day)
return day_map[day]
end
if day == "tomorrow"
return (Time.now + 60*60*24).strftime("%a")
end
if day == "today"
return Time.now.strftime("%a")
end
end
|
#usage ⇒ Object
Returns a description of how to use this plugin
13
14
15
16
17
18
19
20
|
# File 'lib/robut/plugin/weather.rb', line 13
def usage
[
"#{at_nick} weather - returns the weather in the default location for today",
"#{at_nick} weather tomorrow - returns the weather in the default location for tomorrow",
"#{at_nick} weather <location> - returns the weather for <location> today",
"#{at_nick} weather <location> Tuesday - returns the weather for <location> Tuesday"
]
end
|
#weather_data(location = "") ⇒ Object
113
114
115
116
117
118
119
120
|
# File 'lib/robut/plugin/weather.rb', line 113
def weather_data(location = "")
@weather_data ||= {}
@weather_data[location] ||= begin
url = "http://www.google.com/ig/api?weather=#{URI.escape(location)}"
Nokogiri::XML(open(url))
end
@weather_data[location]
end
|