Class: SkyZabbix::Jsonrpc

Inherits:
Object
  • Object
show all
Defined in:
lib/sky_zabbix/jsonrpc.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =

json-rpc version

'2.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, logger: nil) ⇒ Jsonrpc

Returns a new instance of Jsonrpc.


8
9
10
11
# File 'lib/sky_zabbix/jsonrpc.rb', line 8

def initialize(uri, logger: nil)
  @uri = uri
  @logger = logger
end

Instance Attribute Details

#tokenObject

Returns the value of attribute token


13
14
15
# File 'lib/sky_zabbix/jsonrpc.rb', line 13

def token
  @token
end

Instance Method Details

#batch(buildeds) ⇒ Array<Any|Error|nil>

XXX: エラー処理はこれでいい?

Examples:

Return values.

rpc.batch(
  rpc.build('a', 'A'),
  rpc.build('b', 'B', notification: true),
  rpc.build('c', 'C'),
) # => [{value: 'response of A'}, nil, {value: 'response of A'}]

Raise error.

rpc.batch(
  rpc.build('a', 'A'),
  rpc.build('b', 'B', notification: true),
  rpc.build('c', 'Invalid Params'),
) # => Error::BatchError.
#  Can get response of 'a' from ex.result

Parameters:

  • buildeds (Array<Hash>)

    is Array of result of 'build' method.

Returns:


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/sky_zabbix/jsonrpc.rb', line 54

def batch(buildeds)
  uri = URI.parse(@uri)
  resp = do_req(uri, buildeds)
  body = JSON.parse(resp.body)

  result = []
  errors = []
  buildeds.each do |b|
    id = b[:id]
    a = body.find{|x|x['id'] == id}

    r =
      if a.nil?
        nil
      elsif a['error']
        errors.push(Error.new(a))
        nil
      else
        a['result']
      end
    result.push(r)
  end

  unless errors.empty?
    raise Error::BatchError.new(errors, result)
  end
  return result
end

#build(method, params, notification: false) ⇒ Object

Parameters:

  • method (String)

    is json-rpc method name.

  • params (Any?)

    is json-rpc parameters.

  • notification (Boolean) (defaults to: false)

86
87
88
89
90
91
92
93
94
95
96
# File 'lib/sky_zabbix/jsonrpc.rb', line 86

def build(method, params, notification: false)
  res = {
    jsonrpc: VERSION,
    method:  method,
    params:  params,
    auth:    @token,
  }
  res[:id] = id_gen unless notification

  return res
end

#post(method, params, notification: false) ⇒ Object

Parameters:

  • method (String)

    is json-rpc method name.

  • params (Any?)

    is json-rpc parameters.

  • notification (Boolean) (defaults to: false)

18
19
20
# File 'lib/sky_zabbix/jsonrpc.rb', line 18

def post(method, params, notification: false)
  request(build(method, params, notification: notification))
end

#request(builded) ⇒ Any?

Return result of response

Parameters:

  • builded (Hash{String => Any})

    is result of 'build' method.

Returns:

  • (Any?)

    return result of response


24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sky_zabbix/jsonrpc.rb', line 24

def request(builded)
  uri = URI.parse(@uri)

  resp = do_req(uri, builded)

  return nil unless builded[:id] # when notification

  # Parse and error handling
  body = JSON.parse(resp.body)
  raise Error.create(body) if body['error']

  return body['result']
end