]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/weather.rb
chucknorris: read gzip stream before passing it to YAML.load
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / weather.rb
index cb4617f1a9bd03a6000f9874ac78a60e072fc0ec..7c1336f0d017dfd62655bb2d4d859e7a563bd649 100644 (file)
@@ -15,15 +15,15 @@ require 'rexml/document'
 
 # Wraps NOAA National Weather Service information
 class CurrentConditions
-  @@bot = Irc::Utils.bot
-    def initialize(station)
+    def initialize(station, bot)
         @station = station
+        @bot = bot
         @url = "http://www.nws.noaa.gov/data/current_obs/#{URI.encode @station.upcase}.xml"
         @current_conditions = String.new
     end
     def update
       begin
-        resp = @@bot.httputil.get_response(@url)
+        resp = @bot.httputil.get_response(@url)
         case resp
         when Net::HTTPSuccess
           cc_doc = (REXML::Document.new resp.body).root
@@ -48,16 +48,22 @@ class CurrentConditions
         cc_doc.elements.each do |c|
             cc[c.name.to_sym] = c.text
         end
-        "At #{cc[:observation_time_rfc822]}, the wind was #{cc[:wind_string]} at #{cc[:location]} (#{cc[:station_id]}). The temperature was #{cc[:temperature_string]}#{heat_index_or_wind_chill(cc)}, and the pressure was #{cc[:pressure_string]}. The relative humidity was #{cc[:relative_humidity]}%. Current conditions are #{cc[:weather]} with #{cc[:visibility_mi]}mi visibility."
+        cc[:time] = cc[:observation_time_rfc822]
+        cc[:wind] = cc[:wind_string]
+        cc[:temperature] = cc[:temperature_string]
+        cc[:heatindexorwindchill] = heat_index_or_wind_chill(cc)
+        cc[:pressure] = cc[:pressure_string]
+
+        _("At %{time} the conditions at %{location} (%{station_id}) were %{weather} with a visibility of %{visibility_mi}mi. The wind was %{wind} with %{relative_humidity}%% relative humidity. The temperature was %{temperature}%{heatindexorwindchill}, and the pressure was %{pressure}.") % cc
     end
 private
     def heat_index_or_wind_chill(cc)
         hi = cc[:heat_index_string]
         wc = cc[:windchill_string]
-        if hi != 'NA' then
-            " with a heat index of #{hi}"
-        elsif wc != 'NA' then
-            " with a windchill of #{wc}"
+        if hi and hi != 'NA' then
+            _(" with a heat index of %{hi}") % { :hi => hi }
+        elsif wc and wc != 'NA' then
+            _(" with a windchill of %{wc}") % { :wc => wc }
         else
             ""
         end
@@ -124,7 +130,9 @@ class WeatherPlugin < Plugin
 
     wu_units = String.new
 
-    case (units || @bot.config['weather.units']).to_sym
+    units = @bot.config['weather.units'] unless units
+
+    case units.to_sym
     when :english, :metric
       wu_units = "_#{units}"
     when :both
@@ -153,7 +161,7 @@ class WeatherPlugin < Plugin
     if @nws_cache.has_key?(where) then
         met = @nws_cache[where]
     else
-        met = CurrentConditions.new(where)
+        met = CurrentConditions.new(where, @bot)
     end
     if met
       begin