Class: Chatrix::Client

Inherits:
Object
  • Object
show all
Includes:
Wisper::Publisher
Defined in:
lib/chatrix/client.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • token (String)

    The access token to use.

  • id (String)

    The user ID of the token owner.

  • homeserver (String, nil) (defaults to: nil)

    Homeserver to connect to. If not set, the default homeserver defined in Chatrix::Matrix will be used.



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

#meUser (readonly)

Returns The user associated with the access token.

Returns:

  • (User)

    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.

Returns:

  • (Room, nil)

    Returns a Room object if the room could be found, otherwise nil.



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.

Returns:

  • (User, nil)

    Returns a User object if the user could be found, otherwise nil.



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.

Parameters:

  • events (Hash)

    The events to sync.



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_syncingObject

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_syncingObject

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