class MSS::Core::Response

# Response

Each Service has a Client class. There is one method per service operation defined on the client. These methods all return a {Response} object.

In addition to the response data, these responses provide metadata about the HTTP request made and the HTTP response received.

## Response Data

You can access the response data for a client request using the {#data} method or the {#[]} method. Response data is a hash and {#[]} is a shortcut for accessing this hash.

# make a request to describe one instance
ec2 = MSS::EC2.new
response = ec2.client.describe_instances(:instance_ids => ['i-12345678'])

# find the instance in the response data (2 ways to get the data)
instance = response[:reservation_set].first[:instance_set].first
instance = response.data[:reservation_set].first[:instance_set].first

instance[:status] #=> 'running'

## Response Metadata

In addition to the response data, there is additional information available with the response, including:

Given the example and response object from above:

response.request_type #=> :describe_instances
response.request_options #=> { :instance_ids => ['i-12345678'] }
response.http_request #=> #<MSS::Core::Http::Request>
response.http_response #=> #<MSS::Core::Http::Response>

Attributes

api_version[RW]

@return [String] The API version of the request/client.

cached[RW]

@return [Boolean] true if the response was generated from a

another cached response.
cached?[RW]

@return [Boolean] true if the response was generated from a

another cached response.
config[RW]

@api private

data[RW]

@return [Hash] Returns the response data as a hash.

duration[RW]

@return [Float] The total number of seconds taken to make the

request and return the response.
error[RW]

@return [MSS::Error,nil] Returns nil unless the request failed.

Normally this will be nil unless you are using the Asynchronous
interface.
http_request[RW]

@return [Core::Http::Request]

http_response[RW]

@return [Core::Http::Response]

request_options[RW]

@return [Hash] Returns the hash of options passed to the client

request method that generated this response.
request_type[RW]

@return [Symbol] The name of the client request method that

returned this response.
retry_count[RW]

@return [Integer] Returns the number of times the request

was retried.

Public Class Methods

new(http_request = nil, http_response = nil, &block) click to toggle source

@param [Http::Request] http_request @param [Http::Response] http_response

# File lib/mss/core/response.rb, line 106
def initialize http_request = nil, http_response = nil, &block
  @http_request = http_request
  @http_response = http_response
  @request_builder = block
  @data = {}
  @retry_count = 0
  @duration = 0
  build_request if @request_builder && !http_request
end

Public Instance Methods

[](key) click to toggle source

Provides access to the response data. This is a short-cut for calling `response.data`.

@param [Symbol,String] key @return [Hash,nil]

# File lib/mss/core/response.rb, line 121
def [] key
  data[key]
end
cache_key() click to toggle source

@return [String] @api private

# File lib/mss/core/response.rb, line 144
def cache_key
  [
    api_version,
    http_request.access_key_id,
    http_request.host,
    request_type,
    serialized_options
  ].join(":")
end
inspect() click to toggle source

@return [String] @api private

# File lib/mss/core/response.rb, line 138
def inspect
  data.inspect
end
network_error?() click to toggle source

@return [Boolean] Returns `true` if the http request failed due to

a networking issue.
# File lib/mss/core/response.rb, line 132
def network_error?
  http_response.network_error?
end
rebuild_request() click to toggle source

Rebuilds the HTTP request using the block passed to the initializer. This is primarily used by the client when a request must be retried (throttling, server errors, socket errors, etc). @api private

# File lib/mss/core/response.rb, line 158
def rebuild_request
  @http_request.body_stream.rewind if @http_request.body_stream
  build_request
end
safe_to_retry?() click to toggle source

@return [Boolean] Returns `false` if it is not safe to retry a

request.  This happens when the http request body is an IO
object that can not be rewound and re-streamed.
# File lib/mss/core/response.rb, line 166
def safe_to_retry?
  @http_request.body_stream.nil? or
  @http_request.body_stream.respond_to?(:rewind)
end
successful?() click to toggle source

@return [Boolean] Returns true if there is no response error.

# File lib/mss/core/response.rb, line 126
def successful?
  error.nil?
end

Protected Instance Methods

build_request() click to toggle source
# File lib/mss/core/response.rb, line 173
def build_request
  @http_request = @request_builder.call
end
method_missing(*args, &block) click to toggle source

@note The prefered method to get as response data is to use {#[]}.

This provides a backwards-compat layer to the old response objects where each response value had a method extended onto this object. Now all response data is accessible as a hash.

@see [] @see data

# File lib/mss/core/response.rb, line 186
def method_missing *args, &block
  Core::Data.new(data).send(*args, &block)
end
serialize_options_array(array) click to toggle source
# File lib/mss/core/response.rb, line 208
def serialize_options_array array
  "[" + array.map{|v| serialize_options_value(v) }.join(" ") + "]"
end
serialize_options_hash(hash) click to toggle source
# File lib/mss/core/response.rb, line 194
def serialize_options_hash(hash)
  "(" + hash.keys.sort_by(&:to_s).map do |key|
    "#{key}=#{serialize_options_value(hash[key])}"
  end.join(" ") + ")"
end
serialize_options_value(value) click to toggle source
# File lib/mss/core/response.rb, line 200
def serialize_options_value(value)
  case value
  when Hash  then serialize_options_hash(value)
  when Array then serialize_options_array(value)
  else value.inspect
  end
end
serialized_options() click to toggle source
# File lib/mss/core/response.rb, line 190
def serialized_options
  serialize_options_hash(request_options)
end