Class: SimpleAWS::Response::ResponseProxy

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/simple_aws/core/response.rb

Overview

Inner proxy class that handles converting ruby methods into keys found in the underlying Hash.

Constant Summary collapse

TO_SQUASH =
%w(item member)

Instance Method Summary collapse

Constructor Details

#initialize(local_root) ⇒ ResponseProxy

Returns a new instance of ResponseProxy



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/simple_aws/core/response.rb', line 93

def initialize(local_root)
  first_key = local_root.keys.first
  if local_root.keys.length == 1 && TO_SQUASH.include?(first_key)
    # Ensure squash key is ignored and it's children are always
    # turned into an array.
    @local_root = [local_root[first_key]].flatten.map do |entry|
      ResponseProxy.new entry
    end
  else
    @local_root = local_root
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/simple_aws/core/response.rb', line 127

def method_missing(name, *args)
  if key = key_matching(name)
    value_or_proxy @local_root[key]
  else
    super
  end
end

Instance Method Details

#[](key_or_idx) ⇒ Object



106
107
108
# File 'lib/simple_aws/core/response.rb', line 106

def [](key_or_idx)
  value_or_proxy @local_root[key_or_idx]
end

#each(&block) ⇒ Object



123
124
125
# File 'lib/simple_aws/core/response.rb', line 123

def each(&block)
  @local_root.each(&block)
end

#keysObject

Get all keys at the current depth of the Response object. This method will raise a NoMethodError if the current depth is an array.



115
116
117
# File 'lib/simple_aws/core/response.rb', line 115

def keys
  @local_root.keys
end

#lengthObject



119
120
121
# File 'lib/simple_aws/core/response.rb', line 119

def length
  @local_root.length
end