Skip to content
Browse files

weather and forecast commands

  • Loading branch information...
1 parent a973fda commit 7a12017b60ecfe2c6034ad08ae6b259e44b3d37a @igorw committed Sep 12, 2010
Showing with 67 additions and 1 deletion.
  1. +5 −0 Gemfile
  2. +3 −1 bertiebot.rb
  3. +59 −0 plugins/weather.rb
View
5 Gemfile
@@ -1,4 +1,9 @@
source :gemcutter
gem 'cinch'
+
+# tweet command
gem 'twitter'
+
+# weather command
+gem 'google-weather'
View
4 bertiebot.rb
@@ -8,6 +8,7 @@
require './plugins/quit'
require './plugins/seen'
require './plugins/note'
+require './plugins/weather'
trap("INT") {
puts
@@ -24,7 +25,8 @@
BertieBot::Replace,
BertieBot::Quit,
BertieBot::Seen,
- BertieBot::Note
+ BertieBot::Note,
+ BertieBot::Weather
]
end
end
View
59 plugins/weather.rb
@@ -0,0 +1,59 @@
+# coding: utf-8
+require 'google_weather'
+
+# !weather
+# !forecast
+module BertieBot
+ class Weather
+ include Cinch::Plugin
+
+ match /weather (.+)/
+ match /forecast (.+)/, method: :execute_forecast
+
+ def execute(m, location)
+ begin
+ weather = GoogleWeather.new(location)
+ forecast = weather.forecast_conditions[0]
+ m.reply weather.forecast_information.city + ': ' + pretty_forecast(forecast)
+ rescue Exception
+ m.reply 'Invalid location'
+ end
+ end
+
+ def execute_forecast(m, location)
+ begin
+ weather = GoogleWeather.new(location)
+ m.reply m.user.nick + ': Forecast for ' + weather.forecast_information.city
+ weather.forecast_conditions[0..3].each do |forecast|
+ m.reply pretty_forecast(forecast)
+ end
+ rescue Exception => e
+ m.reply 'Invalid location'
+ raise e
+ end
+ end
+
+ private
+
+ def pretty_forecast(forecast)
+ [
+ forecast.day_of_week,
+ 'Low: ' + pretty_temperature(forecast.low),
+ 'High: ' + pretty_temperature(forecast.high),
+ 'Condition: ' + forecast.condition
+ ].join(' | ')
+ end
+
+ def pretty_temperature(fahrenheit)
+ round_to_one(fahrenheit_to_celsius(fahrenheit)) + '°C (' + fahrenheit.to_s + '°F)'
+ end
+
+ def fahrenheit_to_celsius(fahrenheit)
+ (fahrenheit.to_f - 32.0) / 1.8
+ end
+
+ def round_to_one(number)
+ sprintf('%.1f', number)
+ end
+ end
+end

0 comments on commit 7a12017

Please sign in to comment.
Something went wrong with that request. Please try again.