Class: Rubix::Response
- Inherits:
-
Object
- Object
- Rubix::Response
- Defined in:
- lib/rubix/response.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#http_response ⇒ Object
readonly
Returns the value of attribute http_response.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #array? ⇒ Boolean
- #boolean? ⇒ Boolean
- #empty? ⇒ Boolean
- #error? ⇒ Boolean
- #error_code ⇒ Object
- #error_message ⇒ Object
- #error_type ⇒ Object
- #first ⇒ Object
- #has_data? ⇒ Boolean
- #hash? ⇒ Boolean
-
#initialize(http_response) ⇒ Response
constructor
A new instance of Response.
-
#non_200? ⇒ Boolean
Error Handling.
-
#parsed ⇒ Object
Parsing.
-
#result ⇒ Object
Inspecting contents.
- #string? ⇒ Boolean
- #success? ⇒ Boolean
- #zabbix_error? ⇒ Boolean
Constructor Details
#initialize(http_response) ⇒ Response
Returns a new instance of Response.
9 10 11 12 13 |
# File 'lib/rubix/response.rb', line 9 def initialize(http_response) @http_response = http_response @body = http_response.body @code = http_response.code.to_i end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
7 8 9 |
# File 'lib/rubix/response.rb', line 7 def body @body end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
7 8 9 |
# File 'lib/rubix/response.rb', line 7 def code @code end |
#http_response ⇒ Object (readonly)
Returns the value of attribute http_response.
7 8 9 |
# File 'lib/rubix/response.rb', line 7 def http_response @http_response end |
Instance Method Details
#[](key) ⇒ Object
85 86 87 88 |
# File 'lib/rubix/response.rb', line 85 def [] key return if error? result[key] end |
#array? ⇒ Boolean
108 109 110 111 |
# File 'lib/rubix/response.rb', line 108 def array? return false if error? result.is_a?(Array) && result.size > 0 && result.first end |
#boolean? ⇒ Boolean
118 119 120 121 |
# File 'lib/rubix/response.rb', line 118 def boolean? return false if error? result == true || result == false end |
#empty? ⇒ Boolean
95 96 97 |
# File 'lib/rubix/response.rb', line 95 def empty? result.empty? end |
#error? ⇒ Boolean
40 41 42 |
# File 'lib/rubix/response.rb', line 40 def error? non_200? || (parsed.is_a?(Hash) && parsed['error']) end |
#error_code ⇒ Object
48 49 50 51 |
# File 'lib/rubix/response.rb', line 48 def error_code return unless error? (non_200? ? code : parsed['error']['code'].to_i) rescue 0 end |
#error_message ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/rubix/response.rb', line 58 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 ⇒ Object
53 54 55 56 |
# File 'lib/rubix/response.rb', line 53 def error_type return unless error? (non_200? ? "Non-200 Error" : parsed['error']['message']) rescue 'Unknown Error' end |
#first ⇒ Object
90 91 92 93 |
# File 'lib/rubix/response.rb', line 90 def first return if error? result.first end |
#has_data? ⇒ Boolean
99 100 101 |
# File 'lib/rubix/response.rb', line 99 def has_data? success? && (!empty?) end |
#hash? ⇒ Boolean
103 104 105 106 |
# File 'lib/rubix/response.rb', line 103 def hash? return false if error? result.is_a?(Hash) && result.size > 0 && result.first.last end |
#non_200? ⇒ Boolean
Error Handling
36 37 38 |
# File 'lib/rubix/response.rb', line 36 def non_200? code != 200 end |
#parsed ⇒ Object
Parsing
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rubix/response.rb', line 19 def parsed return @parsed if @parsed if non_200? @parsed = {} else begin @parsed = JSON.parse(@body) if @code == 200 rescue JSON::ParserError => e @parsed = {} end end end |
#result ⇒ Object
Inspecting contents
81 82 83 |
# File 'lib/rubix/response.rb', line 81 def result parsed['result'] end |
#string? ⇒ Boolean
113 114 115 116 |
# File 'lib/rubix/response.rb', line 113 def string? return false if error? result.is_a?(String) && result.size > 0 end |
#success? ⇒ Boolean
73 74 75 |
# File 'lib/rubix/response.rb', line 73 def success? !error? end |
#zabbix_error? ⇒ Boolean
44 45 46 |
# File 'lib/rubix/response.rb', line 44 def zabbix_error? code == 200 && error? end |