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.
-
#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 |
# 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.timeline.on(:message) { |r, m| broadcast(:room_message, r, m) } end 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.
72 73 74 |
# File 'lib/chatrix/client.rb', line 72 def get_room(id) @rooms[id] end |
#get_user(id) ⇒ User?
Gets the user with the specified ID or display name.
64 65 66 |
# File 'lib/chatrix/client.rb', line 64 def get_user(id) @users[id] end |
#process_sync(events) ⇒ Object (private)
Process the sync result.
92 93 94 95 96 97 98 |
# File 'lib/chatrix/client.rb', line 92 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.
48 49 50 |
# File 'lib/chatrix/client.rb', line 48 def start_syncing @sync_thread ||= Thread.new { loop { sync! } } end |
#stop_syncing ⇒ Object
Stops syncing against the homeserver.
53 54 55 56 57 58 |
# File 'lib/chatrix/client.rb', line 53 def stop_syncing return unless @sync_thread.is_a? Thread @sync_thread.exit @sync_thread.join @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
.
82 83 84 85 86 87 |
# File 'lib/chatrix/client.rb', line 82 def sync! events = @matrix.sync since: @since process_sync events rescue ApiError => err broadcast(:sync_error, err) end |