Class: Async::Matrix::Api::Chain
- Inherits:
- BasicObject
- Defined in:
- lib/async/matrix/api/chain.rb
Overview
Wraps a StringBuilder to build Matrix API paths via method chaining, terminated by .get(), .post(), .put(), or .delete() which validate the path against the PathTree and fire the HTTP request.
Inherits from BasicObject so that method names like ‘send`, `display`, `format`, `test`, etc. are not defined and fall through to method_missing, where they get recorded as URL path segments.
Usage:
chain = Chain.new(client: client, path_tree: tree, prefix: %w[_matrix client v3])
chain.account.whoami.get
chain.rooms("!abc:ex.com").send("m.room.message", "txn1").put(body: "hi")
chain.rooms("!abc:ex.com")..get(dir: "b", limit: 10)
Constant Summary collapse
- BINARY_ROUTES =
Paths that carry raw bytes instead of JSON. Matched by _binary_route? where * is a single-segment wildcard. The HTTP method determines the operation:
GET → download (returns raw bytes) POST/PUT → upload (sends raw bytes) [ "/_matrix/media/v3/upload", "/_matrix/media/v3/upload/*/*", "/_matrix/media/v3/download/*/*", "/_matrix/media/v3/download/*/*/*", "/_matrix/media/v3/thumbnail/*/*", "/_matrix/client/v1/media/download/*/*", "/_matrix/client/v1/media/download/*/*/*", "/_matrix/client/v1/media/thumbnail/*/*", ].freeze
Instance Method Summary collapse
- #delete(**kwargs) ⇒ Object
-
#get(**kwargs) ⇒ Object
── Terminal HTTP methods ──────────────────────────────────.
-
#initialize(client:, path_tree:, prefix: nil) ⇒ Chain
constructor
A new instance of Chain.
- #inspect ⇒ Object
-
#method_missing(name, *args, **kwargs) ⇒ Object
── Everything else becomes a path segment ─────────────────.
- #path_segments ⇒ Object
- #post(body = nil, **kwargs) ⇒ Object
- #put(body = nil, **kwargs) ⇒ Object
-
#to_s ⇒ Object
── Chain inspection ───────────────────────────────────────.
Constructor Details
#initialize(client:, path_tree:, prefix: nil) ⇒ Chain
Returns a new instance of Chain.
45 46 47 48 49 50 |
# File 'lib/async/matrix/api/chain.rb', line 45 def initialize(client:, path_tree:, prefix: nil) @client = client @path_tree = path_tree @prefix = prefix || ["_matrix", "client", "v3"] @buffer = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, **kwargs) ⇒ Object
── Everything else becomes a path segment ─────────────────
95 96 97 98 99 100 101 102 |
# File 'lib/async/matrix/api/chain.rb', line 95 def method_missing(name, *args, **kwargs) if kwargs.empty? @buffer << [name.to_s, args] else @buffer << [name.to_s, [*args, kwargs]] end self end |
Instance Method Details
#delete(**kwargs) ⇒ Object
73 74 75 76 |
# File 'lib/async/matrix/api/chain.rb', line 73 def delete(**kwargs) max_retries = kwargs.delete(:max_retries) execute("DELETE", query: kwargs, max_retries: max_retries) end |
#get(**kwargs) ⇒ Object
── Terminal HTTP methods ──────────────────────────────────
54 55 56 57 |
# File 'lib/async/matrix/api/chain.rb', line 54 def get(**kwargs) max_retries = kwargs.delete(:max_retries) execute("GET", query: kwargs, max_retries: max_retries) end |
#inspect ⇒ Object
84 85 86 87 |
# File 'lib/async/matrix/api/chain.rb', line 84 def inspect segments = _build_full_segments "#<Async::Matrix::Api::Chain path=/#{segments.join("/")}>" end |
#path_segments ⇒ Object
89 90 91 |
# File 'lib/async/matrix/api/chain.rb', line 89 def path_segments _build_full_segments end |
#post(body = nil, **kwargs) ⇒ Object
59 60 61 62 63 64 |
# File 'lib/async/matrix/api/chain.rb', line 59 def post(body = nil, **kwargs) content_type = kwargs.delete(:content_type) max_retries = kwargs.delete(:max_retries) query = _extract_query_params(kwargs) execute("POST", body: body || kwargs, content_type: content_type, max_retries: max_retries, query: query) end |
#put(body = nil, **kwargs) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/async/matrix/api/chain.rb', line 66 def put(body = nil, **kwargs) content_type = kwargs.delete(:content_type) max_retries = kwargs.delete(:max_retries) query = _extract_query_params(kwargs) execute("PUT", body: body || kwargs, content_type: content_type, max_retries: max_retries, query: query) end |
#to_s ⇒ Object
── Chain inspection ───────────────────────────────────────
80 81 82 |
# File 'lib/async/matrix/api/chain.rb', line 80 def to_s "/" + _build_full_segments.map { |s| _encode(s) }.join("/") end |