zabx 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,17 +2,13 @@ module Zabx
2
2
  class Error < StandardError; end
3
3
 
4
4
  class JsonRpcError < Error
5
- attr_reader :message, :code, :data
5
+ attr_reader :code
6
6
 
7
7
  def initialize(message, code, data = nil)
8
- @message = message
8
+ message = "#{code}: #{message}"
9
+ message += " #{data}" unless data.nil?
10
+ super message
9
11
  @code = code
10
- @data = data
11
- end
12
-
13
- def to_s
14
- output = "#{code}: #{message}"
15
- output += " #{data}" unless data.nil?
16
12
  end
17
13
  end
18
14
 
@@ -20,7 +20,23 @@ module Zabx
20
20
  @options = options
21
21
  end
22
22
 
23
- def single_request(method, args)
23
+ def invoke(method, args)
24
+ response = request(method, args)
25
+ status_code_check(response)
26
+ parse_response(response.body)
27
+ end
28
+
29
+ def method_missing(method, *args)
30
+ if args.length == 1 && args[0].is_a?(Hash)
31
+ invoke(method, args[0])
32
+ else
33
+ invoke(method, args)
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def request(method, args)
24
40
  namespaced_method = @namespace.nil? ? method : "#{@namespace}.#{method}"
25
41
  body = @options.merge({
26
42
  :jsonrpc => JSON_RPC_VERSION,
@@ -28,14 +44,12 @@ module Zabx
28
44
  :params => args,
29
45
  :id => UUIDTools::UUID.random_create.to_i
30
46
  })
31
- begin
32
- @connection.post @url, body
33
- rescue Faraday::ConnectionFailed => exception
34
- raise ConnectionError, exception.message
35
- end
47
+ @connection.post @url, body
48
+ rescue Faraday::ConnectionFailed => exception
49
+ raise ConnectionError, exception.message
36
50
  end
37
51
 
38
- def single_response(response)
52
+ def parse_response(response)
39
53
  error_check response
40
54
  response["result"]
41
55
  end
@@ -55,19 +69,5 @@ module Zabx
55
69
  raise HttpError, "HTTP server error with status code #{response.status}"
56
70
  end
57
71
  end
58
-
59
- def invoke(method, args)
60
- response = single_request(method, args)
61
- status_code_check(response)
62
- single_response(response.body)
63
- end
64
-
65
- def missing_method(method, *args)
66
- if args.length == 1 && args[0].is_a?(Hash)
67
- invoke(method, args[0])
68
- else
69
- invoke(method, args)
70
- end
71
- end
72
72
  end
73
73
  end
@@ -2,24 +2,6 @@ require 'zabx/json_rpc'
2
2
 
3
3
  module Zabx
4
4
  class Api
5
- class Query
6
- def initialize(url, options = {})
7
- @rpc = JsonRpc.new(url, options)
8
- end
9
-
10
- def invoke(method, args)
11
- @rpc.invoke(method, args)
12
- end
13
-
14
- def method_missing(method, *args)
15
- if args.length == 1 && args[0].is_a?(Hash)
16
- invoke(method, args[0])
17
- else
18
- invoke(method, args)
19
- end
20
- end
21
- end
22
-
23
5
  def initialize(url, options)
24
6
  @url = url
25
7
  @username = options.delete(:username)
@@ -27,24 +9,24 @@ module Zabx
27
9
  end
28
10
 
29
11
  def auth
30
- @auth ||= query(:namespace => :user).login(:user => @username, :password => @password)
12
+ @auth ||= rpc(:namespace => :user).login(:user => @username, :password => @password)
31
13
  end
32
14
 
33
15
  def invoke(method, args = {})
34
- query(:auth => auth).invoke(method, args)
16
+ rpc(:auth => auth).invoke(method, args)
35
17
  end
36
18
 
37
19
  def apiinfo
38
- query :namespace => "apiinfo"
20
+ rpc :namespace => "apiinfo"
39
21
  end
40
22
 
41
- def method_missing(method, *args)
42
- query(:auth => auth, :namespace => method)
23
+ def method_missing method
24
+ rpc(:auth => auth, :namespace => method)
43
25
  end
44
26
 
45
27
  private
46
- def query(options = {})
47
- Query.new(@url, options)
28
+ def rpc(options = {})
29
+ JsonRpc.new(@url, options)
48
30
  end
49
31
  end
50
32
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zabx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-08-17 00:00:00.000000000 Z
12
+ date: 2014-08-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json