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.



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



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

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

#inspectObject



91
92
93
94
# File 'lib/async/matrix/api/chain.rb', line 91

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

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



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

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



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

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_sObject

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



87
88
89
# File 'lib/async/matrix/api/chain.rb', line 87

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