osvc_ruby 1.5.0

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,66 @@
1
+ require 'osvc_ruby/modules/validations_module'
2
+ require 'osvc_ruby/modules/normalize_module'
3
+ require 'json'
4
+
5
+ module OSvCRuby
6
+
7
+ class AnalyticsReportResults
8
+
9
+ include NormalizeModule
10
+ include ValidationsModule
11
+
12
+ attr_accessor :lookupName,:id, :filters
13
+
14
+ def initialize(**args)
15
+ @lookupName = args[:lookupName]
16
+ @id = args[:id]
17
+ @filters = []
18
+ end
19
+
20
+
21
+
22
+ def run(client)
23
+
24
+ json_data = convert_to_json(self)
25
+
26
+ ValidationsModule::check_client(client)
27
+
28
+ response = OSvCRuby::Connect.post_or_patch(client,'analyticsReportResults',json_data)
29
+
30
+ check_and_parse_response(response)
31
+
32
+ end
33
+
34
+
35
+ private
36
+
37
+ def convert_to_json(s)
38
+ json_data = {}
39
+ check_for_id_and_name(s)
40
+ s.instance_variables.each do|iv|
41
+ key = iv.to_s.delete("@")
42
+ value = instance_variable_get iv
43
+ json_data[key] = value unless value.nil?
44
+ end
45
+ json_data
46
+ end
47
+
48
+ def check_for_id_and_name(s)
49
+ if s.lookupName.nil? && s.id.nil?
50
+ raise ArgumentError, "AnalyticsReportResults must have an id or lookupName set"
51
+ end
52
+ end
53
+
54
+ def check_and_parse_response(response)
55
+ if response.code.to_i != 200
56
+ puts JSON.pretty_generate(response.body)
57
+ response.body
58
+ else
59
+ body = JSON.parse(response.body)
60
+ NormalizeModule.iterate_through_rows(body)
61
+ end
62
+ end
63
+
64
+ end
65
+
66
+ end
@@ -0,0 +1,41 @@
1
+ require 'osvc_ruby/modules/validations_module'
2
+ require 'osvc_ruby/modules/normalize_module'
3
+ require_relative '../../ext/string.rb'
4
+ require 'json'
5
+
6
+ module OSvCRuby
7
+
8
+ class QueryResults
9
+
10
+ include ValidationsModule, NormalizeModule
11
+
12
+ def initialize; end
13
+
14
+ def query(client,query)
15
+
16
+ ValidationsModule::check_client(client)
17
+
18
+ ValidationsModule::check_query(query,"query")
19
+
20
+ @query = URI.escape("queryResults/?query=#{query}")
21
+
22
+ obj_to_find = OSvCRuby::Connect.get(client,@query)
23
+
24
+ if obj_to_find.code.to_i == 200 || obj_to_find.code.to_i == 201
25
+
26
+ response = NormalizeModule::normalize(obj_to_find)
27
+ else
28
+
29
+ response = obj_to_find.body
30
+
31
+ end
32
+
33
+ JSON.parse(response)
34
+
35
+ end
36
+
37
+
38
+
39
+ end
40
+
41
+ end
@@ -0,0 +1,39 @@
1
+ require_relative 'query_results'
2
+ require 'osvc_ruby/modules/validations_module'
3
+
4
+ module OSvCRuby
5
+
6
+ class QueryResultsSet
7
+
8
+ include ValidationsModule
9
+
10
+ def query_set(client,*args)
11
+
12
+ ValidationsModule::check_client(client)
13
+
14
+ query_arr = []
15
+
16
+ key_map = []
17
+
18
+ args.each do |qh|
19
+
20
+ key_map.push(qh[:key].to_sym)
21
+
22
+ query_arr.push(qh[:query])
23
+
24
+ end
25
+
26
+ query_results_set = Struct.new( *key_map )
27
+ query_search = OSvCRuby::QueryResults.new
28
+
29
+
30
+ final_query_arr = query_arr.join('; ')
31
+ final_results = query_search.query(client,final_query_arr)
32
+
33
+ final_query_results_set = query_results_set.new( *final_results )
34
+ final_query_results_set
35
+ end
36
+
37
+ end
38
+
39
+ end
@@ -0,0 +1,41 @@
1
+ require 'osvc_ruby/version'
2
+ require 'osvc_ruby/configuration'
3
+
4
+
5
+ module OSvCRuby
6
+
7
+ class Client
8
+ # The top-level class that handles configuration and connection to the Oracle Service Cloud REST API.
9
+
10
+ attr_accessor :config
11
+
12
+ def initialize
13
+ raise ArgumentError, "block not given" unless block_given?
14
+ self.config ||= OSvCRuby::Configuration.new
15
+ yield(config)
16
+
17
+ check_config
18
+ optional_check
19
+ end
20
+
21
+ def check_config
22
+ if config.interface ==''
23
+ raise ArgumentError, "Interface cannot be nil or blank"
24
+ elsif config.username ==''
25
+ raise ArgumentError, "Username cannot be nil or blank"
26
+ elsif config.password ==''
27
+ raise ArgumentError, "Password cannot be nil or blank"
28
+ end
29
+
30
+ true
31
+ end
32
+
33
+ def optional_check
34
+ if config.no_ssl_verify.class != FalseClass && config.no_ssl_verify.class != TrueClass
35
+ raise ArgumentError, "The no SSL verification setting must be set to true or false"
36
+ elsif config.version.nil?
37
+ raise ArgumentError, "Connect version cannot be null"
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,15 @@
1
+ module OSvCRuby
2
+
3
+ class Configuration
4
+ # A holder class that holds the configuration information for the OSvCRuby::Client block
5
+
6
+ attr_accessor :interface,:username,:password,:no_ssl_verify,:version,:suppress_rules,:demo_site
7
+
8
+ def initialize
9
+ @no_ssl_verify = false
10
+ @version = 'v1.3'
11
+ @suppress_rules = false
12
+ @demo_site = false
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,226 @@
1
+ require 'osvc_ruby/client'
2
+ require 'osvc_ruby/modules/validations_module'
3
+
4
+ require 'net/http'
5
+ require 'openssl'
6
+ require 'uri'
7
+ require 'cgi'
8
+
9
+ module OSvCRuby
10
+
11
+ # This class is purely to provide the underlying methods for CRUD functionality using Net::HTTP, URI, and OpenSSL
12
+ class Connect
13
+
14
+ include ValidationsModule
15
+
16
+ def self.get(client,resource_url = nil)
17
+
18
+ @final_config = get_check(client,resource_url)
19
+
20
+ @uri = @final_config['site_url']
21
+
22
+ @username = @final_config['username']
23
+ @password = @final_config['password']
24
+
25
+ Net::HTTP.start(@uri.host, @uri.port,
26
+ :use_ssl => true,
27
+ :verify_mode => @final_config['ssl']) do |http|
28
+
29
+ request = Net::HTTP::Get.new @uri.request_uri
30
+
31
+ request.add_field('Content-Type', 'application/x-www-form-urlencoded')
32
+ if @final_config['suppress_rules'] == true
33
+ request.add_field 'OSvC-CREST-Suppress-All',true
34
+ end
35
+
36
+ request.basic_auth @username, @password
37
+
38
+ http.request request # Net::HTTPResponse object
39
+
40
+ end
41
+
42
+ end
43
+
44
+ def self.post_or_patch(client,resource_url = nil, json_content = nil,patch_request = false)
45
+
46
+ @final_config = post_and_patch_check(client,resource_url, json_content, patch_request)
47
+ @uri = @final_config['site_url']
48
+ @username = @final_config['username']
49
+ @password = @final_config['password']
50
+
51
+ Net::HTTP.start(@uri.host, @uri.port,
52
+ :use_ssl => true,
53
+ :verify_mode => @final_config['ssl']) do |http|
54
+
55
+ request = Net::HTTP::Post.new @uri.request_uri
56
+ request.basic_auth @username, @password
57
+ request.content_type = "application/json"
58
+ if @final_config['patch_request'] == true
59
+ request.add_field 'X-HTTP-Method-Override','PATCH'
60
+ end
61
+ if @final_config['suppress_rules'] == true
62
+ request.add_field 'OSvC-CREST-Suppress-All',true
63
+ end
64
+ request.body = JSON.dump(json_content)
65
+
66
+ http.request request # Net::HTTPResponse object
67
+
68
+ end
69
+
70
+ end
71
+
72
+ def self.post(client,resource_url = nil, json_content = nil)
73
+ self.post_or_patch(client,resource_url, json_content)
74
+ end
75
+
76
+ def self.patch(client,resource_url = nil, json_content = nil)
77
+ self.post_or_patch(client,resource_url, json_content,true)
78
+ end
79
+
80
+ def self.delete(client,resource_url = nil)
81
+
82
+ @final_config = delete_check(client,resource_url)
83
+
84
+ @uri = @final_config['site_url']
85
+ @username = @final_config['username']
86
+ @password = @final_config['password']
87
+
88
+ Net::HTTP.start(@uri.host, @uri.port,
89
+ :use_ssl => true,
90
+ :verify_mode => @final_config['ssl']) do |http|
91
+
92
+ request = Net::HTTP::Delete.new @uri.request_uri
93
+ request.basic_auth @username, @password
94
+
95
+ http.request request # Net::HTTPResponse object
96
+
97
+ end
98
+
99
+ end
100
+
101
+
102
+ ## checking methods
103
+
104
+ def self.generate_url_and_config(client,resource_url = nil, patch_request = false)
105
+
106
+ check_client_config(client)
107
+
108
+ @config = client.config
109
+
110
+ @version = @config.version
111
+
112
+ @ssl_verification = ssl_check(@config)
113
+
114
+ @rule_suppression = rule_suppress_check(@config)
115
+
116
+ @cust_or_demo = demo_check(@config)
117
+
118
+ @url = "https://" + @config.interface + ".#{@cust_or_demo}.com/services/rest/connect/#{@version}/#{resource_url}"
119
+
120
+ @final_uri = URI(@url)
121
+
122
+ @patch_request = patch_request == true ? true : false
123
+
124
+ @final_config = {'site_url' => @final_uri,
125
+ 'username' => @config.username,
126
+ 'password' => @config.password,
127
+ 'patch_request' => @patch_request,
128
+ 'ssl' => @ssl_verification,
129
+ 'suppress_rules' => @rule_suppression
130
+ }
131
+
132
+ end
133
+
134
+ def self.demo_check(config)
135
+ if config.demo_site == true
136
+ "rightnowdemo"
137
+ else
138
+ "custhelp"
139
+ end
140
+ end
141
+
142
+ def self.rule_suppress_check(config)
143
+
144
+ if config.suppress_rules == true || config.suppress_rules == "Yes"
145
+
146
+ true
147
+
148
+ else
149
+
150
+ false
151
+
152
+ end
153
+
154
+ end
155
+
156
+ def self.ssl_check(config)
157
+
158
+ if config.no_ssl_verify == true
159
+
160
+ OpenSSL::SSL::VERIFY_NONE
161
+
162
+ else
163
+
164
+ OpenSSL::SSL::VERIFY_PEER
165
+
166
+ end
167
+
168
+ end
169
+
170
+ def self.check_client_config(client)
171
+
172
+ @config = ValidationsModule::check_client(client).config
173
+
174
+ if @config.nil?
175
+ raise ArgumentError, "Client configuration cannot be nil or blank"
176
+ elsif @config.interface.nil?
177
+ raise ArgumentError, "The configured client interface cannot be nil or blank"
178
+ elsif @config.username.nil?
179
+ raise ArgumentError, "The configured client username cannot be nil or blank"
180
+ elsif @config.password.nil?
181
+ raise ArgumentError, "The configured client password cannot be nil or blank"
182
+ end
183
+
184
+ end
185
+
186
+ def self.get_check(client,resource_url = nil)
187
+
188
+ if client.nil?
189
+ raise ArgumentError, "Client must have some configuration set; please create an instance of OSvCRuby::Client with configuration settings"
190
+ elsif !resource_url.nil?
191
+ @final_config = generate_url_and_config(client,resource_url)
192
+ else
193
+ @final_config = generate_url_and_config(client,nil)
194
+ end
195
+
196
+ end
197
+
198
+ def self.post_and_patch_check(client,resource_url = nil, json_content = nil, patch_request = false)
199
+
200
+ if client.nil?
201
+ raise ArgumentError, "Client must have some configuration set; please create an instance of OSvCRuby::Client with configuration settings"
202
+ elsif resource_url.nil?
203
+ raise ArgumentError, "There is no URL resource provided; please specify a URL resource that you would like to send a POST or PATCH request to"
204
+ elsif json_content.nil?
205
+ raise ArgumentError, "There is no json content provided; please specify json content that you would like to send a POST or PATCH request with"
206
+ elsif patch_request == true
207
+ @final_config = generate_url_and_config(client,resource_url,true)
208
+ else
209
+ @final_config = generate_url_and_config(client,resource_url)
210
+ end
211
+
212
+ end
213
+
214
+ def self.delete_check(client,resource_url = nil)
215
+ if client.nil?
216
+ raise ArgumentError, "Client must have some configuration set; please create an instance of OSvCRuby::Client with configuration settings"
217
+ elsif resource_url.nil?
218
+ raise ArgumentError, "There is no URL resource provided; please specify a URL resource that you would like to send a POST or PATCH request to"
219
+ else
220
+ @final_config = generate_url_and_config(client,resource_url)
221
+ end
222
+ end
223
+
224
+ end
225
+
226
+ end
@@ -0,0 +1,62 @@
1
+ require 'json'
2
+
3
+ module OSvCRuby
4
+
5
+ module NormalizeModule
6
+
7
+ class << self
8
+
9
+ def normalize(input)
10
+
11
+ if input.code.to_i == 404
12
+
13
+ input.body
14
+
15
+ else
16
+
17
+ json_input = JSON.parse(input.body)
18
+
19
+ final_hash = []
20
+
21
+ json_input['items'].each do |item|
22
+ results_array = iterate_through_rows(item)
23
+ final_hash.push(results_array)
24
+ end
25
+
26
+ if final_hash.size === 1
27
+ final_hash = final_hash.flatten!
28
+ end
29
+
30
+ final_hash.to_json
31
+
32
+ end
33
+
34
+ end
35
+
36
+ def iterate_through_rows(item)
37
+
38
+ results_array = []
39
+
40
+ item['rows'].each_with_index do |row,_row_i|
41
+
42
+ obj_hash = {}
43
+
44
+ item['columnNames'].each_with_index do |column,i|
45
+
46
+ obj_hash[column] = if !row[i].nil? && row[i].is_i? == true then row[i].to_i else row[i] end
47
+
48
+ end
49
+
50
+ results_array.push(obj_hash)
51
+
52
+ end
53
+
54
+ results_array
55
+
56
+ end
57
+
58
+ end
59
+
60
+ end
61
+
62
+ end
@@ -0,0 +1,34 @@
1
+ require 'osvc_ruby/connect'
2
+ require 'json'
3
+
4
+ module OSvCRuby
5
+
6
+ module ValidationsModule
7
+
8
+ class << self
9
+
10
+ def check_query(query,method_name = "where")
11
+
12
+ if query.empty?
13
+
14
+ raise ArgumentError, "A query must be specified when using the '#{method_name}' method"
15
+
16
+ end
17
+
18
+ end
19
+
20
+ def check_client(client)
21
+
22
+ if client.class != OSvCRuby::Client || client.nil?
23
+
24
+ raise ArgumentError, "Client must have some configuration set; please create an instance of OSvCRuby::Client with configuration settings"
25
+
26
+ end
27
+ client
28
+ end
29
+
30
+ end
31
+
32
+ end
33
+
34
+ end
@@ -0,0 +1,3 @@
1
+ module OSvCRuby
2
+ VERSION = "1.5.0"
3
+ end
data/lib/osvc_ruby.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'convenience_methods'
2
+
3
+ module OSvCRuby;end
4
+
5
+ require 'osvc_ruby/client'
6
+ require 'osvc_ruby/connect'
7
+ require 'osvc_ruby/classes/query_results'
8
+ require 'osvc_ruby/classes/query_results_set'
9
+ require 'osvc_ruby/classes/analytics_report_results'
data/osvc_ruby.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'osvc_ruby/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "osvc_ruby"
8
+ spec.version = OSvCRuby::VERSION
9
+ spec.authors = ["Rajan Davis"]
10
+ spec.email = ["rajangdavis@gmail.com"]
11
+ spec.summary = %q{Making the best of opensource and enterprise technology}
12
+ spec.description = %q{An unofficial Ruby ORM on top of the Oracle Cloud Services (fka RightNow Technologies) REST API}
13
+ spec.homepage = %q{https://github.com/rajangdavis/osvc_ruby}
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "simplecov"
22
+ spec.add_development_dependency "codeclimate-test-reporter", '~> 1.0', '>= 1.0.0'
23
+ spec.add_development_dependency "bundler"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency 'rspec', '~> 3.2', '>= 3.2.0'
26
+ spec.add_development_dependency 'vcr', '~> 3.0', '>= 3.0.3'
27
+ spec.add_development_dependency "webmock"
28
+ end
@@ -0,0 +1,86 @@
1
+ require 'core/spec_helper'
2
+ require 'json'
3
+ require 'uri'
4
+
5
+ describe OSvCRuby::AnalyticsReportResults do
6
+
7
+ let(:client) {
8
+
9
+ OSvCRuby::Client.new do |config|
10
+
11
+ config.interface = ENV['OSC_SITE']
12
+
13
+ config.username = ENV['OSC_ADMIN']
14
+
15
+ config.password = ENV['OSC_PASSWORD']
16
+
17
+ config.demo_site = true
18
+
19
+ end
20
+ }
21
+
22
+ let(:last_updated){
23
+ OSvCRuby::AnalyticsReportResults.new(lookupName: "Last Updated By Status")
24
+ }
25
+
26
+ let(:answers_search){
27
+ OSvCRuby::AnalyticsReportResults.new(id: 176)
28
+ }
29
+
30
+ let(:error_example){
31
+ OSvCRuby::AnalyticsReportResults.new
32
+ }
33
+
34
+
35
+ context "#initialize" do
36
+
37
+ it "should expect an id or a lookupName for a report" do
38
+
39
+ expect(last_updated).to be_an(OSvCRuby::AnalyticsReportResults)
40
+
41
+ expect(last_updated.lookupName).to eq("Last Updated By Status")
42
+
43
+ expect(answers_search).to be_an(OSvCRuby::AnalyticsReportResults)
44
+
45
+ expect(answers_search.id).to eq(176)
46
+
47
+ end
48
+
49
+
50
+
51
+ end
52
+
53
+ context "#run" do
54
+ it 'should expect client is an instance of OSvCRuby::Client class and raise an error if does not' do
55
+
56
+ expect(client).to be_an(OSvCRuby::Client)
57
+
58
+ client = nil
59
+
60
+ expect{answers_search.run(client)}.to raise_error('Client must have some configuration set; please create an instance of OSvCRuby::Client with configuration settings')
61
+
62
+ end
63
+
64
+ it 'should throw an error if there is no id or no lookupName assigned',:vcr do
65
+ expect{error_example.run(client)}.to raise_error('AnalyticsReportResults must have an id or lookupName set')
66
+ error_example.id = 176
67
+ expect(error_example.run(client)).to be_an(Array)
68
+ end
69
+
70
+ it 'should be able to take filters', :vcr do
71
+ keywords = arrf(name: "search_ex", values: "Maestro")
72
+ answers_search.filters << keywords
73
+
74
+ answers = answers_search.run(client)
75
+
76
+ answers.each do |answer|
77
+
78
+ expect(answer['Summary']).to be_a(String)
79
+ expect(answer['Answer ID']).to be_a(Integer)
80
+ end
81
+
82
+ end
83
+
84
+ end
85
+
86
+ end