Class: Rubix::Response

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(http_response) ⇒ Response

Wrap a Net::HTTP::Response.

Parameters:

  • http_response (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

#bodyString (readonly)

Returns the raw HTTP response body.

Returns:

  • (String)

    the raw HTTP response body



16
17
18
# File 'lib/rubix/response.rb', line 16

def body
  @body
end

#codeFixnum (readonly)

Returns the raw HTTP response code.

Returns:

  • (Fixnum)

    the raw HTTP response code



13
14
15
# File 'lib/rubix/response.rb', line 13

def code
  @code
end

#http_responseNet::HTTP::Response (readonly)

Returns the raw HTTP response from the Zabbix API.

Returns:

  • (Net::HTTP::Response)

    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?

Returns:

  • (true, false)


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?

Returns:

  • (true, 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?

Returns:

  • (true, false)


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

Returns:

  • (true, false)


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

def error?
  non_200? || (parsed.is_a?(Hash) && parsed['error'])
end

#error_codenil, Fixnum

Returns the error code of a Zabbix error or nil if this wasn’t an error.

Returns:

  • (nil, Fixnum)


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_messageString?

Return an error message or nil if this wasn’t an error.

Returns:

  • (String, nil)


106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rubix/response.rb', line 106

def error_message
  return unless error?
  begin
    if non_200?
      "Could not get a 200 response from the Zabbix API.  Further details are unavailable."
    else
      stripped_message = (parsed['error']['message'] || '').gsub(/\.$/, '')
      stripped_data = (parsed['error']['data'] || '').gsub(/^\[.*?\] /, '')
      [stripped_message, stripped_data].map(&:strip).reject(&:empty?).join(', ')
    end
  rescue => e
    "No details available."
  end
end

#error_typenil, String

Returns the Zabbix type of the error or nil if this wasn’t an error.

Returns:

  • (nil, String)


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

#firstObject

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

Returns:

  • (Boolean)


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?

Returns:

  • (true, false)


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?

Returns:

  • (true, false)


54
55
56
# File 'lib/rubix/response.rb', line 54

def non_200?
  code != 200
end

#parsedHash

The parsed JSON body.

Returns:

  • (Hash)


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 = JSON.parse(@body) if @code == 200
    rescue JSON::ParserError => e
      @parsed = {}
    end
  end
end

#resultObject

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?

Returns:

  • (true, false)


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

Returns:

  • (true, false)


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.

Returns:

  • (true, false)


82
83
84
# File 'lib/rubix/response.rb', line 82

def zabbix_error?
  code == 200 && error?
end