Class: Lita::Source

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

Overview

A wrapper object representing the source of an incoming message (either the user who sent it, the room they sent it from, or both). If a room is set, the message is from a group chat room. If no room is set, the message is assumed to be a private message, though Source objects can be explicitly marked as private messages. Source objects are also used as “target” objects when sending an outgoing message or performing another operation on a user or a room.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user: nil, room: nil, private_message: false) ⇒ Source

Returns a new instance of Source.

Parameters:

  • user (Lita::User) (defaults to: nil)

    The user who sent the message or should receive the outgoing message.

  • room (Lita::Room, String) (defaults to: nil)

    A string or Room uniquely identifying the room the user sent the message from, or the room where a reply should go. The format of this string (or the ID of the Room object) will differ depending on the chat service.

  • private_message (Boolean) (defaults to: false)

    A flag indicating whether or not the message was sent privately.

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/lita/source.rb', line 35

def initialize(user: nil, room: nil, private_message: false)
  @user = user

  case room
  when String
    @room = room
    @room_object = Room.new(room)
  when Room
    @room = room.id
    @room_object = room
  end

  @private_message = private_message

  raise ArgumentError, I18n.t("lita.source.user_or_room_required") if user.nil? && room.nil?

  @private_message = true if room.nil?
end

Instance Attribute Details

#private_messageBoolean (readonly) Also known as: private_message?

A flag indicating that a message was sent to the robot privately.

Returns:

  • (Boolean)

    The boolean flag.



12
13
14
# File 'lib/lita/source.rb', line 12

def private_message
  @private_message
end

#roomString, NilClass (readonly)

The room the message came from or should be sent to, as a string.

Returns:

  • (String, NilClass)

    A string uniquely identifying the room.



17
18
19
# File 'lib/lita/source.rb', line 17

def room
  @room
end

#room_objectLita::Room, NilClass (readonly)

The room the message came from or should be sent to, as a Room object.

Returns:

Since:

  • 4.4.0



22
23
24
# File 'lib/lita/source.rb', line 22

def room_object
  @room_object
end

#userLita::User, NilClass (readonly)

The user who sent the message or should receive the outgoing message.

Returns:



26
27
28
# File 'lib/lita/source.rb', line 26

def user
  @user
end

Instance Method Details

#private_message!void

This method returns an undefined value.

Destructively marks the source as a private message, meaning an incoming message was sent to the robot privately, or an outgoing message should be sent to a user privately.



58
59
60
# File 'lib/lita/source.rb', line 58

def private_message!
  @private_message = true
end