lib/lita/robot.rb in lita-4.4.2 vs lib/lita/robot.rb in lita-4.4.3
- old
+ new
@@ -78,27 +78,46 @@
rescue Interrupt
shut_down
end
# Makes the robot join a room with the specified ID.
- # @param room_id [String] The ID of the room.
+ # @param room [Room, String] The room to join, as a {Lita::Room} object or a string identifier.
# @return [void]
# @since 3.0.0
- def join(room_id)
- Lita.redis.sadd("persisted_rooms", room_id)
- adapter.join(room_id)
+ def join(room)
+ room_object = find_room(room)
+
+ if room_object
+ Lita.redis.sadd("persisted_rooms", room_object.id)
+ adapter.join(room_object.id)
+ else
+ adapter.join(room)
+ end
end
# Makes the robot part from the room with the specified ID.
- # @param room_id [String] The ID of the room.
+ # @param room [Room, String] The room to leave, as a {Lita::Room} object or a string identifier.
# @return [void]
# @since 3.0.0
- def part(room_id)
- Lita.redis.srem("persisted_rooms", room_id)
- adapter.part(room_id)
+ def part(room)
+ room_object = find_room(room)
+
+ if room_object
+ Lita.redis.srem("persisted_rooms", room_object.id)
+ adapter.part(room_object.id)
+ else
+ adapter.part(room)
+ end
end
+ # A list of room IDs the robot should join on boot.
+ # @return [Array<String>] An array of room IDs.
+ # @since 4.4.2
+ def persisted_rooms
+ Lita.redis.smembers("persisted_rooms").sort
+ end
+
# Sends one or more messages to a user or room.
# @param target [Lita::Source] The user or room to send to. If the Source
# has a room, it will choose the room. Otherwise, it will send to the
# user.
# @param strings [String, Array<String>] One or more strings to send.
@@ -160,19 +179,24 @@
handler.trigger(self, event_name, payload)
end
end
- # A list of room IDs the robot should join.
- def persisted_rooms
- Lita.redis.smembers("persisted_rooms").sort
- end
-
private
# Loads and caches the adapter on first access.
def adapter
@adapter ||= load_adapter
+ end
+
+ # Ensure the argument is a Lita::Room.
+ def find_room(room_or_identifier)
+ case room_or_identifier
+ when Room
+ room_or_identifier
+ else
+ Room.fuzzy_find(room_or_identifier)
+ end
end
# Loads the selected adapter.
def load_adapter
adapter_name = config.robot.adapter