]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/forecast.rb
quiz: stop quizzes and timers on cleanup
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / forecast.rb
index 8079072853084b5ed2d43747f6fad788dd8646dd..293c7c057fb0a4c99faaf848b29ab0ee60f1bdbf 100644 (file)
@@ -8,7 +8,7 @@
 
 require 'soap/wsdlDriver'
 # TODO why not use HttpUtil instead of open-uri?
-require 'open-uri' 
+require 'open-uri'
 require 'rexml/document'
 require 'erb'
 
@@ -48,7 +48,7 @@ class Forecast
         return parse(retrieve),Time.new
     end
 private
-    def retrieve 
+    def retrieve
         forecast = @forecaster.NDFDgenByDay(
             @lat,@long,Time.now.strftime("%Y-%m-%d"),2,"24 hourly")
         (REXML::Document.new(forecast)).root
@@ -105,43 +105,42 @@ class ForecastPlugin < Plugin
             end
         end
     end
-    
+
     def get_forecast(m,loc)
-      Thread.new {
-        begin
-          @cache_mutex.synchronize do
-            if @forecast_cache.has_key?(loc) and
-                Time.new - @forecast_cache[loc][:date] < 3600
-                forecast = @forecast_cache[loc][:forecast]
-                if forecast
-                  m.reply forecast
-                  Thread.exit
-                end
+      begin
+        @cache_mutex.synchronize do
+          if @forecast_cache.has_key?(loc) and
+            Time.new - @forecast_cache[loc][:date] < 3600
+            forecast = @forecast_cache[loc][:forecast]
+            if forecast
+              m.reply forecast
+              return
             end
           end
-          begin
-            l = LatLong.new
-            f = Forecast.new(*l.get_lat_long(loc))
-            forecast,forecast_date = f.forecast
-          rescue => err
-            m.reply err
-          end
-          if forecast
-            m.reply forecast
-            @cache_mutex.synchronize do
-              @forecast_cache[loc] = {
-                :forecast => forecast,
-                :date => forecast_date
-              }
-            end
-          else
-            m.reply "Couldn't find forecast for #{loc}"
+        end
+        begin
+          l = LatLong.new
+          f = Forecast.new(*l.get_lat_long(loc))
+          forecast,forecast_date = f.forecast
+        rescue => err
+          m.reply err
+        end
+        if forecast
+          m.reply forecast
+          @cache_mutex.synchronize do
+            @forecast_cache[loc] = {
+              :forecast => forecast,
+              :date => forecast_date
+            }
           end
-        rescue => e
-          m.reply "ERROR: #{e}"
+        else
+          m.reply "Couldn't find forecast for #{loc}"
         end
-      }
+      rescue => e
+        m.reply "ERROR: #{e}"
+      end
     end
 end
 plugin = ForecastPlugin.new
-plugin.map 'forecast *location', :defaults => {:location => false}
+plugin.map 'forecast *location',
+  :defaults => {:location => false}, :thread => true