Class: Rubix::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/rubix/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#bodyObject (readonly)

Returns the value of attribute body.



7
8
9
# File 'lib/rubix/response.rb', line 7

def body
  @body
end

#codeObject (readonly)

Returns the value of attribute code.



7
8
9
# File 'lib/rubix/response.rb', line 7

def code
  @code
end

#http_responseObject (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

Returns:

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

Returns:

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

Returns:

  • (Boolean)


95
96
97
# File 'lib/rubix/response.rb', line 95

def empty?
  result.empty?
end

#error?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/rubix/response.rb', line 40

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

#error_codeObject



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_messageObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rubix/response.rb', line 58

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_typeObject



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

#firstObject



90
91
92
93
# File 'lib/rubix/response.rb', line 90

def first
  return if error?
  result.first
end

#has_data?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/rubix/response.rb', line 99

def has_data?
  success? && (!empty?)
end

#hash?Boolean

Returns:

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

Returns:

  • (Boolean)


36
37
38
# File 'lib/rubix/response.rb', line 36

def non_200?
  code != 200
end

#parsedObject

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

#resultObject

Inspecting contents



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

def result
  parsed['result']
end

#string?Boolean

Returns:

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

Returns:

  • (Boolean)


73
74
75
# File 'lib/rubix/response.rb', line 73

def success?
  !error?
end

#zabbix_error?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/rubix/response.rb', line 44

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