Class: Chatrix::Client
- Inherits:
-
Object
- Object
- Chatrix::Client
- Includes:
- Wisper::Publisher
- Defined in:
- lib/chatrix/client.rb
Overview
A client wrapping the API in easy-to-use methods.
Instance Attribute Summary collapse
-
#me ⇒ User
readonly
The user associated with the access token.
Instance Method Summary collapse
-
#get_room(id) ⇒ Room?
Gets the room with the specified ID, alias, or name.
-
#get_user(id) ⇒ User?
Gets the user with the specified ID or display name.
-
#initialize(token, id, homeserver: nil) ⇒ Client
constructor
Initializes a new Client instance.
-
#join_room(id) ⇒ Room
Joins the room with the specified ID.
-
#process_sync(events) ⇒ Object
private
Process the sync result.
-
#start_syncing ⇒ Object
Starts syncing against the homeserver.
-
#stop_syncing ⇒ Object
Stops syncing against the homeserver.
-
#sync! ⇒ Object
private
Syncs against the server.
Constructor Details
#initialize(token, id, homeserver: nil) ⇒ Client
Initializes a new Client instance.
Currently it requires a token, future versions will allow login with arbitrary details.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/chatrix/client.rb', line 27 def initialize(token, id, homeserver: nil) @matrix = Matrix.new token, homeserver @users = Users.new @rooms = Rooms.new @users, @matrix @me = @users.send(:get_user, id) @rooms.on(:added) do |room| broadcast(:room_added, room) room.on(:invited) { |s, i| broadcast(:invited, room, s, i) } room.timeline.on(:message) { |r, m| broadcast(:room_message, r, m) } end on(:disconnected) { stop_syncing } end |
Instance Attribute Details
#me ⇒ User (readonly)
Returns The user associated with the access token.
16 17 18 |
# File 'lib/chatrix/client.rb', line 16 def me @me end |
Instance Method Details
#get_room(id) ⇒ Room?
Gets the room with the specified ID, alias, or name.
88 89 90 |
# File 'lib/chatrix/client.rb', line 88 def get_room(id) @rooms[id] end |
#get_user(id) ⇒ User?
Gets the user with the specified ID or display name.
80 81 82 |
# File 'lib/chatrix/client.rb', line 80 def get_user(id) @users[id] end |
#join_room(id) ⇒ Room
Joins the room with the specified ID.
95 96 97 |
# File 'lib/chatrix/client.rb', line 95 def join_room(id) @rooms.join id end |
#process_sync(events) ⇒ Object (private)
Process the sync result.
115 116 117 118 119 120 121 |
# File 'lib/chatrix/client.rb', line 115 def process_sync(events) return unless events.is_a? Hash @since = events['next_batch'] broadcast(:sync, events) @rooms.process_events events['rooms'] if events.key? 'rooms' end |
#start_syncing ⇒ Object
Starts syncing against the homeserver.
Launches a new thread that will continously check for new events from the server.
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/chatrix/client.rb', line 51 def start_syncing @sync_thread ||= Thread.new do begin loop { sync! } rescue => e broadcast(:connection_error, e) ensure broadcast(:disconnected) end end end |
#stop_syncing ⇒ Object
Stops syncing against the homeserver.
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/chatrix/client.rb', line 64 def stop_syncing return unless @sync_thread.is_a? Thread @sync_thread.exit @sync_thread.join rescue => e broadcast(:stop_error, e) ensure @sync_thread = nil end |
#sync! ⇒ Object (private)
Syncs against the server.
If an API error occurs during sync, it will be rescued and broadcasted
as :sync_error
.
105 106 107 108 109 110 |
# File 'lib/chatrix/client.rb', line 105 def sync! events = @matrix.sync since: @since process_sync events rescue ApiError => err broadcast(:sync_error, err) end |