Skip to content
This repository has been archived by the owner on Nov 10, 2022. It is now read-only.

Commit

Permalink
refactor Message
Browse files Browse the repository at this point in the history
  • Loading branch information
vangberg committed Dec 4, 2009
1 parent 9f534f4 commit 8417274
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 29 deletions.
57 changes: 28 additions & 29 deletions lib/isaac/bot.rb
Expand Up @@ -187,53 +187,52 @@ def message(msg)
end

class Message
attr_accessor :raw, :prefix, :server, :nick, :user, :host, :command, :params
attr_accessor :raw,
:prefix, :server, :nick, :user, :host,
:command, :error,
:params

def initialize(msg=nil)
if msg
@raw = msg
parse
end
@raw = msg
parse if msg
end

def numeric_reply?
@numeric_reply
@numeric_reply ||= !!@command.match(/^\d\d\d$/)
end

def parse
match = @raw.match(/(^:(\S+) )?(\S+)(.*)?/)
_, @prefix, @command, @raw_params = match.captures
_, @prefix, @command, raw_params = match.captures

parse_prefix
parse_command
parse_params
raw_params.strip!
if match = raw_params.match(/:(.*)/)
@params = match.pre_match.split(" ")
@params << match[1]
else
@params = raw_params.split(" ")
end
end

private
def parse_prefix
def nick
return unless @prefix
@nick ||= @prefix[/^(\S+)!/, 1]
end

if match = @prefix.match(/(\S+)!(\S+)@(\S+)/)
@nick = match[1]
@user = match[2]
@host = match[3]
else
@server = @prefix
end
def user
return unless @prefix
@user ||= @prefix[/^\S+!(\S+)@/, 1]
end

def parse_command
@numeric_reply = !!@command.match(/^\d\d\d$/)
def host
return unless @prefix
@host ||= @prefix[/@(\S+)$/, 1]
end

def parse_params
@raw_params.strip!
if match = @raw_params.match(/:(.*)/)
@params = match.pre_match.split(" ")
@params << match[1]
else
@params = @raw_params.split(" ")
end
def server
return unless @prefix
return if @prefix.match(/[@!]/)
@server ||= @prefix[/^(\S+)/, 1]
end
end

Expand Down
14 changes: 14 additions & 0 deletions test/test_message.rb
Expand Up @@ -9,12 +9,16 @@ class TestMessage < Test::Unit::TestCase
assert_equal "jeff", msg.nick
assert_equal "spicoli", msg.user
assert_equal "beach.com", msg.host
assert_nil msg.server
end

test "server prefix" do
msg = Message.new(":some.server.com PING")
assert_equal "some.server.com", msg.prefix
assert_equal "some.server.com", msg.server
assert_nil msg.nick
assert_nil msg.user
assert_nil msg.host
end

test "without prefix" do
Expand Down Expand Up @@ -72,4 +76,14 @@ class TestMessage < Test::Unit::TestCase
assert_equal "bar", msg.params[0]
assert_equal "lol :cat", msg.params[1]
end

#test "error" do
#msg = Message.new("200")
#assert_equal false, msg.error?
#assert_nil msg.error

#msg = Message.new("400")
#assert_equal true, msg.error?
#assert_equal 400, msg.error
#end
end

0 comments on commit 8417274

Please sign in to comment.