Class: Chatrix::Rooms
- Inherits:
-
Object
- Object
- Chatrix::Rooms
- Includes:
- Wisper::Publisher
- Defined in:
- lib/chatrix/rooms.rb
Overview
Manages the rooms known to the client.
Instance Method Summary collapse
-
#[](id) ⇒ Room?
Gets a room by its ID, alias, or name.
-
#get_room(id) ⇒ Room
private
Gets the Room instance associated with a room ID.
-
#initialize(users, matrix) ⇒ Rooms
constructor
Initializes a new Rooms instance.
-
#join(id) ⇒ Room
Attempts to join the specified room.
-
#process_events(events) ⇒ Object
Processes a list of room events from syncs.
-
#process_invite(events) ⇒ Object
private
Process
invite
room events. -
#process_join(events) ⇒ Object
private
Process
join
room events. -
#process_leave(events) ⇒ Object
private
Process
leave
room events.
Constructor Details
#initialize(users, matrix) ⇒ Rooms
Initializes a new Rooms instance.
17 18 19 20 21 22 23 24 |
# File 'lib/chatrix/rooms.rb', line 17 def initialize(users, matrix) @matrix = matrix @users = users # room_id => room @rooms = {} end |
Instance Method Details
#[](id) ⇒ Room?
Gets a room by its ID, alias, or name.
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/chatrix/rooms.rb', line 30 def [](id) return @rooms[id] if id.start_with? '!' if id.start_with? '#' res = @rooms.find { |_, r| r.canonical_alias == id } return res.last if res.respond_to? :last end res = @rooms.find { |_, r| r.name == id } res.last if res.respond_to? :last end |
#get_room(id) ⇒ Room (private)
Gets the Room instance associated with a room ID. If there is no Room instance for the ID, one is created and returned.
67 68 69 70 71 72 73 |
# File 'lib/chatrix/rooms.rb', line 67 def get_room(id) return @rooms[id] if @rooms.key? id room = Room.new id, @users, @matrix @rooms[id] = room broadcast(:added, room) room end |
#join(id) ⇒ Room
Attempts to join the specified room.
47 48 49 |
# File 'lib/chatrix/rooms.rb', line 47 def join(id) get_room(id).tap(&:join) end |
#process_events(events) ⇒ Object
Processes a list of room events from syncs.
54 55 56 57 58 |
# File 'lib/chatrix/rooms.rb', line 54 def process_events(events) process_join events['join'] if events.key? 'join' process_invite events['invite'] if events.key? 'invite' process_leave events['leave'] if events.key? 'leave' end |
#process_invite(events) ⇒ Object (private)
Process invite
room events.
87 88 89 90 91 |
# File 'lib/chatrix/rooms.rb', line 87 def process_invite(events) events.each do |room, data| get_room(room).process_invite data end end |
#process_join(events) ⇒ Object (private)
Process join
room events.
78 79 80 81 82 |
# File 'lib/chatrix/rooms.rb', line 78 def process_join(events) events.each do |room, data| get_room(room).process_join data end end |
#process_leave(events) ⇒ Object (private)
Process leave
room events.
96 97 98 99 100 |
# File 'lib/chatrix/rooms.rb', line 96 def process_leave(events) events.each do |room, data| get_room(room).process_leave data end end |