Class: SugiliteIRC::Message

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

Overview

Parses standard IRC messages

Direct Known Subclasses

TwitchMessage

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Message

Initalizes message



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/sugiliteIRC.rb', line 43

def initialize(data)
  @raw = data
  @tags = {}
  @parameters = []
  @prefix = nil
  data = data.split(' ')
  data2 = data.shift
  if data2[0] == '@'
    data2[1..-1].split(';').each { |r_tag| s_tag = r_tag.split('=', 2); @tags[s_tag[0]] = s_tag[1] }
    data2 = data.shift
  end
  if data2[0] == ':'
    @prefix = Prefix.new(data2)
    data2 = data.shift
  end
  @command = data2
  loop do
    if data[0].nil?
      break
    elsif data[0][0] == ':'
      @parameters.push(data.join(' ')[1..-1])
      break
    elsif data.length == 1
      @parameters.push(data[0])
      break
    else
      @parameters.push(data.shift)
    end
  end
end

Instance Attribute Details

#commandObject (readonly)

Message commands (i.e. PING and PRIVMSG)



41
42
43
# File 'lib/sugiliteIRC.rb', line 41

def command
  @command
end

#parametersObject (readonly)

Message parameters



37
38
39
# File 'lib/sugiliteIRC.rb', line 37

def parameters
  @parameters
end

#prefixObject (readonly)

Message prefix



39
40
41
# File 'lib/sugiliteIRC.rb', line 39

def prefix
  @prefix
end

#rawObject (readonly)

Raw message



33
34
35
# File 'lib/sugiliteIRC.rb', line 33

def raw
  @raw
end

#tagsObject (readonly)

Message tags



35
36
37
# File 'lib/sugiliteIRC.rb', line 35

def tags
  @tags
end

Instance Method Details

#channelObject

Gets Channel from irc data.



80
81
82
83
84
85
86
87
# File 'lib/sugiliteIRC.rb', line 80

def channel
  case @command
    when 'PRIVMSG', 'NOTICE', 'HOSTTARGET', 'ROOMSTATE', 'USERNOTICE', 'USERSTATE'
      @parameters[0]
    else
      nil
  end
end

#messageObject

Gets message contents as a string.



100
101
102
103
104
105
106
107
# File 'lib/sugiliteIRC.rb', line 100

def message
  case @command
    when 'PRIVMSG', 'NOTICE'
      @parameters[1]
    else
      nil
  end
end

#senderObject

Gets the sender.



90
91
92
93
94
95
96
97
# File 'lib/sugiliteIRC.rb', line 90

def sender
  case @command
    when 'PRIVMSG', 'NOTICE'
      @prefix.realname
    else
      nil
  end
end

#to_sObject

Dumps from JSON object to String.



75
76
77
# File 'lib/sugiliteIRC.rb', line 75

def to_s
  Oj.dump self, indent: 2
end