Class: Yandex360::Response

Inherits:
Record
  • Object
show all
Defined in:
lib/yandex360/record.rb

Overview

For a reply with no documented entity behind it, such as true, and for nested structures, parts of which the reference leaves as free-form JSON. Readers come from the keys that arrived rather than from a declaration, so there is nothing to keep in sync, and #[] still reads anything.

Instance Method Summary collapse

Methods inherited from Record

#==, #[], attribute, attribute_keys, #hash, #inspect, #key?, #to_h

Constructor Details

#initialize(data) ⇒ Response

Returns a new instance of Response.



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/yandex360/record.rb', line 89

def initialize(data)
  super
  to_h.each_key do |key|
    next unless key.is_a?(String) && key.match?(/\A[a-zA-Z_]\w*\z/)
    # Never shadow a real method such as to_h or hash; those keys stay
    # reachable through #[].
    next if self.class.method_defined?(key) || singleton_class.method_defined?(key)

    define_singleton_method(key) { self[key] }
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Readers are defined above for the keys that arrived, so reaching here means the key is absent. Say which ones are there rather than leaving the caller to guess.

Raises:

  • (NoMethodError)


104
105
106
107
108
109
110
# File 'lib/yandex360/record.rb', line 104

def method_missing(name, *args)
  return super unless args.empty?

  raise NoMethodError, "#{name} is not present in this response; " \
                       "readable keys are #{to_h.keys.join(', ')}, " \
                       "and any key can be read with []"
end

Instance Method Details

#respond_to_missing?(_name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/yandex360/record.rb', line 112

def respond_to_missing?(_name, _include_private=false)
  false
end