apitool-client 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9e8a532859110a88e8c318546cd416005ba41c14
4
+ data.tar.gz: f30edbd256e8e370522bef696b3eb56c53f3b713
5
+ SHA512:
6
+ metadata.gz: 4be2c0cc5339a27747c63fa3b404d7984bb13f6f3fb5525dfa153006018dbcd539d293212babaf1ae01493653b4f3a6af2d25c5d1cbd36043ff70032261e791f
7
+ data.tar.gz: 982ba46803095c965978d75193ad7941bcb8841fcbbe61aac47fddaf6b8a365ccb8ffffd495de227414535d35e8a28a2c1153439b4cd6ed21f11ed3bf39fa4fa
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in apitool-client.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Terranova David
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,31 @@
1
+ # Apitool::Client
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'apitool-client'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install apitool-client
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/apitool-client/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ desc "Open an irb session preloaded with this library"
4
+ task :console do
5
+ sh "irb -rubygems -I lib -r apitool/client"
6
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'apitool/client/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "apitool-client"
8
+ s.version = Apitool::Client::VERSION
9
+ s.authors = ["Terranova David"]
10
+ s.email = ["dterranova@adhara-cybersecurity.com"]
11
+ s.summary = %q{APITool client.}
12
+ s.description = %q{APITool client.}
13
+ s.homepage = ""
14
+ s.license = "MIT"
15
+
16
+ s.files = `git ls-files -z`.split("\x0")
17
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "bundler", "~> 1.7"
22
+ s.add_development_dependency "rake", "~> 10.0"
23
+ s.add_development_dependency "rspec"
24
+ s.add_development_dependency "debugger2"
25
+
26
+ s.add_dependency 'rest-client'
27
+ end
@@ -0,0 +1,13 @@
1
+ require "apitool/client/version"
2
+
3
+ require "rest-client"
4
+ require "apitool/client/apitool_client"
5
+ require "apitool/client/role"
6
+ require "apitool/client/api_key"
7
+ require "apitool/client/vpn"
8
+
9
+ module Apitool
10
+ module Client
11
+ # Your code goes here...
12
+ end
13
+ end
@@ -0,0 +1,95 @@
1
+ class Apitool::Client::ApiKey < Apitool::Client::ApitoolClient
2
+
3
+ # ApiKey
4
+ #
5
+ # {
6
+ # :api_key => {
7
+ # :id => NUMERIC,
8
+ # :token => "TOKEN",
9
+ # :roles=>[
10
+ # {
11
+ # :role => {
12
+ # :id => 1,
13
+ # :name=>"master"
14
+ # }
15
+ # },
16
+ # {
17
+ # :role => {
18
+ # :id => 2,
19
+ # :name=>"admin"
20
+ # }
21
+ # },
22
+ # {
23
+ # :role => {
24
+ # :id => 3,
25
+ # :name=>"user"
26
+ # }
27
+ # }
28
+ # ]
29
+ # }
30
+ # }
31
+
32
+ def index
33
+ get('/api_keys') do |response, request, result|
34
+ if response.code == 200
35
+ parse(response)
36
+ else
37
+ nil
38
+ end
39
+ end
40
+ end
41
+
42
+ def show(token)
43
+ get("/api_keys/#{token}") do |response|
44
+ if response.code == 200
45
+ parse(response)
46
+ else
47
+ nil
48
+ end
49
+ end
50
+ end
51
+
52
+ # roles = 3
53
+ # roles = [3]
54
+ # roles = [1, 2, 3]
55
+ def create(roles = 3)
56
+ parameters = {
57
+ api_key: {
58
+ role_ids: [roles].flatten
59
+ }
60
+ }
61
+ post("/api_keys", parameters) do |response|
62
+ if response.code == 200
63
+ parse(response)
64
+ else
65
+ nil
66
+ end
67
+ end
68
+ end
69
+
70
+ def update(token, roles = 3)
71
+ parameters = {
72
+ api_key: {
73
+ role_ids: [roles].flatten
74
+ }
75
+ }
76
+ put("/api_keys/#{token}", parameters) do |response|
77
+ if response.code == 200
78
+ parse(response)
79
+ else
80
+ nil
81
+ end
82
+ end
83
+ end
84
+
85
+ def destroy(token)
86
+ delete("/api_keys/#{token}") do |response|
87
+ if response.code == 200
88
+ parse(response)
89
+ else
90
+ nil
91
+ end
92
+ end
93
+ end
94
+
95
+ end
@@ -0,0 +1,94 @@
1
+ class Apitool::Client::ApitoolClient
2
+
3
+ def initialize(params = {})
4
+ @host ||= params[:host]
5
+ @port ||= params[:port]
6
+ @ssl ||= params[:ssl] || false
7
+ @token ||= params[:token]
8
+ @version ||= params[:version] || 'v1'
9
+ @symbolize ||= params[:symbolize] || true
10
+ end
11
+
12
+ def response
13
+ @response.nil? ? nil : parse(@response)
14
+ end
15
+
16
+ def request
17
+ @request.nil? ? nil : @request.args
18
+ end
19
+
20
+ def result
21
+ @result.nil? ? nil : @result.code.to_i
22
+ end
23
+
24
+ protected
25
+
26
+ def get_connection
27
+ @client ||= RestClient::Resource.new(
28
+ base_uri
29
+ )
30
+ end
31
+
32
+ def get(path, options = {})
33
+ get_connection[request_uri(path)].get(headers) { |response, request, result, &block|
34
+ @response = response
35
+ @request = request
36
+ @result = result
37
+ yield response, request, result if block_given?
38
+ }
39
+ end
40
+
41
+ def post(path, parameters)
42
+ get_connection[request_uri(path)].post(parameters.to_json, headers) { |response, request, result, &block|
43
+ @response = response
44
+ @request = request
45
+ @result = result
46
+ yield response, request, result if block_given?
47
+ }
48
+ end
49
+
50
+ def put(path, parameters)
51
+ get_connection[request_uri(path)].put(parameters.to_json, headers) { |response, request, result, &block|
52
+ @response = response
53
+ @request = request
54
+ @result = result
55
+ yield response, request, result if block_given?
56
+ }
57
+ end
58
+
59
+ def delete(path)
60
+ get_connection[request_uri(path)].delete(headers) { |response, request, result, &block|
61
+ @response = response
62
+ @request = request
63
+ @result = result
64
+ yield response, request, result if block_given?
65
+ }
66
+ end
67
+
68
+ def parse(data)
69
+ JSON.parse(data, symbolize_names: @symbolize)
70
+ end
71
+
72
+ private
73
+
74
+ def base_uri
75
+ if @ssl
76
+ "https://#{@host}:#{@port}"
77
+ else
78
+ "http://#{@host}:#{@port}"
79
+ end
80
+ end
81
+
82
+ def request_uri(path)
83
+ "/api/#{@version}#{path}"
84
+ end
85
+
86
+ def headers
87
+ {
88
+ content_type: :json,
89
+ accept: :json,
90
+ authorization: "Token token=#{@token}"
91
+ }
92
+ end
93
+
94
+ end
@@ -0,0 +1,63 @@
1
+ class Apitool::Client::Role < Apitool::Client::ApitoolClient
2
+
3
+ def index
4
+ get('/roles') do |response, request, result|
5
+ if response.code == 200
6
+ parse(response)
7
+ else
8
+ nil
9
+ end
10
+ end
11
+ end
12
+
13
+ def show(name)
14
+ get("/roles/#{name}") do |response|
15
+ if response.code == 200
16
+ parse(response)
17
+ else
18
+ nil
19
+ end
20
+ end
21
+ end
22
+
23
+ def create(name = nil)
24
+ parameters = {
25
+ role: {
26
+ name: name
27
+ }
28
+ }
29
+ post("/roles", parameters) do |response|
30
+ if response.code == 200
31
+ parse(response)
32
+ else
33
+ nil
34
+ end
35
+ end
36
+ end
37
+
38
+ def update(name, new_name)
39
+ parameters = {
40
+ role: {
41
+ name: new_name
42
+ }
43
+ }
44
+ put("/roles/#{name}", parameters) do |response|
45
+ if response.code == 200
46
+ parse(response)
47
+ else
48
+ nil
49
+ end
50
+ end
51
+ end
52
+
53
+ def destroy(name)
54
+ delete("/roles/#{name}") do |response|
55
+ if response.code == 200
56
+ parse(response)
57
+ else
58
+ nil
59
+ end
60
+ end
61
+ end
62
+
63
+ end
@@ -0,0 +1,5 @@
1
+ module Apitool
2
+ module Client
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,48 @@
1
+ class Apitool::Client::Vpn < Apitool::Client::ApitoolClient
2
+
3
+ def index
4
+ get('/vpns') do |response, request, result|
5
+ if response.code == 200
6
+ parse(response)
7
+ else
8
+ nil
9
+ end
10
+ end
11
+ end
12
+
13
+ def show(id)
14
+ get("/vpns/#{id}") do |response|
15
+ if response.code == 200
16
+ parse(response)
17
+ else
18
+ nil
19
+ end
20
+ end
21
+ end
22
+
23
+ def create(username = nil)
24
+ parameters = {
25
+ vpn: {
26
+ username: username
27
+ }
28
+ }
29
+ post("/vpns", parameters) do |response|
30
+ if response.code == 200
31
+ parse(response)
32
+ else
33
+ nil
34
+ end
35
+ end
36
+ end
37
+
38
+ def destroy(id)
39
+ delete("/vpns/#{id}") do |response|
40
+ if response.code == 200
41
+ parse(response)
42
+ else
43
+ nil
44
+ end
45
+ end
46
+ end
47
+
48
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apitool::Client::ApiKey, order: :defined do
4
+ before(:context) do
5
+ @client = Apitool::Client::ApiKey.new({
6
+ host: "127.0.0.1",
7
+ port: 3001,
8
+ ssl: false,
9
+ token: "ef253c1cb7f379095e3cc2b8dbfceb72",
10
+ version: "v1"
11
+ })
12
+ end
13
+
14
+ it "should be possible to crud api keys" do
15
+ expect(@client.index.class).to be Array
16
+
17
+ api_key_count = @client.index.count
18
+ api_key = @client.create(3)
19
+ expect(@client.result).to be 200
20
+ expect(api_key[:api_key][:roles][0][:role][:name]).to eq 'user'
21
+ @new_key = api_key[:api_key][:token]
22
+ expect(@client.index.count).to be (api_key_count + 1)
23
+
24
+ api_key = @client.show(@new_key)
25
+ expect(@client.result).to be 200
26
+
27
+ api_key = @client.update(@new_key, 2)
28
+ expect(@client.result).to be 200
29
+ expect(api_key[:api_key][:roles][0][:role][:name]).to eq 'admin'
30
+
31
+ api_key_count = @client.index.count
32
+ @client.destroy(@new_key)
33
+ expect(@client.result).to be 200
34
+ expect(@client.index.count).to be (api_key_count - 1)
35
+ end
36
+
37
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apitool::Client::ApitoolClient do
4
+ before(:context) do
5
+ @client = Apitool::Client::ApitoolClient.new({
6
+ host: "127.0.0.1",
7
+ port: 3001,
8
+ ssl: false,
9
+ token: "ef253c1cb7f379095e3cc2b8dbfceb72",
10
+ version: "v1"
11
+ })
12
+ end
13
+
14
+ it "should be possible to establish a connection" do
15
+ expect(@client).to_not be nil
16
+ @client.send(:get, '')
17
+ expect(@client.result).to be 200
18
+ end
19
+
20
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apitool::Client::Role, order: :defined do
4
+ before(:context) do
5
+ @client = Apitool::Client::Role.new({
6
+ host: "127.0.0.1",
7
+ port: 3001,
8
+ ssl: false,
9
+ token: "ef253c1cb7f379095e3cc2b8dbfceb72",
10
+ version: "v1"
11
+ })
12
+ end
13
+
14
+ it "should be possible to list roles" do
15
+ expect(@client.index.class).to be Array
16
+ end
17
+
18
+ it "should be possible to create a role" do
19
+ role_count = @client.index.count
20
+ role = @client.create('role')
21
+ expect(@client.result).to be 200
22
+ expect(role[:role][:name]).to eq 'role'
23
+ expect(@client.index.count).to be (role_count + 1)
24
+ end
25
+
26
+ it "should be possible to fetch a role" do
27
+ role = @client.show('role')
28
+ expect(@client.result).to be 200
29
+ expect(role[:role][:name]).to eq 'role'
30
+ end
31
+
32
+ it "should be possible to update a role" do
33
+ role = @client.update('role', 'new_role')
34
+ expect(@client.result).to be 200
35
+ expect(role[:role][:name]).to eq 'new_role'
36
+ end
37
+
38
+ it "should be possible to destroy a role" do
39
+ role_count = @client.index.count
40
+ @client.destroy('new_role')
41
+ expect(@client.result).to be 200
42
+ expect(@client.index.count).to be (role_count - 1)
43
+ end
44
+
45
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apitool::Client do
4
+
5
+ it "should have a version" do
6
+ expect(Apitool::Client::VERSION).not_to be nil
7
+ end
8
+
9
+ end
@@ -0,0 +1,3 @@
1
+ require 'debugger'
2
+
3
+ require 'apitool/client'
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: apitool-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Terranova David
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: debugger2
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rest-client
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: APITool client.
84
+ email:
85
+ - dterranova@adhara-cybersecurity.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - apitool-client.gemspec
96
+ - lib/apitool/client.rb
97
+ - lib/apitool/client/api_key.rb
98
+ - lib/apitool/client/apitool_client.rb
99
+ - lib/apitool/client/role.rb
100
+ - lib/apitool/client/version.rb
101
+ - lib/apitool/client/vpn.rb
102
+ - spec/apitool/client/api_key_spec.rb
103
+ - spec/apitool/client/apitool_client_spec.rb
104
+ - spec/apitool/client/role_spec.rb
105
+ - spec/apitool/client_spec.rb
106
+ - spec/spec_helper.rb
107
+ homepage: ''
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.4.5
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: APITool client.
131
+ test_files:
132
+ - spec/apitool/client/api_key_spec.rb
133
+ - spec/apitool/client/apitool_client_spec.rb
134
+ - spec/apitool/client/role_spec.rb
135
+ - spec/apitool/client_spec.rb
136
+ - spec/spec_helper.rb