Class: Async::Matrix::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/async/matrix/client.rb

Overview

Async HTTP client for the Matrix Client-Server API.

Every outbound request is authenticated with the appservice ‘as_token`. All methods are fiber-safe and run naturally inside Falcon’s async reactor.

client = Async::Matrix::Client.new(config)
client.send_text("!room:example.com", "Hello world")
client.join_room("!room:example.com")

Constant Summary collapse

CLIENT_PREFIX =
"/_matrix/client/v3"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.



30
31
32
33
34
35
36
37
38
# File 'lib/async/matrix/client.rb', line 30

def initialize(config)
	@config  = config
	@base    = config.homeserver_url
	@headers = [
		["authorization", "Bearer #{config.as_token}"],
		["content-type",  "application/json"],
		["user-agent",    "AsyncMatrix/#{Async::Matrix::VERSION}"]
	]
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



28
29
30
# File 'lib/async/matrix/client.rb', line 28

def config
  @config
end

Instance Method Details

#closeObject



108
109
110
111
# File 'lib/async/matrix/client.rb', line 108

def close
	@internet&.close
	@internet = nil
end

#get(path) ⇒ Object



96
97
98
# File 'lib/async/matrix/client.rb', line 96

def get(path)
	request("GET", path)
end

#join_room(room_id) ⇒ Object

── Room actions ───────────────────────────────────────────



64
65
66
# File 'lib/async/matrix/client.rb', line 64

def join_room(room_id)
	post("#{CLIENT_PREFIX}/join/#{encode(room_id)}")
end

#leave_room(room_id) ⇒ Object



68
69
70
# File 'lib/async/matrix/client.rb', line 68

def leave_room(room_id)
	post("#{CLIENT_PREFIX}/rooms/#{encode(room_id)}/leave")
end

#post(path, body = {}) ⇒ Object



104
105
106
# File 'lib/async/matrix/client.rb', line 104

def post(path, body = {})
	request("POST", path, body)
end

#put(path, body = {}) ⇒ Object



100
101
102
# File 'lib/async/matrix/client.rb', line 100

def put(path, body = {})
	request("PUT", path, body)
end

#send_html(room_id, html, plaintext = nil) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/async/matrix/client.rb', line 47

def send_html(room_id, html, plaintext = nil)
	content = {
		msgtype:        "m.text",
		body:           plaintext || html.gsub(/<[^>]+>/, ""),
		format:         "org.matrix.custom.html",
		formatted_body: html
	}
	send_message_event(room_id, "m.room.message", content)
end

#send_message_event(room_id, event_type, content) ⇒ Object

── Low-level HTTP ─────────────────────────────────────────



90
91
92
93
94
# File 'lib/async/matrix/client.rb', line 90

def send_message_event(room_id, event_type, content)
	txn_id = SecureRandom.uuid
	path = "#{CLIENT_PREFIX}/rooms/#{encode(room_id)}/send/#{encode(event_type)}/#{txn_id}"
	put(path, content)
end

#send_notice(room_id, text) ⇒ Object



57
58
59
60
# File 'lib/async/matrix/client.rb', line 57

def send_notice(room_id, text)
	content = {msgtype: "m.notice", body: text}
	send_message_event(room_id, "m.room.message", content)
end

#send_text(room_id, text) ⇒ Object

── Messaging ──────────────────────────────────────────────



42
43
44
45
# File 'lib/async/matrix/client.rb', line 42

def send_text(room_id, text)
	content = {msgtype: "m.text", body: text}
	send_message_event(room_id, "m.room.message", content)
end

#set_display_name(name, user_id = nil) ⇒ Object

── Profile ────────────────────────────────────────────────



74
75
76
77
78
79
80
# File 'lib/async/matrix/client.rb', line 74

def set_display_name(name, user_id = nil)
	uid = user_id || @config.bot_mxid
	put(
		"#{CLIENT_PREFIX}/profile/#{encode(uid)}/displayname",
		{displayname: name}
	)
end

#whoamiObject

── Verification ───────────────────────────────────────────



84
85
86
# File 'lib/async/matrix/client.rb', line 84

def whoami
	get("#{CLIENT_PREFIX}/account/whoami")
end