Permalink
Browse files

experimental refactoring of bz handling

  • Loading branch information...
1 parent 3f3b431 commit 1867f94c7150a06a4ede4d985fe0f3f5a3f445cb @willb committed Dec 20, 2011
Showing with 24 additions and 4 deletions.
  1. +24 −4 lib/bzbot/bot.rb
View
@@ -27,11 +27,11 @@
module Bzbot
module Bot
def add_simple_handler(bot,pattern,template)
+ add_query_handler(pattern, template)
bot.on :channel, pattern do
record_log
increment_counter(:queries)
- the_message = ERB.new(template).result(self.send(:binding))
- msg self.channel, the_message.to_s
+ process_query
end
end
@@ -79,8 +79,7 @@ def app
on :channel, /(bz|BZ)( |)([0-9]+)/ do
record_log
increment_counter(:queries)
-
- msg channel, "#{nick}: #{app.bzbot_bz_url}#{match[2]}"
+ process_query
end
on :channel, /^bzbot[:,]{0,1}\s*help$/i do
@@ -202,6 +201,27 @@ def app
end
helpers do
+ def query_handlers
+ @qhs = {}
+ end
+
+ def add_query_handler(pattern, template)
+ @qhs[pattern] = template
+ end
+
+ def process_query
+ message.scan(/(bz|BZ)( |)([0-9]+)/) do |bug|
+ msg channel, "#{nick}: #{app.bzbot_bz_url}#{bug[2]}"
+ end
+
+ @qhs.each do |pattern, template|
+ message.scan(pattern) do |match|
+ the_message = ERB.new(template).result(self.send(:binding))
+ msg self.channel, the_message.to_s
+ end
+ end
+ end
+
def record_log
LogRecord.create(:nick=>nick, :channel=>channel, :message=>message, :happened=>Time.now)
end

0 comments on commit 1867f94

Please sign in to comment.