Class: Async::Matrix::Client
- Inherits:
-
Object
- Object
- Async::Matrix::Client
- 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
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #close ⇒ Object
- #get(path) ⇒ Object
-
#initialize(config) ⇒ Client
constructor
A new instance of Client.
-
#join_room(room_id) ⇒ Object
── Room actions ───────────────────────────────────────────.
- #leave_room(room_id) ⇒ Object
- #post(path, body = {}) ⇒ Object
- #put(path, body = {}) ⇒ Object
- #send_html(room_id, html, plaintext = nil) ⇒ Object
-
#send_message_event(room_id, event_type, content) ⇒ Object
── Low-level HTTP ─────────────────────────────────────────.
- #send_notice(room_id, text) ⇒ Object
-
#send_text(room_id, text) ⇒ Object
── Messaging ──────────────────────────────────────────────.
-
#set_display_name(name, user_id = nil) ⇒ Object
── Profile ────────────────────────────────────────────────.
-
#whoami ⇒ Object
── Verification ───────────────────────────────────────────.
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
#config ⇒ Object (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
#close ⇒ Object
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 } (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 (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} (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} (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 |
#whoami ⇒ Object
── Verification ───────────────────────────────────────────
84 85 86 |
# File 'lib/async/matrix/client.rb', line 84 def whoami get("#{CLIENT_PREFIX}/account/whoami") end |