Class: LibertyBot::Message

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix, command, *params) ⇒ Message

Returns a new instance of Message.



30
31
32
33
34
# File 'lib/libertybot/message.rb', line 30

def initialize(prefix, command, *params)
  @prefix = Prefix.new(prefix.to_s)
  @command = command
  @params = params.flatten
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



28
29
30
# File 'lib/libertybot/message.rb', line 28

def command
  @command
end

#paramsObject (readonly)

Returns the value of attribute params.



28
29
30
# File 'lib/libertybot/message.rb', line 28

def params
  @params
end

#prefixObject (readonly)

Returns the value of attribute prefix.



28
29
30
# File 'lib/libertybot/message.rb', line 28

def prefix
  @prefix
end

Class Method Details

.parse(message) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/libertybot/message.rb', line 36

def self.parse(message)
  prefix, command, params = nil, nil, []
  components = message.split(" ")
  
  if components.first[0] == ":"
    prefix = components.first[1..-1]
    components.shift
  end
  
  command = components.first
  components.shift
  
  components.each do |component|
    if params.last.to_s[0] == ":"
      params << params.pop + " " + component
      next
    end
    
    params << component
  end
  
  params << params.pop[1..-1] if params.last[0] == ":"
  
  self.new(prefix, command, params)
end

Instance Method Details

#to_sObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/libertybot/message.rb', line 62

def to_s
  str = ""
  
  str << ":#{@prefix} " unless @prefix.empty?
  str << @command
  
  @params.each do |param|
    str << " "
    str << ":" if param.index " "
    str << param
  end
  
  str << "\r\n"
  
  str
end