Class: Lita::Room

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

Overview

A room in the chat service. Persisted in Redis.

Since:

  • 4.4.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, metadata = {}) ⇒ Room

Returns a new instance of Room.

Parameters:

  • id (Integer, String)

    The room’s unique ID.

  • metadata (Hash) (defaults to: {})

    Arbitrary room metadata.

Options Hash (metadata):

  • name (String) — default: id

    The room’s display name.

Since:

  • 4.4.0



66
67
68
69
70
# File 'lib/lita/room.rb', line 66

def initialize(id,  = {})
  @id = id.to_s
  @metadata = Util.stringify_keys()
  @name = @metadata["name"] || @id
end

Instance Attribute Details

#idString (readonly)

The room’s unique ID.

Returns:

  • (String)

    The room’s ID.

Since:

  • 4.4.0



53
54
55
# File 'lib/lita/room.rb', line 53

def id
  @id
end

#metadataHash (readonly)

A hash of arbitrary metadata about the room.

Returns:

  • (Hash)

    The room’s metadata.

Since:

  • 4.4.0



57
58
59
# File 'lib/lita/room.rb', line 57

def 
  @metadata
end

#nameString (readonly)

The room’s name as displayed in a standard user interface.

Returns:

  • (String)

    The room’s name.

Since:

  • 4.4.0



61
62
63
# File 'lib/lita/room.rb', line 61

def name
  @name
end

Class Method Details

.create_or_update(id, metadata = {}) ⇒ Lita::Room

Creates a new room with the given ID, or merges and saves supplied metadata to a room with the given ID.

Parameters:

  • id (Integer, String)

    A unique identifier for the room.

  • metadata (Hash) (defaults to: {})

    An optional hash of metadata about the room.

Options Hash (metadata):

  • name (String) — default: id

    The display name of the room.

Returns:

Since:

  • 4.4.0



12
13
14
15
16
17
18
19
# File 'lib/lita/room.rb', line 12

def create_or_update(id,  = {})
  existing_room = find_by_id(id)
   = Util.stringify_keys()
   = existing_room..merge() if existing_room
  room = new(id, )
  room.save
  room
end

.find_by_id(id) ⇒ Lita::Room?

Finds a room by ID.

Parameters:

  • id (Integer, String)

    The room’s unique ID.

Returns:

  • (Lita::Room, nil)

    The room or nil if no such room is known.

Since:

  • 4.4.0



24
25
26
27
# File 'lib/lita/room.rb', line 24

def find_by_id(id)
   = redis.hgetall("id:#{id}")
  new(id, ) if .key?("name")
end

.find_by_name(name) ⇒ Lita::Room?

Finds a room by display name.

Parameters:

  • name (String)

    The room’s name.

Returns:

  • (Lita::Room, nil)

    The room or nil if no such room is known.

Since:

  • 4.4.0



32
33
34
35
# 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) ⇒ Lita::Room?

Finds a room by ID or name

Parameters:

  • identifier (Integer, String)

    The room’s ID or name.

Returns:

  • (Lita::Room, nil)

    The room or nil if no room was found.

Since:

  • 4.4.0



40
41
42
# File 'lib/lita/room.rb', line 40

def fuzzy_find(identifier)
  find_by_id(identifier) || find_by_name(identifier)
end

.redisRedis::Namespace

The Redis::Namespace for room persistence.

Returns:

  • (Redis::Namespace)

    The Redis connection.

Since:

  • 4.4.0



46
47
48
# File 'lib/lita/room.rb', line 46

def redis
  @redis ||= Redis::Namespace.new("rooms", redis: Lita.redis)
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Compares the room against another room object to determine equality. Rooms are considered equal if they have the same ID.

Parameters:

  • other (Lita::Room)

    The room to compare against.

Returns:

  • (Boolean)

    True if rooms are equal, false otherwise.

Since:

  • 4.4.0



76
77
78
# File 'lib/lita/room.rb', line 76

def ==(other)
  other.respond_to?(:id) && id == other.id
end

#hashFixnum

Generates a Fixnum hash value for this user object. Implemented to support equality.

Returns:

  • (Fixnum)

    The hash value.

See Also:

  • Object#hash

Since:

  • 4.4.0



84
85
86
# File 'lib/lita/room.rb', line 84

def hash
  id.hash
end

#savevoid

This method returns an undefined value.

Saves the room record to Redis, overwriting any previous data for the current ID.

Since:

  • 4.4.0



90
91
92
93
94
95
96
97
# File 'lib/lita/room.rb', line 90

def save
  

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