Class: Nagios::API::Interface
- Inherits:
-
Object
- Object
- Nagios::API::Interface
show all
- Defined in:
- lib/nagios/api/interface.rb
Defined Under Namespace
Classes: BackendAPIError, HTTPError
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(args = {}) ⇒ Interface
Returns a new instance of Interface.
11
12
13
14
15
16
|
# File 'lib/nagios/api/interface.rb', line 11
def initialize(args = {})
@base_url = args[:base_url]
@username = args[:username]
@password = args[:password]
@use_cache = args[:use_cache]
end
|
Instance Attribute Details
#base_url ⇒ Object
Returns the value of attribute base_url.
9
10
11
|
# File 'lib/nagios/api/interface.rb', line 9
def base_url
@base_url
end
|
#password ⇒ Object
Returns the value of attribute password.
9
10
11
|
# File 'lib/nagios/api/interface.rb', line 9
def password
@password
end
|
#use_cache ⇒ Object
Returns the value of attribute use_cache.
9
10
11
|
# File 'lib/nagios/api/interface.rb', line 9
def use_cache
@use_cache
end
|
#username ⇒ Object
Returns the value of attribute username.
9
10
11
|
# File 'lib/nagios/api/interface.rb', line 9
def username
@username
end
|
Instance Method Details
#authentication_options ⇒ Object
38
39
40
|
# File 'lib/nagios/api/interface.rb', line 38
def authentication_options
!username.nil? ? { username: username, password: password } : {}
end
|
#memcached ⇒ Object
26
27
28
|
# File 'lib/nagios/api/interface.rb', line 26
def memcached
@memcached ||= Memcached.new("localhost:11211")
end
|
#post(path, params) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/nagios/api/interface.rb', line 42
def post(path, params)
args = { url: base_url + path, headers: { 'Accept' => 'application/json', 'Content-Type' => 'application/json' } }
args.merge!(authentication_options)
result = CurbFu.post(args, params.to_json)
if result.success?
data = JSON.parse(result.body)
if data['success']
return data['content']
else
raise BackendAPIError, data['content']
end
else
raise HTTPError, "Error communicating with API server: #{result.status}"
end
end
|
#query(path) ⇒ Object
18
19
20
21
22
23
24
|
# File 'lib/nagios/api/interface.rb', line 18
def query(path)
result = query_from_api(path)
return result unless use_cache
save_to_cache(path, result) if result
result ||= query_from_cache(path)
end
|
#query_from_api(path) ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/nagios/api/interface.rb', line 60
def query_from_api(path)
args = { url: base_url + path, headers: { Accept: 'application/json' } }
args.merge!(authentication_options)
result = CurbFu.get(args)
if result.success?
data = JSON.parse(result.body)
if data['success']
return data['content']
else
raise BackendAPIError
end
else
raise HTTPError, "Error communicating with API server: #{result.status}"
end
end
|
#query_from_cache(path) ⇒ Object
34
35
36
|
# File 'lib/nagios/api/interface.rb', line 34
def query_from_cache(path)
memcached.get(path) rescue nil
end
|
#save_to_cache(path, data) ⇒ Object
30
31
32
|
# File 'lib/nagios/api/interface.rb', line 30
def save_to_cache(path, data)
memcached.set path, data, 0
end
|