zbx 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +22 -3
  2. data/lib/zbx/api.rb +34 -12
  3. data/lib/zbx/version.rb +1 -1
  4. metadata +1 -1
data/README.md CHANGED
@@ -1,6 +1,9 @@
1
- # Zbx
1
+ # ZBX
2
2
 
3
- TODO: Write a gem description
3
+ This project is inspired by the following repos:
4
+
5
+ - [zabbixapi](https://github.com/vadv/zabbixapi)
6
+ - [pyzabbix](https://github.com/lukecyca/pyzabbix).
4
7
 
5
8
  ## Installation
6
9
 
@@ -18,7 +21,23 @@ Or install it yourself as:
18
21
 
19
22
  ## Usage
20
23
 
21
- TODO: Write usage instructions here
24
+ ```ruby
25
+ require 'zbx'
26
+
27
+ # the following code are doing the same thing ,that is get a host whose id is 10160
28
+
29
+ ZBX::API.new username, password, api_url do
30
+ host.get hostids: 10160
31
+ end
32
+
33
+ ZBX::API.new username, password, api_url do
34
+ host do
35
+ get hostids: 10160
36
+ end
37
+ end
38
+
39
+ ZBX::API.new(username, password, api_url).host.get hostids: 10160
40
+ ```
22
41
 
23
42
  ## Contributing
24
43
 
data/lib/zbx/api.rb CHANGED
@@ -12,21 +12,28 @@ module ZBX
12
12
  #
13
13
  # ZBX::API.new(user, pass, api_url).host.get(hostids: 1)
14
14
 
15
- def initialize user, pass, url, &b
15
+ def initialize user=nil, pass=nil, url=nil, &b
16
16
  @auth = nil
17
17
  @user = user
18
18
  @pass = pass
19
- @uri = URI.parse(url)
19
+ @api_url = url
20
+ instance_eval(&b) if block_given?
21
+ end
20
22
 
21
- # Our http client. Borrowed from zabbixapi(https://github.com/vadv/zabbixapi)
22
- @http ||= Net::HTTP.new @uri.host, @uri.port
23
- if @uri.port == 443
24
- @http.use_ssl = true
25
- @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
26
- end
23
+ def username! username
24
+ @user, @auth = username, nil
25
+ end
26
+ alias_method :username=, :username!
27
27
 
28
- instance_eval(&b) if block_given?
28
+ def password! password
29
+ @pass, @auth = password, nil
30
+ end
31
+ alias_method :password=, :password!
32
+
33
+ def api_url! api_url
34
+ @api_url, @auth = api_url, nil
29
35
  end
36
+ alias_method :api_url=, :api_url!
30
37
 
31
38
  def request method, params={}
32
39
  # in any api request except `user.login`
@@ -41,7 +48,8 @@ module ZBX
41
48
  _request(method: method,
42
49
  params: params,
43
50
  auth: auth!,
44
- id: _id, jsonrpc: '2.0')
51
+ id: _id,
52
+ jsonrpc: '2.0')
45
53
  end
46
54
 
47
55
  def auth!
@@ -60,12 +68,26 @@ module ZBX
60
68
 
61
69
  private
62
70
 
71
+ def _http
72
+ # Our http client. Borrowed from zabbixapi(https://github.com/vadv/zabbixapi)
73
+ @http ||= Net::HTTP.new _uri.host, _uri.port
74
+ if _uri.port == 443
75
+ @http.use_ssl = true
76
+ @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
77
+ end
78
+ @http
79
+ end
80
+
81
+ def _uri
82
+ @uri ||= URI.parse(@api_url)
83
+ end
84
+
63
85
  def _request options={}
64
86
  # send post request
65
- req = Net::HTTP::Post.new @uri.request_uri
87
+ req = Net::HTTP::Post.new _uri.request_uri
66
88
  req.add_field('Content-Type', 'application/json-rpc')
67
89
  req.body = options.to_json
68
- JSON.parse(@http.request(req).body)['result']
90
+ JSON.parse(_http.request(req).body)['result']
69
91
  rescue
70
92
  nil
71
93
  end
data/lib/zbx/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module ZBX
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zbx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: