class Imperium::Response

A Response is a decorator around the {www.rubydoc.info/gems/httpclient/HTTP/Message HTTP::Message} object returned when a request is made.

It exposes, through a convenient API, headers common to all interactions with the Consul HTTP API

Attributes

default_response_object_class[RW]

Public Class Methods

new(response, response_object_class: :none) click to toggle source

Construct a new response

@param response [HTTP::Message] The response as returned from the http client @param response_object_class [APIObject] The class to coerce values into,

if left the default (:none) no coersion will be attempted.
Calls superclass method
# File lib/imperium/response.rb, line 22
def initialize(response, response_object_class: :none)
  super(response)
  @klass = if response_object_class == :none
             self.class.default_response_object_class
           else
             response_object_class
           end
end

Public Instance Methods

coerced_body() click to toggle source

Parse the response JSON and initialize objects using the class passed to the constructor.

@return [Array<Hash, APIObject>, Hash<String => APIObject>]

# File lib/imperium/response.rb, line 84
def coerced_body
  return parsed_body if @klass == :none || @klass.nil?
  @coerced_body ||= if parsed_body.is_a?(Array)
                      parsed_body.map { |attrs| @klass.new(attrs) }
                    else
                      parsed_body.each_with_object({}) { |(k, attrs), h|
                        h[k] = @klass.new(attrs)
                      }
                    end
end
each(&block) click to toggle source

Iterate over the values contained in the structure returned from {#coerced_body}

# File lib/imperium/response.rb, line 77
def each(&block)
  coerced_body.each(&block)
end
index() click to toggle source

The index returned from a request via the X-Consul-Index header.

@return [NilClass] When the X-Consul-Index header is not present. @return [Integer]

# File lib/imperium/response.rb, line 55
def index
  return nil unless headers.key?('X-Consul-Index')
  Integer(headers['X-Consul-Index'])
rescue ArgumentError
  return nil
end
known_leader?() click to toggle source

Indicates if the contacted server has a known leader.

@return [TrueClass] When the response indicates there is a known leader @return [FalseClass] When the response indicates there is not a known leader @return [NilClass] When the X-Consul-KnownLeader header is not present.

# File lib/imperium/response.rb, line 36
def known_leader?
  return unless headers.key?('X-Consul-KnownLeader')
  headers['X-Consul-KnownLeader'] == 'true'
end
last_contact() click to toggle source

The time in milliseconds since the contacted server has been in contact with the leader.

@return [NilClass] When the X-Consul-LastContact header is not present. @return [Integer]

# File lib/imperium/response.rb, line 46
def last_contact
  return unless headers.key?('X-Consul-LastContact')
  Integer(headers['X-Consul-LastContact'])
end
not_found?() click to toggle source

A convenience method for checking if the response had a 404 status code.

# File lib/imperium/response.rb, line 64
def not_found?
  status == 404
end
translate_addresses?() click to toggle source

Indicate status of translate_wan_addrs setting on the server.

@return [TrueClass] When X-Consul-Translate-Addresses is set @return [FalseClass] When X-Consul-Translate-Addresses is unset

# File lib/imperium/response.rb, line 72
def translate_addresses?
  headers.key?('X-Consul-Translate-Addresses')
end

Private Instance Methods

parsed_body() click to toggle source
# File lib/imperium/response.rb, line 97
def parsed_body
  @parsed_body ||= JSON.parse(content)
end