osc_ruby 0.1.13 → 0.2.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,163 @@
1
+ require 'osc_ruby/connect'
2
+ require 'osc_ruby/validations_module'
3
+ require 'json'
4
+
5
+ module OSCRuby
6
+
7
+ module ClassFactoryModule
8
+
9
+ class << self
10
+
11
+ def new_from_fetch(attributes,class_name)
12
+
13
+ ValidationsModule.check_attributes(attributes)
14
+
15
+ class_name.new(attributes)
16
+
17
+ end
18
+
19
+ def create(client,obj,resource_uri,return_json)
20
+
21
+ ValidationsModule::check_client(client)
22
+
23
+ final_json = obj.class.check_self(obj)
24
+
25
+ resource = URI.escape(resource_uri)
26
+
27
+ response = QueryModule::create(client,resource,final_json)
28
+
29
+ response_body = JSON.parse(response.body)
30
+
31
+ if response.code.to_i == 201 && return_json == false
32
+
33
+ obj.set_attributes(response_body)
34
+
35
+ elsif return_json == true
36
+
37
+ response.body
38
+
39
+ end
40
+
41
+ end
42
+
43
+ def find(client,id,obj_query,return_json,class_name)
44
+
45
+ ValidationsModule::check_client(client)
46
+
47
+ ValidationsModule::check_for_id(id)
48
+
49
+ url = "queryResults/?query=select * from #{obj_query} where id = #{id}"
50
+
51
+ resource = URI.escape(url)
52
+
53
+ class_json = QueryModule::find(client,resource)
54
+
55
+ if return_json == true
56
+
57
+ class_json
58
+
59
+ else
60
+
61
+ class_json_final = JSON.parse(class_json)
62
+
63
+ ClassFactoryModule::new_from_fetch(class_json_final[0],class_name)
64
+
65
+ end
66
+
67
+ end
68
+
69
+ def all(client,obj_query,return_json,class_name)
70
+
71
+ ValidationsModule::check_client(client)
72
+
73
+ resource = URI.escape("queryResults/?query=select * from #{obj_query}")
74
+
75
+ object_json = QueryModule::find(client,resource)
76
+
77
+ ClassFactoryModule::instantiate_multiple_objects(return_json, object_json, class_name)
78
+
79
+ end
80
+
81
+ def where(client,query,object_in_query,return_json,class_name)
82
+
83
+ ValidationsModule::check_client(client)
84
+
85
+ ValidationsModule::check_query(query)
86
+
87
+ @query = URI.escape("queryResults/?query=select * from #{object_in_query} where #{query}")
88
+
89
+ object_json = QueryModule::find(client,@query)
90
+
91
+ ClassFactoryModule::instantiate_multiple_objects(return_json, object_json, class_name)
92
+
93
+ end
94
+
95
+ def update(client,obj,resource_uri,return_json)
96
+
97
+ ValidationsModule::check_client(client)
98
+
99
+ ValidationsModule::check_object_for_id(obj,obj.class)
100
+
101
+ final_json = obj.class.check_self(obj,true)
102
+
103
+ resource = URI.escape("#{resource_uri}/#{obj.id}")
104
+
105
+ response = QueryModule::update(client,resource,final_json)
106
+
107
+ if response.code.to_i == 200 && return_json == false
108
+
109
+ updated_obj = obj.class.find(client,obj.id)
110
+
111
+ obj.update_attributes(updated_obj)
112
+
113
+ elsif return_json == true
114
+
115
+ response.body
116
+
117
+ end
118
+
119
+ end
120
+
121
+ def destroy(client,obj,resource_uri,return_json)
122
+
123
+ ValidationsModule::check_client(client)
124
+
125
+ ValidationsModule::check_object_for_id(obj,obj.class)
126
+
127
+ resource = URI.escape("/#{resource_uri}/#{obj.id}")
128
+
129
+ response = QueryModule::destroy(client,resource)
130
+
131
+ if response.code.to_i == 200 && return_json == false
132
+
133
+ nil
134
+
135
+ elsif return_json == true
136
+
137
+ response.body
138
+
139
+ end
140
+
141
+ end
142
+
143
+ def instantiate_multiple_objects(return_json, object_json, class_name)
144
+
145
+ if return_json == true
146
+
147
+ object_json
148
+
149
+ else
150
+
151
+ object_json_final = JSON.parse(object_json)
152
+
153
+ object_json_final.map { |attributes| ClassFactoryModule::new_from_fetch(attributes,class_name) }
154
+
155
+ end
156
+
157
+ end
158
+
159
+ end
160
+
161
+ end
162
+
163
+ end
@@ -15,6 +15,7 @@ module OSCRuby
15
15
  yield(config)
16
16
 
17
17
  check_config
18
+ optional_check
18
19
  end
19
20
 
20
21
  def check_config
@@ -23,10 +24,18 @@ module OSCRuby
23
24
  elsif config.username ==''
24
25
  raise ArgumentError, "Username cannot be nil or blank"
25
26
  elsif config.password ==''
26
- raise ArgumentError, "Password cannot be nil or blank"
27
+ raise ArgumentError, "Password cannot be nil or blank"
27
28
  end
28
29
 
29
30
  true
30
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
31
40
  end
32
41
  end
@@ -3,9 +3,11 @@ module OSCRuby
3
3
  class Configuration
4
4
  # A holder class that holds the configuration information for the OSCRuby::Client block
5
5
 
6
- attr_accessor :interface,:username,:password
6
+ attr_accessor :interface,:username,:password,:no_ssl_verify,:version
7
7
 
8
8
  def initialize
9
+ @no_ssl_verify = false
10
+ @version = 'v1.3'
9
11
  end
10
12
  end
11
13
  end
@@ -21,7 +21,7 @@ module OSCRuby
21
21
 
22
22
  Net::HTTP.start(@uri.host, @uri.port,
23
23
  :use_ssl => true,
24
- :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|
24
+ :verify_mode => @final_config['ssl']) do |http|
25
25
 
26
26
  request = Net::HTTP::Get.new @uri.request_uri
27
27
 
@@ -45,7 +45,7 @@ module OSCRuby
45
45
 
46
46
  Net::HTTP.start(@uri.host, @uri.port,
47
47
  :use_ssl => true,
48
- :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|
48
+ :verify_mode => @final_config['ssl']) do |http|
49
49
 
50
50
  request = Net::HTTP::Post.new @uri.request_uri
51
51
  request.basic_auth @username, @password
@@ -71,7 +71,7 @@ module OSCRuby
71
71
 
72
72
  Net::HTTP.start(@uri.host, @uri.port,
73
73
  :use_ssl => true,
74
- :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|
74
+ :verify_mode => @final_config['ssl']) do |http|
75
75
 
76
76
  request = Net::HTTP::Delete.new @uri.request_uri
77
77
  request.basic_auth @username, @password
@@ -91,17 +91,25 @@ module OSCRuby
91
91
 
92
92
  @config = client.config
93
93
 
94
- if !resource_url.nil?
95
- @resource_url = resource_url
94
+ @version = @config.version
95
+
96
+ if @config.no_ssl_verify == true
97
+
98
+ @ssl_verification = OpenSSL::SSL::VERIFY_NONE
99
+
100
+ else
101
+
102
+ @ssl_verification = OpenSSL::SSL::VERIFY_PEER
103
+
96
104
  end
97
105
 
98
- @url = "https://" + @config.interface + ".custhelp.com/services/rest/connect/v1.3/#{resource_url}"
106
+ @url = "https://" + @config.interface + ".custhelp.com/services/rest/connect/#{@version}/#{resource_url}"
99
107
 
100
108
  @final_uri = URI(@url)
101
109
 
102
110
  @patch_request = patch_request == true ? true : false
103
111
 
104
- @final_config = {'site_url' => @final_uri, 'username' => @config.username, 'password' => @config.password, 'patch_request' => @patch_request}
112
+ @final_config = {'site_url' => @final_uri, 'username' => @config.username, 'password' => @config.password, 'patch_request' => @patch_request, 'ssl' => @ssl_verification}
105
113
 
106
114
  end
107
115
 
@@ -1,4 +1,5 @@
1
1
  require 'osc_ruby/connect'
2
+ require 'osc_ruby/validations_module'
2
3
  require_relative '../ext/string'
3
4
 
4
5
  require 'json'
@@ -6,79 +7,85 @@ require 'json'
6
7
  module OSCRuby
7
8
 
8
9
  module QueryModule
9
-
10
- def self.find(rn_client,resource)
11
10
 
12
- obj_to_find = OSCRuby::Connect.get(rn_client,resource)
11
+ class << self
13
12
 
14
- if obj_to_find.code.to_i == 200 || obj_to_find.code.to_i == 201
13
+ include ValidationsModule
14
+
15
+ def find(rn_client,resource)
16
+
17
+ obj_to_find = OSCRuby::Connect.get(rn_client,resource)
18
+
19
+ if obj_to_find.code.to_i == 200 || obj_to_find.code.to_i == 201
20
+
21
+ check_obj_for_errors(obj_to_find)
15
22
 
16
- check_obj_for_errors(obj_to_find)
23
+ normalize(obj_to_find)
24
+ else
17
25
 
18
- normalize(obj_to_find)
19
- else
26
+ obj_to_find.body
20
27
 
21
- obj_to_find.body
28
+ end
22
29
 
23
30
  end
24
31
 
25
- end
32
+ def create(rn_client,resource,json_content)
26
33
 
27
- def self.create(rn_client,resource,json_content)
34
+ OSCRuby::Connect.post_or_patch(rn_client,resource,json_content)
28
35
 
29
- OSCRuby::Connect.post_or_patch(rn_client,resource,json_content)
36
+ end
30
37
 
31
- end
38
+ def update(rn_client,resource,json_content)
32
39
 
33
- def self.update(rn_client,resource,json_content)
40
+ OSCRuby::Connect.post_or_patch(rn_client,resource,json_content,true)
34
41
 
35
- OSCRuby::Connect.post_or_patch(rn_client,resource,json_content,true)
42
+ end
36
43
 
37
- end
44
+ def destroy(rn_client,resource)
38
45
 
39
- def self.destroy(rn_client,resource)
46
+ OSCRuby::Connect.delete(rn_client,resource)
47
+
48
+ end
40
49
 
41
- OSCRuby::Connect.delete(rn_client,resource)
42
-
43
- end
50
+ def normalize(input)
44
51
 
45
- def self.normalize(input)
52
+ if input.code.to_i == 404
46
53
 
47
- if input.code.to_i == 404
54
+ input.body
48
55
 
49
- input.body
56
+ else
50
57
 
51
- else
58
+ json_input = JSON.parse(input.body)
52
59
 
53
- json_input = JSON.parse(input.body)
60
+ final_hash = []
54
61
 
55
- final_hash = []
62
+ json_input['items'][0]['rows'].each do |row|
56
63
 
57
- json_input['items'][0]['rows'].each do |row|
64
+ obj_hash = {}
65
+
66
+ json_input['items'][0]['columnNames'].each_with_index do |column,i|
67
+ obj_hash[column] = if !row[i].nil? && row[i].is_i? == true then row[i].to_i else row[i] end
68
+ end
69
+
70
+ final_hash.push(obj_hash)
58
71
 
59
- obj_hash = {}
60
-
61
- json_input['items'][0]['columnNames'].each_with_index do |column,i|
62
- obj_hash[column] = if !row[i].nil? && row[i].is_i? == true then row[i].to_i else row[i] end
63
72
  end
64
73
 
65
- final_hash.push(obj_hash)
74
+ final_hash.to_json
66
75
 
67
76
  end
68
77
 
69
- final_hash.to_json
70
-
71
78
  end
72
79
 
73
- end
80
+ def check_obj_for_errors(obj_to_check)
74
81
 
75
- def self.check_obj_for_errors(obj_to_check)
82
+ json_obj = JSON.parse(obj_to_check.body)
76
83
 
77
- json_obj = JSON.parse(obj_to_check.body)
84
+ if !json_obj.nil? && json_obj['items'][0]['rows'].count == 0
78
85
 
79
- if !json_obj.nil? && json_obj['items'][0]['rows'].count == 0
86
+ raise ArgumentError, 'There were no objects matching your query; please try again.'
80
87
 
81
- raise ArgumentError, 'There were no objects matching your query; please try again.'
88
+ end
82
89
 
83
90
  end
84
91
 
@@ -1,5 +1,7 @@
1
1
  require 'osc_ruby/client'
2
2
  require 'osc_ruby/query_module'
3
+ require 'osc_ruby/validations_module'
4
+ require 'osc_ruby/class_factory_module'
3
5
  require 'json'
4
6
  require 'uri'
5
7
 
@@ -8,252 +10,124 @@ module OSCRuby
8
10
  class ServiceProduct
9
11
 
10
12
  include QueryModule
13
+ include ValidationsModule
14
+ include ClassFactoryModule
11
15
 
12
- attr_accessor :names, :parent, :displayOrder, :adminVisibleInterfaces, :endUserVisibleInterfaces, :id, :lookupName, :createdTime, :updatedTime, :name
16
+ attr_accessor :names,:parent,:displayOrder,:adminVisibleInterfaces,:endUserVisibleInterfaces,:id,:lookupName,:createdTime,:updatedTime,:name
13
17
 
14
18
  def initialize(attributes = nil)
15
19
 
16
20
  @names = []
17
-
18
21
  @adminVisibleInterfaces = []
19
-
20
22
  @endUserVisibleInterfaces = []
21
23
 
22
24
  if attributes.nil?
23
25
 
24
26
  @parent = {}
25
-
26
27
  @displayOrder = 1
27
28
 
28
29
  else
29
30
 
30
31
  @id = attributes["id"]
31
-
32
32
  @lookupName = attributes["lookupName"]
33
-
34
33
  @createdTime = attributes["createdTime"]
35
-
36
34
  @updatedTime = attributes["updatedTime"]
37
-
38
35
  @displayOrder = attributes["displayOrder"]
39
-
40
36
  @name = attributes["name"]
41
-
42
37
  @parent = attributes["parent"]
43
38
 
44
39
  end
45
40
 
46
41
  end
47
42
 
43
+
48
44
  def create(client,return_json = false)
49
45
 
50
- self.class.check_client(client)
51
-
52
- new_product = self
53
-
54
- final_json = self.class.check_self(new_product)
55
-
56
- resource = URI.escape("/serviceProducts")
57
-
58
- response = QueryModule::create(client,resource,final_json)
59
-
60
- response_body = JSON.parse(response.body)
61
-
62
- if response.code.to_i == 201 && return_json == false
63
-
64
- self.id = response_body['id'].to_i
65
-
66
- self.name = response_body["lookupName"]
67
-
68
- self.lookupName = response_body["lookupName"]
69
-
70
- self.displayOrder = response_body["displayOrder"]
71
-
72
- if !response_body["parent"].nil?
73
-
74
- self.parent = response_body["parent"]["links"][0]["href"].split('/').pop.to_i
75
-
76
- else
77
-
78
- self.parent = nil
79
-
80
- end
81
-
82
- elsif return_json == true
83
-
84
- response.body
85
-
86
- end
46
+ ClassFactoryModule.create(client,self,"/serviceProducts",return_json)
87
47
 
88
48
  end
89
49
 
50
+
90
51
  def self.find(client,id = nil,return_json = false)
91
52
 
92
- check_client(client)
93
-
94
- if id.nil? == true
95
- raise ArgumentError, 'ID cannot be nil'
96
- elsif id.class != Fixnum
97
- raise ArgumentError, 'ID must be an integer'
98
- end
99
-
100
- resource = URI.escape("queryResults/?query=select * from serviceproducts where id = #{id}")
101
-
102
- service_product_json = QueryModule::find(client,resource)
103
-
104
- if return_json == true
105
-
106
- service_product_json
107
-
108
- else
109
-
110
- service_product_json_final = JSON.parse(service_product_json)
111
-
112
- new_from_fetch(service_product_json_final[0])
113
-
114
- end
53
+ ClassFactoryModule.find(client,id,'serviceproducts',return_json,OSCRuby::ServiceProduct)
115
54
 
116
55
  end
117
56
 
57
+
118
58
  def self.all(client, return_json = false)
119
59
 
120
- check_client(client)
121
-
122
- resource = URI.escape("queryResults/?query=select * from serviceproducts")
123
-
124
- service_product_json = QueryModule::find(client,resource)
125
-
126
- if return_json == true
127
-
128
- service_product_json
129
-
130
- else
131
-
132
- service_product_json_final = JSON.parse(service_product_json)
133
-
134
- service_product_json_final.map { |attributes| new_from_fetch(attributes) }
135
-
136
- end
60
+ ClassFactoryModule.all(client,'serviceproducts',return_json,OSCRuby::ServiceProduct)
137
61
 
138
62
  end
139
63
 
64
+
140
65
  def self.where(client, query = '', return_json = false)
141
66
 
142
- check_client(client)
143
-
144
- check_query(query)
145
-
146
- @query = URI.escape("queryResults/?query=select * from serviceproducts where #{query}")
147
-
148
- service_product_json = QueryModule::find(client,@query)
149
-
150
- if return_json == true
151
-
152
- service_product_json
153
-
154
- else
155
-
156
- service_product_json_final = JSON.parse(service_product_json)
157
-
158
- service_product_json_final.map { |attributes| new_from_fetch(attributes) }
159
-
160
- end
67
+ ClassFactoryModule.where(client,query,'serviceproducts',return_json,OSCRuby::ServiceProduct)
161
68
 
162
69
  end
163
70
 
71
+
164
72
  def update(client, return_json = false)
165
73
 
166
- self.class.check_client(client)
167
-
168
- product_to_update = self
169
-
170
- self.class.check_for_id(product_to_update)
171
-
172
- final_json = self.class.check_self(product_to_update,true)
173
-
174
- resource = URI.escape("/serviceProducts/#{product_to_update.id}")
175
-
176
- response = QueryModule::update(client,resource,final_json)
177
-
178
- if response.code.to_i == 200 && return_json == false
179
-
180
- updated_product = OSCRuby::ServiceProduct.find(client,product_to_update.id)
181
-
182
- self.lookupName = updated_product.lookupName
183
-
184
- self.createdTime = updated_product.createdTime
185
-
186
- self.updatedTime = updated_product.updatedTime
187
-
188
- self.name = updated_product.name
189
-
190
- self.parent = updated_product.parent
191
-
192
- elsif return_json == true
193
-
194
- response.body
195
-
196
- end
74
+ ClassFactoryModule::update(client,self,"serviceProducts",return_json)
197
75
 
198
76
  end
199
77
 
200
- def destroy(client, return_json = false)
201
-
202
- self.class.check_client(client)
203
-
204
- product_to_destroy = self
205
-
206
- self.class.check_for_id(product_to_destroy)
207
-
208
- resource = URI.escape("/serviceProducts/#{product_to_destroy.id}")
209
-
210
- response = QueryModule::destroy(client,resource)
211
-
212
- if response.code.to_i == 200 && return_json == false
213
78
 
214
- nil
215
-
216
- elsif return_json == true
217
-
218
- response.body
79
+ def destroy(client, return_json = false)
219
80
 
220
- end
81
+ ClassFactoryModule.destroy(client,self,'serviceProducts',return_json)
221
82
 
222
83
  end
223
84
 
224
85
 
86
+
225
87
 
88
+ # Convenience Methods for making the CRUD operations nicer to use
226
89
 
90
+ def set_attributes(response_body)
227
91
 
92
+ self.id = response_body["id"]
228
93
 
94
+ self.name = response_body["lookupName"]
229
95
 
96
+ self.lookupName = response_body["lookupName"]
230
97
 
98
+ self.displayOrder = response_body["displayOrder"]
231
99
 
100
+ if !response_body["parent"].nil?
232
101
 
102
+ self.parent = response_body["parent"]["links"][0]["href"].split('/').pop.to_i
233
103
 
234
- # Convenience Methods for making the CRUD operations nicer to use
235
-
236
- def self.new_from_fetch(attributes)
104
+ else
237
105
 
238
- check_attributes(attributes)
106
+ self.parent = nil
239
107
 
240
- OSCRuby::ServiceProduct.new(attributes)
108
+ end
241
109
 
242
110
  end
243
111
 
244
- def self.check_for_id(obj)
112
+ def update_attributes(updated_product)
113
+
114
+ self.lookupName = updated_product.lookupName
245
115
 
246
- if obj.id.nil?
116
+ self.createdTime = updated_product.createdTime
247
117
 
248
- raise ArgumentError, 'OSCRuby::ServiceProduct must have a valid ID set'
118
+ self.updatedTime = updated_product.updatedTime
249
119
 
250
- end
120
+ self.name = updated_product.name
121
+
122
+ self.parent = updated_product.parent
251
123
 
252
124
  end
253
125
 
254
126
  def self.check_self(obj,is_update = false)
255
127
 
256
- obj_attrs = self.extract_attributes(obj)
128
+ obj_attrs = ValidationsModule::extract_attributes(obj)
129
+
130
+ obj_attrs = check_interfaces(obj_attrs)
257
131
 
258
132
  if is_update == true
259
133
 
@@ -317,21 +191,7 @@ module OSCRuby
317
191
 
318
192
  end
319
193
 
320
- def self.extract_attributes(obj)
321
-
322
- empty_arr = [{}]
323
-
324
- obj_vars = obj.instance_variables
325
-
326
- obj_vars.each do |var|
327
-
328
- obj_attr = var.to_s.delete("@")
329
-
330
- obj_attr_val = obj.instance_variable_get(var)
331
-
332
- empty_arr[0][obj_attr] = obj_attr_val
333
-
334
- end
194
+ def self.check_interfaces(empty_arr)
335
195
 
336
196
  if empty_arr[0]['adminVisibleInterfaces'].empty?
337
197
 
@@ -349,38 +209,6 @@ module OSCRuby
349
209
 
350
210
  end
351
211
 
352
-
353
-
354
- # Will probably extract the following into a Validations class or something
355
-
356
- def self.check_attributes(attributes)
357
-
358
- if attributes.class != Hash
359
-
360
- raise ArgumentError, "Attributes must be a hash; please use the appropriate data structure"
361
-
362
- end
363
-
364
- end
365
-
366
- def self.check_query(query)
367
-
368
- if query.empty?
369
-
370
- raise ArgumentError, 'A query must be specified when using the "where" method'
371
-
372
- end
373
-
374
- end
375
-
376
- def self.check_client(client)
377
-
378
- if client.class != OSCRuby::Client || client.nil?
379
- raise ArgumentError, "Client must have some configuration set; please create an instance of OSCRuby::Client with configuration settings"
380
- end
381
-
382
- end
383
-
384
212
  end
385
213
 
386
214
  end