class Chatrix::Client

A client wrapping the API in easy-to-use methods.

Attributes

me[R]

@return [User] The user associated with the access token.

Public Class Methods

new(token, id, homeserver: nil) click to toggle source

Initializes a new Client instance.

Currently it requires a token, future versions will allow login with arbitrary details.

@param token [String] The access token to use. @param id [String] The user ID of the token owner. @param homeserver [String,nil] Homeserver to connect to. If not set,

the default homeserver defined in Chatrix::Matrix will be used.
# 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

Public Instance Methods

get_room(id) click to toggle source

Gets the room with the specified ID, alias, or name.

@return [Room,nil] Returns a Room object if the room could be found,

otherwise `nil`.
# File lib/chatrix/client.rb, line 88
def get_room(id)
  @rooms[id]
end
get_user(id) click to toggle source

Gets the user with the specified ID or display name.

@return [User,nil] Returns a User object if the user could be found,

otherwise `nil`.
# File lib/chatrix/client.rb, line 80
def get_user(id)
  @users[id]
end
join_room(id) click to toggle source

Joins the room with the specified ID. @param id [String] The room ID to join. @return [Room] The Room instance for the joined room.

# File lib/chatrix/client.rb, line 95
def join_room(id)
  @rooms.join id
end
start_syncing() click to toggle source

Starts syncing against the homeserver.

Launches a new thread that will continously check for new events from the server.

@see sync! See the documentation for {#sync!} for more information

and what happens in case of an error during sync.
# 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() click to toggle source

Stops syncing against the homeserver.

# 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

Private Instance Methods

process_sync(events) click to toggle source

Process the sync result.

@param events [Hash] The events to sync.

# 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
sync!() click to toggle source

Syncs against the server.

If an API error occurs during sync, it will be rescued and broadcasted as `:sync_error`.

# 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