Class: Twitch::Bot::IrcMessage

Inherits:
Object
  • Object
show all
Defined in:
lib/twitch/bot/irc_message.rb

Overview

This class splits an IRC message into its basic parts. see ircv3.net/specs/extensions/message-tags.html#format

rubocop:disable Layout/LineLength <message> ::= [‘@’ <tags> <SPACE>] [‘:’ <prefix> <SPACE> ] <command> <params> <crlf> rubocop:enable Layout/LineLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(msg) ⇒ IrcMessage

Returns a new instance of IrcMessage.



102
103
104
105
106
107
108
109
# File 'lib/twitch/bot/irc_message.rb', line 102

def initialize(msg)
  raw_tags, @prefix, @command, raw_params = msg.match(
    /^(?:@(\S+) )?(?::(\S+) )?(\S+)(.*)/,
  ).captures

  @tags = IrcMessageTags.new(raw_tags)
  @params = IrcMessageParams.new(raw_params).params
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



100
101
102
# File 'lib/twitch/bot/irc_message.rb', line 100

def command
  @command
end

#paramsObject (readonly)

Returns the value of attribute params.



100
101
102
# File 'lib/twitch/bot/irc_message.rb', line 100

def params
  @params
end

#prefixObject (readonly)

Returns the value of attribute prefix.



100
101
102
# File 'lib/twitch/bot/irc_message.rb', line 100

def prefix
  @prefix
end

#tagsObject (readonly)

Returns the value of attribute tags.



100
101
102
# File 'lib/twitch/bot/irc_message.rb', line 100

def tags
  @tags
end

Instance Method Details

#channelObject

Not really a :reek:NilCheck



138
139
140
# File 'lib/twitch/bot/irc_message.rb', line 138

def channel
  params.detect { |param| param.start_with?("#") }&.delete("#")
end

#errorObject



111
112
113
# File 'lib/twitch/bot/irc_message.rb', line 111

def error
  command[/[45]\d\d/] ? command.to_i : 0
end

#error?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/twitch/bot/irc_message.rb', line 115

def error?
  error.positive?
end

#targetObject



119
120
121
# File 'lib/twitch/bot/irc_message.rb', line 119

def target
  channel || user
end

#textObject



123
124
125
126
127
128
129
# File 'lib/twitch/bot/irc_message.rb', line 123

def text
  if error?
    error.to_s
  else
    params.last
  end
end

#userObject



131
132
133
134
135
# File 'lib/twitch/bot/irc_message.rb', line 131

def user
  return unless prefix

  prefix[/^(\S+)!/, 1]
end