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
- Pattern =
The arbitrary regex pattern.
/^(?:[:@]([^\s]+) )?([^\s]+)(?: ((?:[^:\s][^\s]* ?)*))?(?: ?:(.*))?$/
Instance Attribute Summary (collapse)
-
- (Symbol, Fixnum) name
The command name.
-
- (Array) params
A list of parameters.
-
- (String) prefix
A hostname or a hostmask.
Class Method Summary (collapse)
-
+ (Command) parse(data)
Parse a line and encapsulate it as a Command.
Instance Method Summary (collapse)
-
- (Object) [](index)
Get a parameter by its index.
-
- (Command) initialize(name, params = [])
constructor
Instantiate a command.
-
- (String, OpenStruct) sender
Get the sender of the command.
-
- (String) to_s
Convert it to an IRC-compliant line.
Constructor Details
- (Command) initialize(name, params = [])
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
- (Symbol, Fixnum) name
The command name.
14 15 16 |
# File 'library/blur/network/command.rb', line 14 def name @name end |
- (Array) params
A list of parameters.
16 17 18 |
# File 'library/blur/network/command.rb', line 16 def params @params end |
- (String) prefix
A hostname or a hostmask.
18 19 20 |
# File 'library/blur/network/command.rb', line 18 def prefix @prefix end |
Class Method Details
+ (Command) parse(data)
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
- (Object) [](index)
Get a parameter by its index.
40 |
# File 'library/blur/network/command.rb', line 40 def [] index; @params[index] end |
- (String, OpenStruct) sender
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 |
- (String) to_s
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 |