Class: Zabbix::Client::Method

Inherits:
Object
  • Object
show all
Defined in:
lib/zabbix/client/client.rb

Constant Summary collapse

JSON_RPC_VERSION =
'2.0'
JSON_RPC_REQUEST_ID =
1
LOGIN_METHOD =
'user.login'
LOGOUT_METHOD =
'user.logout'
UNAUTHENTICATED_METHODS =
[
  'user.login',
  'apiinfo.version',
]
DEFAULT_HEADERS =
{
  'Content-Type' => 'application/json-rpc'
}

Instance Method Summary collapse

Constructor Details

#initialize(prefix, client) ⇒ Method

Returns a new instance of Method.



18
19
20
21
# File 'lib/zabbix/client/client.rb', line 18

def initialize(prefix, client)
  @prefix = prefix
  @client = client
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/zabbix/client/client.rb', line 23

def method_missing(method_name, *args, &block)
  validate_args(args)

  method   = "#{@prefix}.#{method_name}"
  params   = args[0] || []
  options  = args[1] || {}
  response = query(method, params, options)

  if (error = response['error'])
    error = error.merge('method' => method, 'params' => params)
    raise Zabbix::Client::Error.new(error)
  end

  result = response['result']

  case method
  when LOGIN_METHOD
    @client.auth = result
  when LOGOUT_METHOD
    @client.auth = nil
  end

  if block
    block.call(result)
  else
    result
  end
end