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..whoami.get
chain.rooms("!abc:ex.com").send("m.room.message", "txn1").put(body: "hi")
chain.rooms("!abc:ex.com").messages.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

Constructor Details

#initialize(client:, path_tree:, prefix: nil) ⇒ Chain

Returns a new instance of Chain.



43
44
45
46
47
48
# File 'lib/async/matrix/api/chain.rb', line 43

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



100
101
102
103
104
105
106
107
# File 'lib/async/matrix/api/chain.rb', line 100

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



71
72
73
74
# File 'lib/async/matrix/api/chain.rb', line 71

def delete(**kwargs)
  max_retries = kwargs.delete(:max_retries)
  execute("DELETE", query: kwargs, max_retries: max_retries)
end

#get(**kwargs) ⇒ Object

── Terminal HTTP methods ──────────────────────────────────



52
53
54
55
# File 'lib/async/matrix/api/chain.rb', line 52

def get(**kwargs)
  max_retries = kwargs.delete(:max_retries)
  execute("GET", query: kwargs, max_retries: max_retries)
end

#inspectObject



89
90
91
92
# File 'lib/async/matrix/api/chain.rb', line 89

def inspect
  segments = _build_full_segments
  "#<Async::Matrix::Api::Chain path=/#{segments.join("/")}>"
end

#patch(body = nil, **kwargs) ⇒ Object



76
77
78
79
80
81
# File 'lib/async/matrix/api/chain.rb', line 76

def patch(body = nil, **kwargs)
  content_type = kwargs.delete(:content_type)
  max_retries  = kwargs.delete(:max_retries)
  query        = _extract_query_params(kwargs)
  execute("PATCH", body: body || kwargs, content_type: content_type, max_retries: max_retries, query: query)
end

#path_segmentsObject



94
95
96
# File 'lib/async/matrix/api/chain.rb', line 94

def path_segments
  _build_full_segments
end

#post(body = nil, **kwargs) ⇒ Object



57
58
59
60
61
62
# File 'lib/async/matrix/api/chain.rb', line 57

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



64
65
66
67
68
69
# File 'lib/async/matrix/api/chain.rb', line 64

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_sObject

── Chain inspection ───────────────────────────────────────



85
86
87
# File 'lib/async/matrix/api/chain.rb', line 85

def to_s
  "/" + _build_full_segments.map { |s| _encode(s) }.join("/")
end