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
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.
-
#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 miliseconds 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.
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/imperium/response.rb', line 72 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
65 66 67 |
# File 'lib/imperium/response.rb', line 65 def each(&block) coerced_body.each(&block) 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 miliseconds 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.
52 53 54 |
# File 'lib/imperium/response.rb', line 52 def not_found? status == 404 end |
#translate_addresses? ⇒ TrueClass, FalseClass
Indicate status of translate_wan_addrs setting on the server.
60 61 62 |
# File 'lib/imperium/response.rb', line 60 def translate_addresses? headers.key?('X-Consul-Translate-Addresses') end |