Class: SlackBot::Message

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, bot) ⇒ Message

Returns a new instance of Message.



8
9
10
11
12
# File 'lib/slack/message.rb', line 8

def initialize(data, bot)
  @data = data
  @bot = bot
  @user = bot.user(data['user'])
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/slack/message.rb', line 34

def method_missing(name, *args)
  # Access data if no args and is valid key, else throw exception
  if args.count == 0 && @data.has_key?(name.to_s)
    @data[name.to_s]
  else
    super(name, args)
  end
end

Instance Attribute Details

#userObject

Returns the value of attribute user.



6
7
8
# File 'lib/slack/message.rb', line 6

def user
  @user
end

Instance Method Details

#[](key) ⇒ Object



26
27
28
# File 'lib/slack/message.rb', line 26

def [](key)
  @data[key]
end

#[]=(key, val) ⇒ Object



30
31
32
# File 'lib/slack/message.rb', line 30

def []=(key, val)
  @data[key] = val
end

#reply(text) ⇒ Object



14
15
16
# File 'lib/slack/message.rb', line 14

def reply(text)
  @bot.post channel, text
end

#to_sObject



18
19
20
# File 'lib/slack/message.rb', line 18

def to_s
  "#{@user.name}: #{@data['text']}"
end