Class: Zenvelope
- Inherits:
-
Object
- Object
- Zenvelope
- Defined in:
- lib/zenvelope.rb
Overview
API wrapper class
Defined Under Namespace
Classes: Action
Constant Summary collapse
- JSON_RPC_VERSION =
'2.0'
- RANDOM_ID_SEED =
100_000
- VERSION =
'0.2.1'
Instance Attribute Summary collapse
-
#auth ⇒ Object
readonly
Returns the value of attribute auth.
Instance Method Summary collapse
-
#initialize(url = 'http://localhost') ⇒ Zenvelope
constructor
A new instance of Zenvelope.
- #login(creds = { user: 'Admin', password: 'zabbix' }) ⇒ Object
- #method_missing(method) ⇒ Object
- #query(method, parameters) ⇒ Object
Constructor Details
#initialize(url = 'http://localhost') ⇒ Zenvelope
Returns a new instance of Zenvelope.
24 25 26 27 |
# File 'lib/zenvelope.rb', line 24 def initialize(url = 'http://localhost') @auth = nil @url = url.chomp('/') + '/api_jsonrpc.php' end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
Instance Attribute Details
#auth ⇒ Object (readonly)
Returns the value of attribute auth.
10 11 12 |
# File 'lib/zenvelope.rb', line 10 def auth @auth end |
Instance Method Details
#login(creds = { user: 'Admin', password: 'zabbix' }) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/zenvelope.rb', line 29 def login(creds = { user: 'Admin', password: 'zabbix' }) results = query('user.login', creds) # return the auth id if successful login, otherwise return false @auth = if results.is_a?(String) && /[a-zA-Z0-9]{32}/.match(results) results else false end end |
#query(method, parameters) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/zenvelope.rb', line 44 def query(method, parameters) payload = { id: rand(RANDOM_ID_SEED), jsonrpc: JSON_RPC_VERSION, method: method, params: parameters } payload[:auth] = @auth unless method.include? 'apiinfo' begin response = JSON.parse(request(payload), symbolize_names: true) rescue response = { error: { message: 'Unable to parse JSON response', data: "Please check that the path #{@url} is correct." } } end if response.key? :error response else response[:result] end end |