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
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

#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.



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.

Returns:

  • (User, nil)

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



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.

Parameters:

  • id (String)

    The room ID to join.

Returns:

  • (Room)

    The Room instance for the joined room.



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.

Parameters:

  • events (Hash)

    The events to sync.



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_syncingObject

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_syncingObject

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