]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
grouphug: refactor confession retrieval
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Sun, 8 Feb 2009 13:54:31 +0000 (14:54 +0100)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Sun, 8 Feb 2009 13:54:31 +0000 (14:54 +0100)
Turn the confession extraction into its own method, and use it both for
specific and random confession retrieval.

data/rbot/plugins/grouphug.rb

index 45083e1c02bd1d5ca4f3f56fc46eb679a411f4ea..fa35803ce19b296c29a9004343304b9e9fd68a78 100644 (file)
@@ -46,6 +46,15 @@ class GrouphugPlugin < Plugin
     end
   end
 
+  def get_confessions(html)
+    return [] unless html
+    start = html.index(START)
+    res = html[start, html.length - start].scan(REG)
+    return [] unless res
+    return res.map { |quote|
+      quote[0].ircify_html
+    }
+  end
 
   def confess(m, params)
     opts = { :cache => false }
@@ -56,22 +65,20 @@ class GrouphugPlugin < Plugin
         path = "confessions/#{params[:num]}"
         opts.delete(:cache)
         data = @bot.httputil.get("http://grouphug.us/#{path}", opts)
-        start = data.index(START)
-        res = data[start, data.length - start].scan(REG)
-        confession = res.first[0].ircify_html
-        confession = "no confession ##{params[:num]} found" if confession.empty? and params[:num]
-        m.reply confession
+        confessions = get_confessions(data)
+        if confessions.length > 1
+          warn "more than one confession found!"
+          warn confessions
+        end
+        confessions << "no confession ##{params[:num]} found" if confessions.empty?
+        m.reply confessions.first
       else # Cache random confessions
         if @confessions.empty?
           data = @bot.httputil.get("http://grouphug.us/#{path}", opts)
-          start = data.index(START)
-          res = data[start, data.length - start].scan(REG)
-          res.each do |quote|
-            @confessions << quote[0].ircify_html
-          end
+          @confessions.replace get_confessions(data)
         end
-        confession = @confessions.pop
-        m.reply confession
+        @confessions << "no confessions found!" if @confessions.empty?
+        m.reply @confessions.pop
       end
     rescue Exception => e
       error e