From 14f78ebf5a06d0bb09298cbc34ab56bac55353d7 Mon Sep 17 00:00:00 2001 From: ichverstehe Date: Mon, 6 Oct 2008 18:48:22 +0200 Subject: [PATCH] minor refactoring --- lib/isaac.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/isaac.rb b/lib/isaac.rb index 8d0214b..cadcad3 100644 --- a/lib/isaac.rb +++ b/lib/isaac.rb @@ -72,12 +72,15 @@ def execute(params={}, &block) #:nodoc: end private + def event(type, matcher) + @events[type].detect {|e| matcher =~ e.match} + end + def connect @irc = TCPSocket.open(@config.server, @config.port) @queue = Queue.new(@irc) @queue << "NICK #{@config.nick}" @queue << "USER foobar twitthost twittserv :My Name" - # Stupid line. Invoke on :connect, if it exists. Etc. etc. @queue << @events[:connect].first.invoke if @events[:connect].first while line = @irc.gets handle line @@ -94,13 +97,13 @@ def handle(line) channel = $2 message = $3 type = channel.match(/^#/) ? :channel : :private - if event = @events[type].detect {|e| message =~ e.match} + if event = event(type, message) @queue << event.invoke(:nick => nick, :channel => channel, :message => message) end when /^:\S+ ([4-5]\d\d) \S+ (\S+)/ error = $1 nick = channel = $2 - if event = @events[:error].detect {|e| error == e.match.to_s } + if event = event(:error, error) @queue << event.invoke(:nick => nick, :channel => channel) end when /^PING (\S+)/