Class: Ircp::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/ircp/message.rb

Constant Summary collapse

CRLF =
"\r\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) {|_self| ... } ⇒ Message

Returns a new instance of Message.

Yields:

  • (_self)

Yield Parameters:

  • _self (Ircp::Message)

    the object that the method was called on



9
10
11
12
13
14
15
16
# File 'lib/ircp/message.rb', line 9

def initialize(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  @raw = options[:raw]
  @prefix = options[:prefix] ? Prefix.new(options[:prefix]) : nil
  @command = options[:command] || args.shift
  @params = args + Array(options[:params])
  yield self if block_given?
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



7
8
9
# File 'lib/ircp/message.rb', line 7

def command
  @command
end

#paramsObject

Returns the value of attribute params.



7
8
9
# File 'lib/ircp/message.rb', line 7

def params
  @params
end

#prefixObject

Returns the value of attribute prefix.



7
8
9
# File 'lib/ircp/message.rb', line 7

def prefix
  @prefix
end

#rawObject

Returns the value of attribute raw.



7
8
9
# File 'lib/ircp/message.rb', line 7

def raw
  @raw
end

Instance Method Details

#inspectObject



18
19
20
21
22
# File 'lib/ircp/message.rb', line 18

def inspect
  variables = instance_variables.map { |name| "#{name}=#{instance_variable_get(name).inspect}" }
  variables.unshift "#{self.class}"
  "<#{variables.join ' '}>"
end

#to_ircObject Also known as: to_s



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ircp/message.rb', line 24

def to_irc
  command = @command.to_s.upcase

  tokens = []
  tokens << ":#{@prefix}" if @prefix
  tokens << command

  new_params = @params.dup
  unless new_params.empty?
    last = new_params.pop.to_s.dup
    last.insert 0, ':' if !last.start_with?(':') && last.include?(' ')
    new_params << last
  end
  tokens += new_params

  msg = tokens.map { |token| token.to_s }.reject { |token| token.empty? }.join(' ')
  msg << CRLF unless msg.end_with?(CRLF)
  msg
end