Class: Imperium::Response
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Imperium::Response
- 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
Direct Known Subclasses
AgentListChecksResponse, AgentListServicesResponse, EventFireResponse, KVDELETEResponse, KVGETResponse, KVPUTResponse, TransactionResponse
Class Attribute Summary collapse
-
.default_response_object_class ⇒ Object
Returns the value of attribute default_response_object_class.
Instance Method Summary collapse
-
#coerced_body ⇒ Array<Hash, APIObject>, Hash<String => APIObject>
Parse the response JSON and initialize objects using the class passed to the constructor.
-
#each(&block) ⇒ Object
Iterate over the values contained in the structure returned from #coerced_body.
-
#index ⇒ NilClass, Integer
The index returned from a request via the X-Consul-Index header.
-
#initialize(response, response_object_class: :none) ⇒ Response
constructor
Construct a new response.
-
#known_leader? ⇒ TrueClass, ...
Indicates if the contacted server has a known leader.
-
#last_contact ⇒ NilClass, Integer
The time in milliseconds since the contacted server has been in contact with the leader.
-
#not_found? ⇒ Boolean
A convenience method for checking if the response had a 404 status code.
-
#translate_addresses? ⇒ TrueClass, FalseClass
Indicate status of translate_wan_addrs setting on the server.
Constructor Details
#initialize(response, response_object_class: :none) ⇒ Response
Construct a new response
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_class ⇒ Object
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_body ⇒ Array<Hash, APIObject>, Hash<String => APIObject>
Parse the response JSON and initialize objects using the class passed to the constructor.
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 |
#index ⇒ NilClass, Integer
The index returned from a request via the X-Consul-Index header.
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.
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_contact ⇒ NilClass, Integer
The time in milliseconds since the contacted server has been in contact with the leader.
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.
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.
72 73 74 |
# File 'lib/imperium/response.rb', line 72 def translate_addresses? headers.key?('X-Consul-Translate-Addresses') end |