Skip to content

Commit

Permalink
experimental refactoring of bz handling
Browse files Browse the repository at this point in the history
  • Loading branch information
willb committed Dec 20, 2011
1 parent 3f3b431 commit 1867f94
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions lib/bzbot/bot.rb
Expand Up @@ -27,11 +27,11 @@
module Bzbot module Bzbot
module Bot module Bot
def add_simple_handler(bot,pattern,template) def add_simple_handler(bot,pattern,template)
add_query_handler(pattern, template)
bot.on :channel, pattern do bot.on :channel, pattern do
record_log record_log
increment_counter(:queries) increment_counter(:queries)
the_message = ERB.new(template).result(self.send(:binding)) process_query
msg self.channel, the_message.to_s
end end
end end


Expand Down Expand Up @@ -79,8 +79,7 @@ def app
on :channel, /(bz|BZ)( |)([0-9]+)/ do on :channel, /(bz|BZ)( |)([0-9]+)/ do
record_log record_log
increment_counter(:queries) increment_counter(:queries)

process_query
msg channel, "#{nick}: #{app.bzbot_bz_url}#{match[2]}"
end end


on :channel, /^bzbot[:,]{0,1}\s*help$/i do on :channel, /^bzbot[:,]{0,1}\s*help$/i do
Expand Down Expand Up @@ -202,6 +201,27 @@ def app
end end


helpers do 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 def record_log
LogRecord.create(:nick=>nick, :channel=>channel, :message=>message, :happened=>Time.now) LogRecord.create(:nick=>nick, :channel=>channel, :message=>message, :happened=>Time.now)
end end
Expand Down

0 comments on commit 1867f94

Please sign in to comment.