datavirtuality_rest 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4472c398087483906413dab58918157ccc11a430
4
+ data.tar.gz: fe5d4f9277c9fff790744d46ab9d1a60dcee324e
5
+ SHA512:
6
+ metadata.gz: 3c8af863c25768a5cbac4402ed614cbbdf34c7970f1f0683f8c244cad550f6a474e5d450497fc1b64ccae24dceceedba75eab04b344d8ad0b749a3897d0df62b
7
+ data.tar.gz: 8b6b95b2775f908e9d34f0c53f38ef1210a66e58a0f19becddb6b114e1facebbe6206695a02b3f7b485624a2841694047ce07148c43ea8379aa4a1a0ec64b1ef
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2017 Data Virtuality GmbH
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.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # DataVirtuaity REST Client
2
+
3
+ Easy way to retrieve data from your DataVirtuality server via the REST API to consume data in any Ruby application.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'datavirtuality_rest'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it manually:
16
+
17
+ $ gem install datavirtuality_rest
18
+
19
+ ## Usage Examples
20
+
21
+ First instantiate your client by specifying the server URL, username and password:
22
+
23
+ > client = DataVirtuality::Rest.new('http://127.0.0.1:8080', '<USER NAME>', '<PASSWORD>')
24
+
25
+ Now you can retrieve data in two ways:
26
+
27
+ 1. Read a full table or view by specifying the full schema and path, separated by a slash:
28
+
29
+ > client.get('views/demo_view')
30
+ => [{:id=>"438256", :date=>"2017-04-01", :hits=>331}, {:id=>"438918", :date=>"2017-04-01", :hits=>314}]
31
+
32
+
33
+ 2. Specify a customer SQL query to gain full flexibility:
34
+
35
+ > client.query('SELECT * FROM views.demo_view')
36
+ => [{:id=>"438918", :date=>"2017-04-01", :hits=>886}, {:id=>"431936", :date=>"2017-02-02", :hits=>389}])
37
+
38
+ ## Contributing
39
+
40
+ Are you using Ruby to access data from Data Virtuality? Do you have any feedback or are you missing additional functionality? Just let us know by creating an issue or pull request to get in touch with us.
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'datavirtuality/rest'
4
+ require 'datavirtuality/rest_exception'
5
+
6
+ # DataVirtuality client module
7
+ module DataVirtuality
8
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'httpclient'
4
+ require 'json'
5
+ require 'json/ext'
6
+
7
+ module DataVirtuality
8
+ # DataVirtuality REST client
9
+ class Rest
10
+ def initialize(host, username, password)
11
+ @host = host
12
+ @username = username
13
+ @password = password
14
+
15
+ @http_client = HTTPClient.new
16
+ @http_client.set_auth(host, username, password)
17
+ end
18
+
19
+ def status
20
+ json_response(http_get('status'))&.[](:status)&.downcase&.to_sym
21
+ end
22
+
23
+ def data_sources
24
+ json_response(http_get('source')).map { |h| h[:Name] }
25
+ end
26
+
27
+ def get(table)
28
+ json_response(http_get('source/' + table))
29
+ end
30
+
31
+ def query(sql)
32
+ json_response(http_post('query?array=false', sql: sql))
33
+ end
34
+
35
+ private
36
+
37
+ attr_reader :http_client, :host, :username, :password
38
+
39
+ def http_get(path)
40
+ response = http_client.get endpoint(path)
41
+
42
+ { status: response.status, body: response.body }
43
+ end
44
+
45
+ def http_post(path, body)
46
+ response = http_client.post(
47
+ endpoint(path),
48
+ body.to_json,
49
+ 'Content-Type' => 'application/json'
50
+ )
51
+
52
+ { status: response.status, body: response.body }
53
+ end
54
+
55
+ def endpoint(path)
56
+ host + '/rest/api/' + path
57
+ end
58
+
59
+ def json_response(result)
60
+ if result[:status] != 200
61
+ error = begin
62
+ JSON.parse result[:body]
63
+ rescue => e
64
+ raise RestException, e.message
65
+ end
66
+
67
+ raise RestException, error['description'], error['hint']
68
+ end
69
+
70
+ JSON.parse result[:body], symbolize_names: true
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DataVirtuality
4
+ # Exception for errors returned from DataVirtuality REST API
5
+ class RestException < RuntimeError
6
+ def initialize(message, hint = nil)
7
+ super(message)
8
+ @hint = hint
9
+ end
10
+
11
+ attr_reader :hint
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: datavirtuality_rest
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - DataVirtuality GmbH
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-04-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httpclient
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: vcr
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: webmock
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: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Enables easy access to DataVirtuality via its REST API
84
+ email: support@datavirtuality.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - LICENSE.txt
90
+ - README.md
91
+ - lib/datavirtuality.rb
92
+ - lib/datavirtuality/rest.rb
93
+ - lib/datavirtuality/rest_exception.rb
94
+ homepage: http://www.datavirtuality.com
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.6.11
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: A REST client for DataVirtuality
118
+ test_files: []