Class: Slacky::Message

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

Constant Summary collapse

@@decorator =
@@bot = nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, channel, raw) ⇒ Message

Returns a new instance of Message.



15
16
17
18
19
20
21
22
# File 'lib/slacky/message.rb', line 15

def initialize(user, channel, raw)
  @raw = raw
  @user = user
  @channel = channel
  @text = raw.text.strip
  @pieces = @text.split ' '
  self.extend @@decorator if @@decorator
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



3
4
5
# File 'lib/slacky/message.rb', line 3

def channel
  @channel
end

#rawObject (readonly)

Returns the value of attribute raw.



3
4
5
# File 'lib/slacky/message.rb', line 3

def raw
  @raw
end

#textObject (readonly)

Returns the value of attribute text.



3
4
5
# File 'lib/slacky/message.rb', line 3

def text
  @text
end

#userObject (readonly)

Returns the value of attribute user.



3
4
5
# File 'lib/slacky/message.rb', line 3

def user
  @user
end

Class Method Details

.bot=(bot) ⇒ Object



11
12
13
# File 'lib/slacky/message.rb', line 11

def self.bot=(bot)
  @@bot = bot
end

.decorator=(decorator) ⇒ Object



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

def self.decorator=(decorator)
  @@decorator = decorator
end

Instance Method Details

#commandObject



40
41
42
43
44
45
46
# File 'lib/slacky/message.rb', line 40

def command
  if @channel.type == :im
    command? ? downword(1) : downword(0)
  else
    command? ? downword(1) : nil
  end
end

#command?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
# File 'lib/slacky/message.rb', line 32

def command?
  first = downword 0
  return false unless first
  [ @@bot.name, "<@#{@@bot.slack_id.downcase}>" ].any? do |name|
    first == name || first == "#{name}:"
  end
end

#command_argsObject



48
49
50
51
52
# File 'lib/slacky/message.rb', line 48

def command_args
  return nil unless command
  index = @text.downcase.index(command) + command.length
  @text[index..-1].strip
end

#no?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/slacky/message.rb', line 58

def no?
  [ 'n', 'no', 'nope' ].include? @text.downcase
end

#reply(msg) ⇒ Object



24
25
26
# File 'lib/slacky/message.rb', line 24

def reply(msg)
  @@bot.client.message channel: @channel.slack_id, reply_to: @raw.id, text: msg
end

#typingObject



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

def typing
  @@bot.client.typing channel: @channel.slack_id
end

#yes?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/slacky/message.rb', line 54

def yes?
  [ 'y', 'yes', 'yep' ].include? @text.downcase
end