Class: Async::Matrix::Api::Concat

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

Overview

StringBuilder concat handler that renders a method chain into URL path segments.

Convention:

- Bare methods become path segments:       .account.whoami -> ["account", "whoami"]
- Methods with args inject the arg as a path segment after the method name:
    .rooms("!abc:ex.com") -> ["rooms", "!abc:ex.com"]
- .call("literal") injects a raw path segment:
    .("m.room.message") -> ["m.room.message"]

The handler returns an array of raw (unencoded) segments. URL-encoding is applied later when constructing the final URL.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buffer) ⇒ Concat

Returns a new instance of Concat.



29
30
31
# File 'lib/async/matrix/api/concat.rb', line 29

def initialize(buffer)
  @buffer = buffer.respond_to?(:to_a) ? buffer.to_a : buffer
end

Class Method Details

.call(buffer) ⇒ Object



25
26
27
# File 'lib/async/matrix/api/concat.rb', line 25

def self.call(buffer)
  new(buffer).segments
end

Instance Method Details

#segmentsObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/async/matrix/api/concat.rb', line 33

def segments
  result = []

  @buffer.each do |entry|
    case entry
    when Symbol
      # :slash, :dash -- ignore in URL context
      next
    when Array
      name, args = entry
      next unless name.is_a?(String)

      if args.nil? || args.empty?
        # Bare method -> path segment
        result << name
      else
        # Method with args -> method name as segment, then each arg as segment
        result << name
        args.each do |arg|
          next if arg.is_a?(Hash) # kwargs are not path segments
          result << arg.to_s
        end
      end
    end
  end

  result
end