class Lita::Room

A room in the chat service. Persisted in Redis. @since 4.4.0

Attributes

id[R]

The room's unique ID. @return [String] The room's ID.

metadata[R]

A hash of arbitrary metadata about the room. @return [Hash] The room's metadata.

name[R]

The room's name as displayed in a standard user interface. @return [String] The room's name.

Public Class Methods

create_or_update(id, metadata = {}) click to toggle source

Creates a new room with the given ID, or merges and saves supplied metadata to a room with the given ID. @param id [Integer, String] A unique identifier for the room. @param metadata [Hash] An optional hash of metadata about the room. @option metadata [String] name (id) The display name of the room. @return [Lita::Room] The room.

# File lib/lita/room.rb, line 12
def create_or_update(id, metadata = {})
  existing_room = find_by_id(id)
  metadata = Util.stringify_keys(metadata)
  metadata = existing_room.metadata.merge(metadata) if existing_room
  room = new(id, metadata)
  room.save
  room
end
find_by_id(id) click to toggle source

Finds a room by ID. @param id [Integer, String] The room's unique ID. @return [Lita::Room, nil] The room or nil if no such room is known.

# File lib/lita/room.rb, line 24
def find_by_id(id)
  metadata = redis.hgetall("id:#{id}")
  new(id, metadata) if metadata.key?("name")
end
find_by_name(name) click to toggle source

Finds a room by display name. @param name [String] The room's name. @return [Lita::Room, nil] The room or nil if no such room is known.

# File lib/lita/room.rb, line 32
def find_by_name(name)
  id = redis.get("name:#{name}")
  find_by_id(id) if id
end
fuzzy_find(identifier) click to toggle source

Finds a room by ID or name @param identifier [Integer, String] The room's ID or name. @return [Lita::Room, nil] The room or nil if no room was found.

# File lib/lita/room.rb, line 40
def fuzzy_find(identifier)
  find_by_id(identifier) || find_by_name(identifier)
end
new(id, metadata = {}) click to toggle source

@param id [Integer, String] The room's unique ID. @param metadata [Hash] Arbitrary room metadata. @option metadata [String] name (id) The room's display name.

# File lib/lita/room.rb, line 66
def initialize(id, metadata = {})
  @id = id.to_s
  @metadata = Util.stringify_keys(metadata)
  @name = @metadata["name"] || @id
end
redis() click to toggle source

The Redis::Namespace for room persistence. @return [Redis::Namespace] The Redis connection.

# File lib/lita/room.rb, line 46
def redis
  @redis ||= Redis::Namespace.new("rooms", redis: Lita.redis)
end

Public Instance Methods

==(other) click to toggle source

Compares the room against another room object to determine equality. Rooms are considered equal if they have the same ID. @param other (Lita::Room) The room to compare against. @return [Boolean] True if rooms are equal, false otherwise.

# File lib/lita/room.rb, line 76
def ==(other)
  other.respond_to?(:id) && id == other.id
end
Also aliased as: eql?
eql?(other)
Alias for: ==
hash() click to toggle source

Generates a Fixnum hash value for this user object. Implemented to support equality. @return [Fixnum] The hash value. @see Object#hash

# File lib/lita/room.rb, line 84
def hash
  id.hash
end
save() click to toggle source

Saves the room record to Redis, overwriting any previous data for the current ID. @return [void]

# File lib/lita/room.rb, line 90
def save
  ensure_name_metadata_set

  redis.pipelined do
    redis.hmset("id:#{id}", *metadata.to_a.flatten)
    redis.set("name:#{name}", id)
  end
end

Private Instance Methods

ensure_name_metadata_set() click to toggle source

Ensure the room's metadata contains its name, to ensure their Redis hash contains at least one value. It's not possible to store an empty hash key in Redis.

# File lib/lita/room.rb, line 103
def ensure_name_metadata_set
  room_name = metadata.delete("name")
  metadata["name"] = room_name || id
end
redis() click to toggle source

The Redis connection for room persistence.

# File lib/lita/room.rb, line 109
def redis
  self.class.redis
end