Class: ZBX::API

Inherits:
Object
  • Object
show all
Defined in:
lib/zbx/api.rb

Instance Method Summary collapse

Constructor Details

#initialize(user = nil, password = nil, api_url = nil, &b) ⇒ API

username, password, and zabbix api url if block is given, it will be evaluated in the object that is to say, you can write something like this.

ZBX::API.new(user, password, api_url) do

host.get(hostids: 1)

end

– this is equal to the following –

ZBX::API.new(user, password, api_url).host.get(hostids: 1)



15
16
17
18
19
20
# File 'lib/zbx/api.rb', line 15

def initialize user=nil, password=nil, api_url=nil, &b
  @auth = nil
  @user, @password, @http = user, password, HttpClient.new(api_url)

  instance_eval(&b) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, &b) ⇒ Object



50
51
52
# File 'lib/zbx/api.rb', line 50

def method_missing m, &b
  Entity.new m, self, &b
end

Instance Method Details

#auth!Object



46
47
48
# File 'lib/zbx/api.rb', line 46

def auth!
  @auth ||= user. user: @user, password: @password
end

#request(method, params = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/zbx/api.rb', line 28

def request method, params={}
  # in any api request except `user.login`
  # we add the following options:
  #
  # - params[:output] = "extend"
  # - id              = a random id
  # - jsonrpc         = '2.0'
  # - auth            = an authentication token

  params[:output] = "extend" unless params[:output] or params["output"]
  opts = {method: method,
          params: params,
          id: _id,
          jsonrpc: '2.0'}
  opts[:auth] = auth! unless method == 'user.login'
  @http.request opts
end

#set(option = {}) ⇒ Object



22
23
24
25
26
# File 'lib/zbx/api.rb', line 22

def set option={}
  @user, @auth = option[:user], nil if option[:user]
  @password, @auth = option[:password], nil if option[:password]
  @http.api_url = option[:api_url] if option[:api_url]
end