Permalink
Browse files

More refactorings (and this should be enough).

  * Created an Events module to document all the events that the bot
    can handle.
  * Created a Commands module to document all the commands that the bot
    is able to perform
  * Daemon should now be subclassed in order to be used.
  • Loading branch information...
1 parent 6938514 commit 38c0cd60a094ebc27733801695fa11c0f58780aa @david committed Jun 4, 2008
Showing with 41 additions and 32 deletions.
  1. +3 −1 lib/minibot.rb
  2. +8 −0 lib/minibot/commands.rb
  3. +6 −15 lib/minibot/daemon.rb
  4. +21 −0 lib/minibot/events.rb
  5. +3 −16 spec/daemon_spec.rb
View
@@ -1 +1,3 @@
-require File.join(File.dirname(__FILE__), 'minibot', 'daemon.rb')
+require File.join(File.dirname(__FILE__), 'minibot', 'events')
+require File.join(File.dirname(__FILE__), 'minibot', 'commands')
+require File.join(File.dirname(__FILE__), 'minibot', 'daemon')
@@ -0,0 +1,8 @@
+module MiniBot
+ module Commands
+ def join(channel)
+ write "JOIN #{channel}"
+ end
+
+ end
+end
View
@@ -2,6 +2,9 @@
module MiniBot
class Daemon
+ include Events
+ include Commands
+
DEFAULTS = {
:join => [],
:port => 6667
@@ -17,14 +20,6 @@ def run
end
end
- def event(sym, *args, &block)
- @event_handlers[sym] << block
- end
-
- def join(channel)
- write "JOIN #{channel}"
- end
-
private
def authenticate(nick, username, realname)
@@ -55,16 +50,12 @@ def write(str)
def dispatch(command)
if match = (/:(\w+)!.+ INVITE \w+ :(#\w+)/.match command)
- handle_event :invite, match[2], match[1]
+ send :invited, match[2], match[1]
elsif match = (/:(\w+)!.+ PRIVMSG (#\w+) :(.+)/.match command)
- handle_event :message, match[2], match[1], match[3]
+ send :message, match[2], match[1], match[3]
else
- handle_event :default, command
+ send :default, command
end
end
-
- def handle_event(event, *args)
- @event_handlers[event].each { |handler| handler.call *args }
- end
end
end
View
@@ -0,0 +1,21 @@
+module MiniBot
+ module Events
+ def message(channel, user, message)
+ end
+
+ def message_to_self(channel, user, message)
+ end
+
+ def private_message(user, message)
+ end
+
+ def user_joined(channel, user)
+ end
+
+ def user_parted(channel, user)
+ end
+
+ def invited(channel, user)
+ end
+ end
+end
View
@@ -16,7 +16,6 @@ def daemon(options = {})
d = daemon
d.send(:connect, 'irc.freenode.net', 6667)
- puts d.inspect
d.instance_variable_get("@socket").should == socket
end
@@ -39,25 +38,13 @@ def daemon(options = {})
it "should dispatch invites" do
d = daemon
-
- tester = mock("tester")
- tester.should_receive(:call).with("#ior3k", "ior3k")
- d.event :invite do |channel, user|
- tester.call channel, user
- end
-
+ d.should_receive(:invited).with('#ior3k', 'ior3k')
d.send :dispatch, ":ior3k!n=david@89.152.220.123 INVITE nnn :#ior3k"
end
- it "should dispatch messages, unconditionally" do
+ it "should dispatch messages" do
d = daemon
-
- tester = mock("tester")
- tester.should_receive(:call).with("#ior3k", "ior3k", "This is a test message!")
- d.event :message do |channel, user, message|
- tester.call channel, user, message
- end
-
+ d.should_receive(:message).with('#ior3k', 'ior3k', 'This is a test message!')
d.send :dispatch, ":ior3k!n=david@89.152.220.123 PRIVMSG #ior3k :This is a test message!"
end
end

0 comments on commit 38c0cd6

Please sign in to comment.