class ZBX::API

Public Class Methods

new(user=nil, password=nil, api_url=nil, &b) click to toggle source

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)

# 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

Public Instance Methods

auth!() click to toggle source
# File lib/zbx/api.rb, line 46
def auth!
  @auth ||= user.login user: @user, password: @password
end
method_missing(m, &b) click to toggle source
# File lib/zbx/api.rb, line 50
def method_missing m, &b
  Entity.new m, self, &b
end
request(method, params={}) click to toggle source
# 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={}) click to toggle source
# 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

Private Instance Methods

_id() click to toggle source
# File lib/zbx/api.rb, line 56
def _id
  rand(100000)
end