Class: Blur::Network::Command
- Inherits:
-
Object
- Object
- Blur::Network::Command
- Defined in:
- library/blur/network/command.rb
Overview
The Command
class is used for encapsulating the command-lines.
Blur is using regular-expression for parsing, this is to be replaced with a more native way of parsing, making it much, much easier on the processor.
Constant Summary collapse
- Pattern =
The arbitrary regex pattern.
/^(?:[:@]([^\s]+) )?([^\s]+)(?: ((?:[^:\s][^\s]* ?)*))?(?: ?:(.*))?$/
Instance Attribute Summary collapse
-
#name ⇒ Symbol, Fixnum
The command name.
-
#params ⇒ Array
A list of parameters.
-
#prefix ⇒ String
A hostname or a hostmask.
Class Method Summary collapse
-
.parse(data) ⇒ Command
Parse a line and encapsulate it as a Command.
Instance Method Summary collapse
-
#[](index) ⇒ Object
Get a parameter by its
index
. -
#initialize(name, params = []) ⇒ Command
constructor
Instantiate a command.
-
#sender ⇒ String, OpenStruct
Get the sender of the command.
-
#to_s ⇒ String
Convert it to an IRC-compliant line.
Constructor Details
#initialize(name, params = []) ⇒ Command
Instantiate a command.
45 46 47 |
# File 'library/blur/network/command.rb', line 45 def initialize name, params = [] @name, @params = name, params end |
Instance Attribute Details
#name ⇒ Symbol, Fixnum
Returns the command name.
14 15 16 |
# File 'library/blur/network/command.rb', line 14 def name @name end |
#params ⇒ Array
Returns a list of parameters.
16 17 18 |
# File 'library/blur/network/command.rb', line 16 def params @params end |
#prefix ⇒ String
Returns a hostname or a hostmask.
18 19 20 |
# File 'library/blur/network/command.rb', line 18 def prefix @prefix end |
Class Method Details
.parse(data) ⇒ Command
Parse a line and encapsulate it as a Command.
29 30 31 32 33 34 35 36 37 |
# File 'library/blur/network/command.rb', line 29 def self.parse data match = data.strip.match Pattern prefix, name, args, extra = match.captures params = extra ? args.split << extra : args.split new(name, params).tap do |this| this.prefix = prefix end end |
Instance Method Details
#[](index) ⇒ Object
Get a parameter by its index
.
40 |
# File 'library/blur/network/command.rb', line 40 def [] index; @params[index] end |
#sender ⇒ String, OpenStruct
the return value is a string if it’s a hostname, and an openstruct with the attributes #nickname, #username and #hostname if it’s a hostmask.
Get the sender of the command.
56 57 58 59 60 61 62 63 64 |
# File 'library/blur/network/command.rb', line 56 def sender return @sender if @sender if prefix =~ /^(\S+)!(\S+)@(\S+)$/ @sender = OpenStruct.new nickname: $1, username: $2, hostname: $3 else @sender = prefix end end |
#to_s ⇒ String
Convert it to an IRC-compliant line.
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'library/blur/network/command.rb', line 69 def to_s String.new.tap do |line| line << "#{prefix} " if prefix line << name.to_s params.each_with_index do |param, index| line << ' ' line << ?: if index == params.length - 1 and param =~ /[ :]/ line << param.to_s end end end |