Class: Imperium::Response

Inherits:
SimpleDelegator
  • Object
show all
Includes:
Enumerable
Defined in:
lib/imperium/response.rb

Overview

A Response is a decorator around the 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

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, response_object_class: :none) ⇒ Response

Construct a new response

Parameters:

  • response (HTTP::Message)

    The response as returned from the http client

  • response_object_class (APIObject) (defaults to: :none)

    The class to coerce values into, if left the default (:none) no coersion will be attempted.



22
23
24
25
26
27
28
29
# 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

Class Attribute Details

.default_response_object_classObject

Returns the value of attribute default_response_object_class.



14
15
16
# File 'lib/imperium/response.rb', line 14

def default_response_object_class
  @default_response_object_class
end

Instance Method Details

#coerced_bodyArray<Hash, APIObject>, Hash<String => APIObject>

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

Returns:



84
85
86
87
88
89
90
91
92
93
# 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) ⇒ Object

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



77
78
79
# File 'lib/imperium/response.rb', line 77

def each(&block)
  coerced_body.each(&block)
end

#indexNilClass, Integer

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

Returns:

  • (NilClass)

    When the X-Consul-Index header is not present.

  • (Integer)


55
56
57
58
59
60
# 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?TrueClass, ...

Indicates if the contacted server has a known leader.

Returns:

  • (TrueClass)

    When the response indicates there is a known leader

  • (FalseClass)

    When the response indicates there is not a known leader

  • (NilClass)

    When the X-Consul-KnownLeader header is not present.



36
37
38
39
# File 'lib/imperium/response.rb', line 36

def known_leader?
  return unless headers.key?('X-Consul-KnownLeader')
  headers['X-Consul-KnownLeader'] == 'true'
end

#last_contactNilClass, Integer

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

Returns:

  • (NilClass)

    When the X-Consul-LastContact header is not present.

  • (Integer)


46
47
48
49
# 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?Boolean

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

Returns:

  • (Boolean)


64
65
66
# File 'lib/imperium/response.rb', line 64

def not_found?
  status == 404
end

#translate_addresses?TrueClass, FalseClass

Indicate status of translate_wan_addrs setting on the server.

Returns:

  • (TrueClass)

    When X-Consul-Translate-Addresses is set

  • (FalseClass)

    When X-Consul-Translate-Addresses is unset



72
73
74
# File 'lib/imperium/response.rb', line 72

def translate_addresses?
  headers.key?('X-Consul-Translate-Addresses')
end