Class: Rubix::Response
- Inherits:
-
Object
- Object
- Rubix::Response
- Defined in:
- lib/rubix/response.rb
Overview
A class used to wrap Net::HTTP::Response objects to make it easier to inspect them for various cases.
Instance Attribute Summary collapse
-
#body ⇒ String
readonly
The raw HTTP response body.
-
#code ⇒ Fixnum
readonly
The raw HTTP response code.
-
#http_response ⇒ Net::HTTP::Response
readonly
The raw HTTP response from the Zabbix API.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Return the contents of
key
within theresult
key ornil
if an error. -
#array? ⇒ true, false
Is the contents of the first element of the
result
key an Array?. -
#boolean? ⇒ true, false
Is the contents of the
result
key eithertrue
orfalse
?. -
#empty? ⇒ true, false
Is the
result
key empty?. -
#error? ⇒ true, false
Was the response an error? This will return
true
if. -
#error_code ⇒ nil, Fixnum
Returns the error code of a Zabbix error or
nil
if this wasn’t an error. -
#error_message ⇒ String?
Return an error message or
nil
if this wasn’t an error. -
#error_type ⇒ nil, String
Returns the Zabbix type of the error or
nil
if this wasn’t an error. -
#first ⇒ Object
Return the
first
element of theresult
key ornil
if an error. -
#has_data? ⇒ Boolean
Does this response “have data” in the sense that.
-
#hash? ⇒ true, false
Is the contents of the first element of the
result
key a Hash?. -
#initialize(http_response) ⇒ Response
constructor
Wrap a
Net::HTTP::Response
. -
#non_200? ⇒ true, false
Was the response not a 200?.
-
#parsed ⇒ Hash
The parsed JSON body.
-
#result ⇒ Object
The contents of the
result
key. -
#string? ⇒ true, false
Is the contents of the
result
key a String?. -
#success? ⇒ true, false
Was this response successful? Successful responses must.
-
#zabbix_error? ⇒ true, false
Was the response a Zabbix error, implying a 200 with an
error
key.
Constructor Details
#initialize(http_response) ⇒ Response
Wrap a Net::HTTP::Response
.
21 22 23 24 25 |
# File 'lib/rubix/response.rb', line 21 def initialize(http_response) @http_response = http_response @body = http_response.body @code = http_response.code.to_i end |
Instance Attribute Details
#body ⇒ String (readonly)
Returns the raw HTTP response body.
16 17 18 |
# File 'lib/rubix/response.rb', line 16 def body @body end |
#code ⇒ Fixnum (readonly)
Returns the raw HTTP response code.
13 14 15 |
# File 'lib/rubix/response.rb', line 13 def code @code end |
#http_response ⇒ Net::HTTP::Response (readonly)
Returns the raw HTTP response from the Zabbix API.
10 11 12 |
# File 'lib/rubix/response.rb', line 10 def http_response @http_response end |
Instance Method Details
#[](key) ⇒ Object
Return the contents of key
within the result
key or nil
if an error.
133 134 135 136 |
# File 'lib/rubix/response.rb', line 133 def [] key return if error? result[key] end |
#array? ⇒ true, false
Is the contents of the first element of the result
key an Array?
174 175 176 177 |
# File 'lib/rubix/response.rb', line 174 def array? return false if error? result.is_a?(Array) && result.size > 0 && result.first end |
#boolean? ⇒ true, false
Is the contents of the result
key either true
or false
?
190 191 192 193 |
# File 'lib/rubix/response.rb', line 190 def boolean? return false if error? result == true || result == false end |
#empty? ⇒ true, false
Is the result
key empty?
148 149 150 151 |
# File 'lib/rubix/response.rb', line 148 def empty? return true unless result result.empty? end |
#error? ⇒ true, false
Was the response an error? This will return true
if
-
the response was not a 200
-
the response was a 200 and contains an
error
key
64 65 66 |
# File 'lib/rubix/response.rb', line 64 def error? non_200? || (parsed.is_a?(Hash) && parsed['error']) end |
#error_code ⇒ nil, Fixnum
Returns the error code of a Zabbix error or nil
if this wasn’t an error.
90 91 92 93 |
# File 'lib/rubix/response.rb', line 90 def error_code return unless error? (non_200? ? code : parsed['error']['code'].to_i) rescue 0 end |
#error_message ⇒ String?
Return an error message or nil
if this wasn’t an error.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/rubix/response.rb', line 106 def return unless error? begin if non_200? "Could not get a 200 response from the Zabbix API. Further details are unavailable." else = (parsed['error']['message'] || '').gsub(/\.$/, '') stripped_data = (parsed['error']['data'] || '').gsub(/^\[.*?\] /, '') [, stripped_data].map(&:strip).reject(&:empty?).join(', ') end rescue => e "No details available." end end |
#error_type ⇒ nil, String
Returns the Zabbix type of the error or nil
if this wasn’t an error.
98 99 100 101 |
# File 'lib/rubix/response.rb', line 98 def error_type return unless error? (non_200? ? "Non-200 Error" : parsed['error']['message']) rescue 'Unknown Error' end |
#first ⇒ Object
Return the first
element of the result
key or nil
if an error.
140 141 142 143 |
# File 'lib/rubix/response.rb', line 140 def first return if error? result.first end |
#has_data? ⇒ Boolean
Does this response “have data” in the sense that
-
it is a successful response (see
Rubix::Response#success?
) -
it has a
result
key which is not empty
157 158 159 |
# File 'lib/rubix/response.rb', line 157 def has_data? success? && (!empty?) end |
#hash? ⇒ true, false
Is the contents of the first element of the result
key a Hash?
165 166 167 168 |
# File 'lib/rubix/response.rb', line 165 def hash? return false if error? result.is_a?(Hash) && result.size > 0 && result.first.last end |
#non_200? ⇒ true, false
Was the response not a 200?
54 55 56 |
# File 'lib/rubix/response.rb', line 54 def non_200? code != 200 end |
#parsed ⇒ Hash
The parsed JSON body.
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rubix/response.rb', line 34 def parsed return @parsed if @parsed if non_200? @parsed = {} else begin @parsed = MultiJson.load(@body) if @code == 200 rescue MultiJson::DecodeError => e @parsed = {} end end end |
#result ⇒ Object
The contents of the result
key. Returns nil
if an error.
126 127 128 129 |
# File 'lib/rubix/response.rb', line 126 def result return if error? parsed['result'] end |
#string? ⇒ true, false
Is the contents of the result
key a String?
182 183 184 185 |
# File 'lib/rubix/response.rb', line 182 def string? return false if error? result.is_a?(String) && result.size > 0 end |
#success? ⇒ true, false
Was this response successful? Successful responses must
-
have a 200 response code
-
not have an
error
key in the response
74 75 76 |
# File 'lib/rubix/response.rb', line 74 def success? !error? end |
#zabbix_error? ⇒ true, false
Was the response a Zabbix error, implying a 200 with an error
key.
82 83 84 |
# File 'lib/rubix/response.rb', line 82 def zabbix_error? code == 200 && error? end |