Class: SimpleAWS::Response
- Inherits:
-
Object
- Object
- SimpleAWS::Response
- Defined in:
- lib/simple_aws/core/response.rb
Overview
Wrapper object for all responses from AWS. This class gives a lot of leeway to how you access the response object. You can access the response directly through it's Hash representation, which is a direct mapping from the raw XML returned from AWS.
You can also use ruby methods. This object will convert those methods in ruby_standard into appropriate keys (camelCase) and look for them in the hash. This can be done at any depth.
This class tries not to be too magical to ensure that it never gets in the way. All nested objects are queryable like their parents are, and all sets and arrays are found and accessible through your typical Enumerable interface.
The starting point of the Response querying will vary according to the structure returned by the AWS API in question. For some APIs, like EC2, the response is a relatively flat:
<DataRequestResponse>
<requestId>...</requestId>
<dataRequested>
...
</dataRequested>
</DataRequestResponse>
In this case, your querying will start inside of <DataRequestResponse>, ala the first
method you'll probably call is data_requested. For other APIs, the response
object is a little deeper and looks like this:
<DataRequestResponse>
<DataRequestedResult>
<DataRequested>
...
</DataRequested>
</DataRequestedResult>
<ResponseMetadata>
<RequestId>...</RequestId>
</ResponseMetadata>
</DataRequestResponse>
For these response structures, your query will start inside of <DataRequestedResult>,
ala your first method call will be data_requested. To get access to the request id of
both of these structures, simply use request_id on the base response. You'll also
notice the case differences of the XML tags, this class tries to ensure that case doesn't
matter when you're querying with methods. If you're using raw hash access then yes the
case of the keys in question need to match.
This class does ensure that any collection is always an Array, given that
when AWS returns a single item in a collection, the xml-to-hash parser gives a
single hash back instead of an array. This class will also look for
array indicators from AWS, like <item> or <member> and squash them.
If AWS returns an error code, instead of getting a Response back the library will instead throw an UnsuccessfulResponse error with the pertinent information.
Defined Under Namespace
Classes: ResponseProxy
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
The raw parsed response body in Hash format.
-
#code ⇒ Object
readonly
HTTP Status code of the response.
-
#headers ⇒ Object
readonly
Hash of headers found in the response.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Direct access to the request body's hash.
-
#initialize(http_response) ⇒ Response
constructor
A new instance of Response.
-
#method_missing(name, *args) ⇒ Object
Delegate first-level method calls to the root Proxy object.
-
#request_id ⇒ Object
Get the request ID from this response.
Constructor Details
#initialize(http_response) ⇒ Response
Returns a new instance of Response.
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/simple_aws/core/response.rb', line 179 def initialize(http_response) if !http_response.success? parse_and_throw_error_from http_response end @code = http_response.code @body = http_response.parsed_response @headers = http_response.headers if @body.is_a?(Hash) inner = @body[@body.keys.first] response_root = if result_key = inner.keys.find {|k| k =~ /Result$/} inner[result_key] else inner end if response_root @request_root = ResponseProxy.new response_root end end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
Delegate first-level method calls to the root Proxy object
216 217 218 219 220 221 222 |
# File 'lib/simple_aws/core/response.rb', line 216 def method_missing(name, *args) if @request_root @request_root.send(name, *args) else super end end |
Instance Attribute Details
#body ⇒ Object (readonly)
The raw parsed response body in Hash format
167 168 169 |
# File 'lib/simple_aws/core/response.rb', line 167 def body @body end |
#code ⇒ Object (readonly)
HTTP Status code of the response
172 173 174 |
# File 'lib/simple_aws/core/response.rb', line 172 def code @code end |
#headers ⇒ Object (readonly)
Hash of headers found in the response
177 178 179 |
# File 'lib/simple_aws/core/response.rb', line 177 def headers @headers end |
Instance Method Details
#[](key) ⇒ Object
Direct access to the request body's hash. This works on the first level down in the AWS response, bypassing the root element of the returned XML so you can work directly in the attributes that matter
209 210 211 |
# File 'lib/simple_aws/core/response.rb', line 209 def [](key) @request_root[key] end |
#request_id ⇒ Object
Get the request ID from this response. Works on all known AWS response formats. Some AWS APIs don't give a request id, such as CloudFront. For responses that do not have a request id, this method returns nil.
229 230 231 232 233 234 235 236 237 |
# File 'lib/simple_aws/core/response.rb', line 229 def request_id if = @body[@body.keys.first]["ResponseMetadata"] ["RequestId"] elsif id = @body[@body.keys.first]["requestId"] id else nil end end |