Module: MatrixSdk::Response

Defined in:
lib/matrix_sdk/response.rb

Overview

An usability wrapper for API responses as an extended [Hash] All results can be read as both hash keys and as read-only methods on the key

Examples:

Simple usage of the response wrapper to get the avatar URL

resp = api.get_avatar_url(api.whoami?.user_id)
# => { avatar_url: 'mxc://matrix.org/SDGdghriugerRg' }
resp.is_a? Hash
# => true
resp.key? :avatar_url
# => true
resp.avatar_url
# => 'mxc://matrix.org/SDGdghriugerRg'
resp.api.set_avatar_url(...)
# => {}

See Also:

  • Hash

Since:

  • 0.0.3

Defined Under Namespace

Modules: Extensions

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#apiApi (readonly)

Returns The API connection that returned the response.

Returns:

  • (Api)

    The API connection that returned the response



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/matrix_sdk/response.rb', line 23

module Response
  def self.new(api, data)
    data.extend(Extensions)
    data.instance_variable_set(:@api, api)

    data.select { |_k, v| v.is_a? Hash }
        .each { |_v, v| Response.new api, v }

    data
  end

  module Extensions
    attr_reader :api

    def respond_to_missing?(name, *_args)
      key? name
    end

    def method_missing(name, *args)
      return fetch(name) if key?(name) && args.empty?

      super
    end
  end
end

Class Method Details

.new(api, data) ⇒ Object

Since:

  • 0.0.3



24
25
26
27
28
29
30
31
32
# File 'lib/matrix_sdk/response.rb', line 24

def self.new(api, data)
  data.extend(Extensions)
  data.instance_variable_set(:@api, api)

  data.select { |_k, v| v.is_a? Hash }
      .each { |_v, v| Response.new api, v }

  data
end