dynamicloud 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,424 @@
1
+ require 'dynamicloud/version'
2
+
3
+ #This module contains all what you need to build criteria, queries, updates, inserts, deletes, etc
4
+ module Dynamicloud
5
+ class API
6
+ class Criteria
7
+ class Condition
8
+ #Auxiliar constant to build from condition objects
9
+ ROOT = Condition.new
10
+
11
+ WITHOUT = '-'
12
+
13
+ #This method will return a of this condition
14
+ #@param parent this is the parent of this condition
15
+ #@return a string
16
+ def to_record_string(parent)
17
+ ''
18
+ end
19
+ end
20
+ # End of Condition class
21
+
22
+ class ANDCondition < Condition
23
+ # Will build an and condition using two part.
24
+ # @param left left part of this and condition
25
+ # @param right right part of this and condition
26
+ def initialize(left, right)
27
+ unless (left.is_a?(Condition)) || (right.is_a?(Condition))
28
+ raise 'left and right should implement Condition'
29
+ end
30
+
31
+ @left = left
32
+ @right = right
33
+ end
34
+
35
+ # This method will return a String of this condition
36
+ # @param parent this is the parent of this condition
37
+ # @return a json
38
+ def to_record_string(parent)
39
+ (parent.is_a?(ORCondition) ? '"where": {' : '') + @left.to_record_string(self) + ',' + @right.to_record_string(self) + (parent.is_a?(ORCondition) ? '}' : '')
40
+ end
41
+ end
42
+ # End of ANDCondition class
43
+
44
+ class EqualCondition < Condition
45
+
46
+ # This constructor will build an equal condition using left and right parts.
47
+ # @param left left part of this equal condition
48
+ # @param right right part of this equal condition
49
+ def initialize(left, right, greater_lesser = '-')
50
+ @left = left
51
+ @right = right
52
+ @need_quotes = right.is_a? String
53
+ @greater_lesser = greater_lesser
54
+ end
55
+
56
+ # This method will return a String of this condition
57
+ # @param parent this is the parent of this condition
58
+ # @return a json
59
+ def to_record_string(parent)
60
+ if @greater_lesser == '-'
61
+ return '"' + @left.to_s + '" : ' + (@need_quotes ? '"' : '') + @right.to_s + (@need_quotes ? '"' : '');
62
+ end
63
+
64
+ '"' + @left.to_s + '" : { ' + (@greater_lesser == '>' ? '"$gte": ' : '"$lte": ') +
65
+ (@need_quotes ? '"' : '') + @right.to_s + (@need_quotes ? '"' : '') + ' }'
66
+ end
67
+ end
68
+ # End of EqualCondition class
69
+
70
+ class GreaterLesser < Condition
71
+ def initialize(left, right, greater_lesser)
72
+ @greater_lesser = greater_lesser
73
+ @left = left
74
+ @right = right
75
+ @need_quotes = right.is_a?(String)
76
+ end
77
+
78
+ # This method will return a String of this condition
79
+ # @param parent this is the parent of this condition
80
+ # @return a json
81
+ def to_record_string(parent)
82
+ '"' + @left + '": { ' +
83
+ (@greater_lesser == '>' ? '"$gt"' : '"$lt"') + ': ' +
84
+ (@need_quotes ? '"' : '') + @right.to_s + (@need_quotes ? '"' : '') +
85
+ ' }'
86
+ end
87
+ end
88
+ # End of GreaterLesser class
89
+
90
+ # This class represents an IN and NOT IN condition.
91
+ # @author Eleazar Gomez
92
+ # @version 1.0.0
93
+ # @since 8/24/15
94
+ class INCondition < Condition
95
+
96
+ # Constructor to build either IN or NOT IN condition
97
+ # @param left attribute to compare
98
+ # @param values values to use to build IN or NOT IN condition
99
+ # @param not_in indicates if this condition is a not in.
100
+ def initialize(left, values, not_in = false)
101
+ @left = left
102
+ @values = values
103
+ @not_in = not_in
104
+ end
105
+
106
+ # This method will return a String of this condition
107
+ # @param parent this is the parent of this condition
108
+ # @return a json
109
+ def to_record_string(parent)
110
+ condition = '"' + @left + '": {' + (@not_in ? '"$nin"' : '"$in"') + ': ['
111
+
112
+ items = ''
113
+ @values.each do |value|
114
+ items = items + ((items.length == 0 ? '' : ',') + (value.is_a?(String) ? '"' : '') + value.to_s + (value.is_a?(String) ? '"' : ''))
115
+ end
116
+
117
+ condition + items + ']}'
118
+ end
119
+ end
120
+ # End of INCondition class
121
+
122
+ # This class represents a like condition <b>left like '%som%thing%'</b>
123
+ # @author Eleazar Gomez
124
+ # @version 1.0.0
125
+ # @since 8/23/15
126
+ class LikeCondition < Condition
127
+
128
+ # This constructor will build a like condition using left and right part.
129
+ # @param left left part of this like condition.
130
+ # @param right right part of this like condition.
131
+ def initialize(left, right, not_like = false)
132
+ @left = left
133
+ @right = right
134
+ @not_like = not_like
135
+ end
136
+
137
+ # This method will return a String of this condition
138
+ # @param parent this is the parent of this condition
139
+ # @return a json
140
+ def to_record_string(parent)
141
+ '"' + @left + '": { "$' + (@not_like ? 'n' : '') + 'like" : ' + '"' + @right + '"' + ' }'
142
+ end
143
+ end
144
+ # End of LikeCondition class
145
+
146
+ # This class represents a not equal condition <b>left != '%som%thing%'</b>
147
+ # @author Eleazar Gomez
148
+ # @version 1.0.0
149
+ # @since 8/23/15
150
+ class NotEqualCondition < Condition
151
+
152
+ # Constructor tha builds this condition
153
+ # @param left attribute to compare
154
+ # @param right right part of this condition
155
+ def initialize(left, right)
156
+ @left = left
157
+ @right = right
158
+ end
159
+
160
+ # This method will return a String of this condition
161
+ # @param parent this is the parent of this condition
162
+ # @return a json
163
+ def to_record_string(parent)
164
+ '"$ne" : {"' + @left + '" : ' + (@right.is_a?(String) ? '"' : '') + (@right.to_s) + (@right.is_a?(String) ? '"' : '') + '}'
165
+ end
166
+ end
167
+ # End of NotEqualCondition class
168
+
169
+ # This class represents an is null or is not null condition.
170
+ # @author Eleazar Gomez
171
+ # @version 1.0.0
172
+ # @since 8/23/15
173
+ class NullCondition < Condition
174
+
175
+ # Constructor tha builds this condition
176
+ # @param left attribute to compare
177
+ # @param is_not_null flag that indicates what kind of comparison.
178
+ def initialize(left, is_not_null = false)
179
+ @left = left
180
+ @is_not_null = is_not_null
181
+ end
182
+
183
+ # This method will return a String of this condition
184
+ # @param parent this is the parent of this condition
185
+ # @return a json
186
+ def to_record_string(parent)
187
+ '"' + @left + '": {' + (@is_not_null ? '"$notNull"' : '"$null"') + ': "1"}';
188
+ end
189
+ end
190
+ # End of NullCondition class
191
+
192
+
193
+ # This class represents an or condition.
194
+ # Implements condition to return a JSON according left and right parts.
195
+ # @author Eleazar Gomez
196
+ # @version 1.0.0
197
+ # @since 8/23/15
198
+ class ORCondition < Condition
199
+
200
+ # Constructor tha builds this condition
201
+ # @param left attribute to compare
202
+ # @param right right part of this or condition
203
+ def initialize(left, right)
204
+ unless (left.is_a?(Condition)) || (right.is_a?(Condition))
205
+ raise 'left and right should implement Condition'
206
+ end
207
+
208
+ @left = left
209
+ @right = right
210
+ end
211
+
212
+ # This method will return a String of this condition
213
+ # @param parent this is the parent of this condition
214
+ # @return a json
215
+ def to_record_string(parent)
216
+ (parent.is_a?(ORCondition) ? '' : '"$or": {') + @left.to_record_string(self) + ',' +
217
+ @right.to_record_string(self) + (parent.is_a?(ORCondition) ? '' : '}')
218
+ end
219
+ end
220
+ # End of ORCondition class
221
+
222
+ # This class represents a GroupBy clause.
223
+ # @author Eleazar Gomez
224
+ # @version 1.0.0
225
+ # @since 8/23/15
226
+ class GroupByClause < Condition
227
+
228
+ # Constructor tha builds this condition
229
+ # @param attributes attributes in group by clause
230
+ def initialize(attributes)
231
+ @attributes = attributes
232
+ end
233
+
234
+ # This method will return a String of this condition
235
+ # @param parent this is the parent of this condition
236
+ # @return a json
237
+ def to_record_string(parent)
238
+ group_by = '"groupBy": ['
239
+
240
+ attrs = ''
241
+ @attributes.each do |attr|
242
+ attrs = attrs + (attrs.length == 0 ? '' : ',') + '"' + attr + '"';
243
+ end
244
+
245
+ group_by + attrs + ']'
246
+ end
247
+ end
248
+ # End of GroupByClause class
249
+
250
+ # This class represents an OrderBy clause
251
+ # @author Eleazar Gomez
252
+ # @version 1.0.0
253
+ # @since 8/23/15
254
+ class OrderByClause < Condition
255
+ attr_accessor :asc, :attribute
256
+
257
+ # Build an orderBy clause using asc flag
258
+ # @param attribute attribute to use to order
259
+ # @return an orderBy object
260
+ def self.asc(attribute)
261
+ order = OrderByClause.new
262
+ order.asc = true
263
+ order.attribute = attribute
264
+
265
+ order
266
+ end
267
+
268
+ # Build an orderBy clause using desc flag
269
+ # @param attribute attribute to use to order
270
+ # @return an orderBy object
271
+ def self.desc(attribute)
272
+ order = OrderByClause.new
273
+ order.asc = false
274
+ order.attribute = attribute
275
+
276
+ order
277
+ end
278
+
279
+ # This method will return a String of this condition
280
+ # @param parent this is the parent of this condition
281
+ # @return a json
282
+ def to_record_string(parent)
283
+ '"order": "' + attribute + (asc ? ' ASC' : ' DESC') + '"'
284
+ end
285
+ end
286
+ # End of OrderByClause class
287
+
288
+
289
+ # This is a builder to create conditions: AND, OR, LIKE, NOT LIKE, IN, NOT IN, EQUALS, GREATER THAN, GREATER EQUALS THAN
290
+ # LESSER THAN, LESSER EQUALS THAN.
291
+ # @author Eleazar Gomez
292
+ # @version 1.0.0
293
+ # @since 8/22/15
294
+ class Conditions
295
+
296
+ # It will build an and condition using two parts (Left and Right)
297
+ # @param left left part of and
298
+ # @param right right part of and
299
+ # @return A built condition
300
+ def self.and(left, right)
301
+ ANDCondition.new(left, right);
302
+ end
303
+
304
+
305
+ # It will build an or condition using two parts (Left and Right)
306
+ # @param left left part of or
307
+ # @param right right part of or
308
+ # @return A built condition.
309
+ def self.or(left, right)
310
+ ORCondition.new(left, right)
311
+ end
312
+
313
+ # It will an in condition using an array of values.
314
+ # @param left attribute to compare
315
+ # @param values character values to build IN condition
316
+ # @return a built condition.
317
+ def self.in(left, values)
318
+ inner_in_condition(left, values, false)
319
+ end
320
+
321
+ # It will an in condition using an array of values.
322
+ # @param left attribute to compare
323
+ # @param values number values to build IN condition
324
+ # @return a built condition.
325
+ def self.not_in(left, values)
326
+ inner_in_condition(left, values, true)
327
+ end
328
+
329
+ # It will build a like condition.
330
+ # @param left attribute to comapare
331
+ # @param like to use for like condition
332
+ # @return a built condition.
333
+ def self.like(left, like)
334
+ LikeCondition.new(left, like, false)
335
+ end
336
+
337
+ # It will build a not like condition.
338
+ # @param left attribute to comapare
339
+ # @param like to use for like condition
340
+ # @return a built condition.
341
+ def self.not_like(left, like)
342
+ LikeCondition.new(left, like, true)
343
+ end
344
+
345
+ # It will build an equals condition.
346
+ # @param left attribute to compare
347
+ # @param right right part of this condition
348
+ # @return a built condition.
349
+ def self.equals(left, right)
350
+ inner_equals(left, right, Dynamicloud::API::Criteria::Condition::WITHOUT)
351
+ end
352
+
353
+ # It will build a not equals condition.
354
+ # @param left attribute to compare
355
+ # @param right right part of this condition
356
+ # @return a built condition.
357
+ def self.not_equals(left, right)
358
+ inner_not_equals(left, right)
359
+ end
360
+
361
+
362
+ # It will build a greater equals condition.
363
+ # @param left attribute to compare
364
+ # @param right right part of this condition
365
+ # @return a built condition.
366
+ def self.greater_equals(left, right)
367
+ inner_equals(left, right, '>')
368
+ end
369
+
370
+
371
+ # It will build a greater condition.
372
+ # @param left attribute to compare
373
+ # @param right right part of this condition
374
+ # @return a built condition.
375
+ def self.greater_than(left, right)
376
+ GreaterLesser.new(left, right, '>')
377
+ end
378
+
379
+
380
+ # It will build a lesser condition.
381
+ # @param left attribute to compare
382
+ # @param right right part of this condition
383
+ # @return a built condition.
384
+ def self.lesser_than(left, right)
385
+ GreaterLesser.new(left, right, '<')
386
+ end
387
+
388
+ # It will build a lesser equals condition.
389
+ # @param left attribute to compare
390
+ # @param right right part of this condition
391
+ # @return a built condition.
392
+ def self.lesser_equals(left, right)
393
+ inner_equals(left, right, '<')
394
+ end
395
+
396
+ # This method will build a not equals condition.
397
+ # @param left value to compare
398
+ # @param right right part of this condition
399
+ # @return a built condition
400
+ private
401
+ def self.inner_not_equals(left, right)
402
+ NotEqualCondition.new(left, right)
403
+ end
404
+
405
+ # This method will build either a equals condition.
406
+ # @param left value to compare
407
+ # @param greater_lesser indicates if greater or lesser condition must be added.
408
+ # @return a built condition
409
+ def self.inner_equals(left, right, greater_lesser)
410
+ EqualCondition.new(left, right, greater_lesser)
411
+ end
412
+
413
+ # It will either an in or not in condition using an array of values and a boolean that indicates
414
+ # what kind of IN will be built.
415
+ # @param left attribute to compare
416
+ # @param values values to build IN condition
417
+ # @return a built condition.
418
+ def self.inner_in_condition(left, values, not_in)
419
+ INCondition.new(left, values, not_in)
420
+ end
421
+ end
422
+ end
423
+ end
424
+ end
@@ -0,0 +1,105 @@
1
+ module Dynamicloud
2
+ class API
3
+ # This class represents a Model in <b>Dynamicloud</b>.
4
+ # @author Eleazar Gomez
5
+ # @version 1.0.0
6
+ # @since 8/25/15
7
+ class Model
8
+ class RecordModel
9
+ attr_accessor(:id, :name, :description)
10
+
11
+ # Constructs a model using its ID with Mapper
12
+ # @param id model id
13
+ def initialize(id)
14
+ @id = id
15
+ @name = nil
16
+ @description = nil
17
+ end
18
+ end
19
+
20
+ # Indicates the different field's types in DynamiCloud
21
+ # @author Eleazar Gomez
22
+ # @version 1.0.0
23
+ # @since 9/2/15
24
+ class RecordFieldType
25
+ # Build the enum
26
+ # @param type type value
27
+ def initialize(type)
28
+ @type = type
29
+ end
30
+
31
+ TEXT = RecordFieldType.new(1)
32
+ NUMBER = RecordFieldType.new(10)
33
+ CHECKBOX = RecordFieldType.new(2)
34
+ RADIO_BUTTON = RecordFieldType.new(3)
35
+ SELECT = RecordFieldType.new(4)
36
+ SELECT_MULTI_SELECTION = RecordFieldType.new(5)
37
+ TEXTAREA = RecordFieldType.new(6)
38
+ BIG_TEXT = RecordFieldType.new(7)
39
+ PASSWORD = RecordFieldType.new(8)
40
+ DATE = RecordFieldType.new(9)
41
+
42
+ # Gets the FieldType according type parameter
43
+ # @param type target
44
+ # @return RecordTypeField
45
+ def self.get_field_type(type)
46
+ case type
47
+ when 1
48
+ TEXT
49
+ when 2
50
+ CHECKBOX
51
+ when 3
52
+ RADIO_BUTTON
53
+ when 4
54
+ SELECT
55
+ when 5
56
+ SELECT_MULTI_SELECTION
57
+ when 6
58
+ TEXTAREA
59
+ when 7
60
+ BIG_TEXT
61
+ when 8
62
+ PASSWORD
63
+ when 9
64
+ DATE
65
+ else
66
+ NUMBER
67
+ end
68
+ end
69
+ end
70
+
71
+ # Represents the field's items
72
+ # @author Eleazar Gomez
73
+ # @version 1.0.0
74
+ # @since 9/3/15
75
+ class RecordFieldItem
76
+ attr_accessor(:value, :text)
77
+
78
+ def initialize
79
+ @value = ''
80
+ @text = ''
81
+ end
82
+ end
83
+
84
+ # Represents a field in Dynamicloud.
85
+ # @author Eleazar Gomez
86
+ # @version 1.0.0
87
+ # @since 9/2/15
88
+ class RecordField
89
+ attr_accessor(:id, :identifier, :label, :comment, :uniqueness, :required, :type, :items, :mid)
90
+
91
+ def initialize
92
+ @id = -1
93
+ @identifier = nil
94
+ @label = nil
95
+ @comment = nil
96
+ @uniqueness = false
97
+ @required = false
98
+ @type = nil
99
+ @items = nil
100
+ @mid = -1
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,66 @@
1
+ require 'rubygems'
2
+ require 'json'
3
+ require 'httpclient'
4
+
5
+ module DynamicService
6
+ # Class to call services
7
+ # @author Eleazar Gomez
8
+ # @version 1.0.0
9
+ # @since 8/22/15
10
+ class ServiceCaller
11
+ #@param service_url this the url to invoke
12
+ #@param params optional params that will be attached to the request
13
+ #@param method http method that will be used
14
+ #@param headers these are optional headers that will be attached to this request
15
+ #@param destiny this is a file that will be used to store a file from Dynamicloud servers
16
+ def self.call_service(service_url, params = {}, method = 'post', headers = {}, destiny = nil)
17
+ http = HTTPClient.new
18
+ http.connect_timeout = 10
19
+
20
+ headers['User-Agent'] = 'Dynamicloud client'
21
+ headers['APIVersion'] = Configuration::PROPERTIES.get_property :version
22
+ headers['Language'] = 'Ruby'
23
+
24
+ # download file
25
+ if destiny
26
+ destiny.write(http.get_content(service_url))
27
+ return
28
+ end
29
+
30
+ if method.eql? 'post'
31
+ return handle_response(http.post service_url, params)
32
+ end
33
+
34
+ if method.eql? 'get'
35
+ return handle_response(http.get service_url, params)
36
+ end
37
+
38
+ if method.eql? 'delete'
39
+ return handle_response(http.delete service_url, params)
40
+ else
41
+ raise 'Unsupported Http Method - "' << method.to_s << '"'
42
+ end
43
+ end
44
+
45
+ private
46
+ #Validate the http response status
47
+ def self.handle_response(response)
48
+ begin
49
+ if response.status == 200
50
+ response.content
51
+ else
52
+ parsed_json = JSON.parse(response.content)
53
+ message = parsed_json['message']
54
+
55
+ if message
56
+ raise message
57
+ else
58
+ raise 'Fatal error executing request'
59
+ end
60
+ end
61
+ rescue StandardError => se
62
+ raise se.message
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,5 @@
1
+ module Dynamicloud
2
+ VERSION = '1.0.1'
3
+
4
+ #(Dynamicloud::API::Criteria::EqualCondition.new 'name', 'Ele', '>').to_record_string nil
5
+ end
data/lib/exceptions.rb ADDED
@@ -0,0 +1,5 @@
1
+ class Exceptions
2
+ class IllegalStateException < Exception
3
+
4
+ end
5
+ end