zbx 0.1.1 → 1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -39,9 +39,8 @@ end
39
39
  ZBX::API.new(username, password, api_url).host.get hostids: 10160
40
40
 
41
41
  ZBX::API.new do
42
- api_url! api_url
43
- username! user
44
- password! password
42
+ set username: 'username', password: password
43
+ set api_url: 'http://api_url'
45
44
  host.get hostids: 10160
46
45
  end
47
46
  ```
data/Rakefile CHANGED
@@ -1 +1,7 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec')
5
+
6
+ # If you want to make this the default task
7
+ task :default => :spec
data/lib/zbx.rb CHANGED
@@ -1,9 +1,10 @@
1
1
  require "zbx/version"
2
2
  require 'zbx/entity'
3
+ require 'zbx/http_client'
3
4
  require 'zbx/api'
5
+
4
6
  require 'json'
5
7
  require 'net/http'
6
8
 
7
9
  module ZBX
8
10
  end
9
-
data/lib/zbx/api.rb CHANGED
@@ -14,22 +14,15 @@ module ZBX
14
14
 
15
15
  def initialize user=nil, pass=nil, url=nil, &b
16
16
  @auth = nil
17
- @user = user
18
- @pass = pass
19
- @api_url = url
20
- instance_eval(&b) if block_given?
21
- end
22
-
23
- def username! username
24
- @user, @auth = username, nil
25
- end
17
+ @user, @pass, @http = user, pass, HttpClient.new url
26
18
 
27
- def password! password
28
- @pass, @auth = password, nil
19
+ instance_eval(&b) if block_given?
29
20
  end
30
21
 
31
- def api_url! api_url
32
- @api_url, @auth = api_url, nil
22
+ def set option={}
23
+ @user, @auth = option[:username], nil if option[:username]
24
+ @pass, @auth = option[:password], nil if option[:password]
25
+ @http.api_url = option[:api_url] if option[:api_url]
33
26
  end
34
27
 
35
28
  def request method, params={}
@@ -42,18 +35,18 @@ module ZBX
42
35
  # - auth = an authentication token
43
36
 
44
37
  params[:output] ||= "extend"
45
- _request(method: method,
46
- params: params,
47
- auth: auth!,
48
- id: _id,
49
- jsonrpc: '2.0')
38
+ @http.request(method: method,
39
+ params: params,
40
+ auth: auth!,
41
+ id: _id,
42
+ jsonrpc: '2.0')
50
43
  end
51
44
 
52
45
  def auth!
53
- @auth ||= _request(method: "user.login",
54
- params: {user: @user, password: @pass},
55
- id: _id,
56
- jsonrpc: '2.0')
46
+ @auth ||= @http.request(method: "user.login",
47
+ params: {user: @user, password: @pass},
48
+ id: _id,
49
+ jsonrpc: '2.0')
57
50
 
58
51
  # if login failed, we raise an auth error !
59
52
  @auth or raise "Authentication failed"
@@ -65,30 +58,6 @@ module ZBX
65
58
 
66
59
  private
67
60
 
68
- def _http
69
- # Our http client. Borrowed from zabbixapi(https://github.com/vadv/zabbixapi)
70
- @http ||= Net::HTTP.new _uri.host, _uri.port
71
- if _uri.port == 443
72
- @http.use_ssl = true
73
- @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
74
- end
75
- @http
76
- end
77
-
78
- def _uri
79
- @uri ||= URI.parse(@api_url)
80
- end
81
-
82
- def _request options={}
83
- # send post request
84
- req = Net::HTTP::Post.new _uri.request_uri
85
- req.add_field('Content-Type', 'application/json-rpc')
86
- req.body = options.to_json
87
- JSON.parse(_http.request(req).body)['result']
88
- rescue
89
- nil
90
- end
91
-
92
61
  def _id
93
62
  rand(100000)
94
63
  end
@@ -0,0 +1,42 @@
1
+ module ZBX
2
+ # HttpClient
3
+ # send http request for api
4
+
5
+ class HttpClient
6
+ def initialize api_url=nil
7
+ @api_url = api_url
8
+ end
9
+
10
+ def request options={}
11
+ # send post request
12
+ req = Net::HTTP::Post.new uri.request_uri
13
+ req.add_field('Content-Type', 'application/json-rpc')
14
+ req.body = options.to_json
15
+ JSON.parse(http.request(req).body)['result']
16
+ rescue
17
+ nil
18
+ end
19
+
20
+ def http
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
27
+ @http
28
+ end
29
+
30
+ def uri
31
+ @uri ||= URI.parse(@api_url)
32
+ end
33
+
34
+ def api_url= url
35
+ @api_url, @uri, @http = url, nil, nil
36
+ self
37
+ end
38
+ end
39
+ end
40
+
41
+
42
+
data/lib/zbx/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module ZBX
2
- VERSION = "0.1.1"
2
+ VERSION = "1.0"
3
3
  end
data/spec/auth_spec.rb ADDED
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe ZBX::API do
4
+ api_url = 'http://localhost/api_jsonrpc.php'
5
+ before(:each) do
6
+ @invalid_client = ZBX::API.new "fakeusername", "fakepassword", api_url
7
+ @valid_client = ZBX::API.new "wenjun.yan", "123456", api_url
8
+ end
9
+
10
+ describe "invalid client authentication" do
11
+ it "should raise a runtime error!" do
12
+ expect { @invalid_client.auth! }.to raise_error(RuntimeError, /Authentication/)
13
+ end
14
+ end
15
+
16
+ describe "valid client authentication" do
17
+ it "should not raise any error" do
18
+ expect { @valid_client.auth! }.to_not raise_error
19
+ end
20
+
21
+ it "should return a authentication token, which is a string" do
22
+ token = @valid_client.auth!
23
+ token.should be_an(String)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe ZBX::API do
4
+ api_url = 'http://localhost/api_jsonrpc.php'
5
+
6
+ before(:each) do
7
+ @client = ZBX::API.new "wenjun.yan", "123456", api_url
8
+ end
9
+
10
+ describe "Using host.get to check if api request works " do
11
+ it "should return an not-empty array" do
12
+ ret = @client.host.get hostids: 10163
13
+ ret.should be_an(Array)
14
+ ret.first["hostid"].should eq("10163")
15
+ end
16
+
17
+ it "should return an empty array" do
18
+ ret = @client.host.get hostids: 1111111
19
+ ret.should be_an(Array)
20
+ ret.should be_empty
21
+ end
22
+ end
23
+
24
+ describe "Overwriting the default param `output`" do
25
+ it "should works and the returned host object has only hostid" do
26
+ ret = @client.host.get hostids: 10163, output: "hostid"
27
+ ret.first.size.should eq(1)
28
+ ret.first.should have_key("hostid")
29
+ end
30
+
31
+ it "should works and the returned host objectt has only hostid and host" do
32
+ ret = @client.host.get hostids: 10163, output: ["hostid", "host"]
33
+ ret.first.size.should eq(2)
34
+ ret.first.should have_key("hostid")
35
+ ret.first.should have_key("host")
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,5 @@
1
+ require 'bundler/setup'
2
+ require 'zbx'
3
+
4
+ RSpec.configure do |config|
5
+ end
data/zbx.gemspec CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
8
8
  spec.version = ZBX::VERSION
9
9
  spec.authors = ["wenjun.yan"]
10
10
  spec.email = ["mylastnameisyan@gmail.com"]
11
- spec.description = %q{yet another zabbix api}
11
+ spec.description = %q{yet another zabbix api wrapper and a dsl}
12
12
  spec.summary = %q{zabbix api in ruby way}
13
13
  spec.homepage = ""
14
14
  spec.license = "MIT"
@@ -20,4 +20,5 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
22
  spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
23
24
  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.1.1
4
+ version: '1.0'
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: 2013-08-21 00:00:00.000000000 Z
12
+ date: 2013-08-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -43,7 +43,23 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
- description: yet another zabbix api
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: yet another zabbix api wrapper and a dsl
47
63
  email:
48
64
  - mylastnameisyan@gmail.com
49
65
  executables: []
@@ -58,7 +74,11 @@ files:
58
74
  - lib/zbx.rb
59
75
  - lib/zbx/api.rb
60
76
  - lib/zbx/entity.rb
77
+ - lib/zbx/http_client.rb
61
78
  - lib/zbx/version.rb
79
+ - spec/auth_spec.rb
80
+ - spec/functional_spec.rb
81
+ - spec/spec_helper.rb
62
82
  - zbx.gemspec
63
83
  homepage: ''
64
84
  licenses:
@@ -85,5 +105,8 @@ rubygems_version: 1.8.25
85
105
  signing_key:
86
106
  specification_version: 3
87
107
  summary: zabbix api in ruby way
88
- test_files: []
108
+ test_files:
109
+ - spec/auth_spec.rb
110
+ - spec/functional_spec.rb
111
+ - spec/spec_helper.rb
89
112
  has_rdoc: