syncano 3.1.1.beta

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.
Files changed (57) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +25 -0
  3. data/Gemfile +4 -0
  4. data/Guardfile +5 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +304 -0
  7. data/Rakefile +7 -0
  8. data/lib/generators/syncano/install_generator.rb +17 -0
  9. data/lib/generators/syncano/templates/initializers/syncano.rb +4 -0
  10. data/lib/syncano.rb +92 -0
  11. data/lib/syncano/batch_queue.rb +58 -0
  12. data/lib/syncano/batch_queue_element.rb +33 -0
  13. data/lib/syncano/clients/base.rb +115 -0
  14. data/lib/syncano/clients/rest.rb +69 -0
  15. data/lib/syncano/clients/sync.rb +132 -0
  16. data/lib/syncano/errors.rb +13 -0
  17. data/lib/syncano/jimson_client.rb +34 -0
  18. data/lib/syncano/packets/auth.rb +7 -0
  19. data/lib/syncano/packets/base.rb +64 -0
  20. data/lib/syncano/packets/call.rb +34 -0
  21. data/lib/syncano/packets/call_response.rb +33 -0
  22. data/lib/syncano/packets/error.rb +19 -0
  23. data/lib/syncano/packets/message.rb +30 -0
  24. data/lib/syncano/packets/notification.rb +39 -0
  25. data/lib/syncano/packets/ping.rb +12 -0
  26. data/lib/syncano/query_builder.rb +144 -0
  27. data/lib/syncano/resources/admin.rb +26 -0
  28. data/lib/syncano/resources/api_key.rb +46 -0
  29. data/lib/syncano/resources/base.rb +375 -0
  30. data/lib/syncano/resources/collection.rb +186 -0
  31. data/lib/syncano/resources/data_object.rb +304 -0
  32. data/lib/syncano/resources/folder.rb +34 -0
  33. data/lib/syncano/resources/notifications/base.rb +103 -0
  34. data/lib/syncano/resources/notifications/create.rb +20 -0
  35. data/lib/syncano/resources/notifications/destroy.rb +20 -0
  36. data/lib/syncano/resources/notifications/message.rb +9 -0
  37. data/lib/syncano/resources/notifications/update.rb +24 -0
  38. data/lib/syncano/resources/project.rb +42 -0
  39. data/lib/syncano/resources/role.rb +11 -0
  40. data/lib/syncano/resources/subscription.rb +12 -0
  41. data/lib/syncano/resources/user.rb +47 -0
  42. data/lib/syncano/response.rb +22 -0
  43. data/lib/syncano/sync_connection.rb +110 -0
  44. data/lib/syncano/version.rb +4 -0
  45. data/spec/admins_spec.rb +16 -0
  46. data/spec/api_keys_spec.rb +34 -0
  47. data/spec/collections_spec.rb +67 -0
  48. data/spec/data_objects_spec.rb +113 -0
  49. data/spec/folders_spec.rb +39 -0
  50. data/spec/notifications_spec.rb +43 -0
  51. data/spec/projects_spec.rb +35 -0
  52. data/spec/roles_spec.rb +13 -0
  53. data/spec/spec_helper.rb +13 -0
  54. data/spec/sync_resources_spec.rb +35 -0
  55. data/spec/syncano_spec.rb +9 -0
  56. data/syncano.gemspec +32 -0
  57. metadata +250 -0
@@ -0,0 +1,304 @@
1
+ class Syncano
2
+ module Resources
3
+ # Data object resource - corresponds to Syncano data resource
4
+ class DataObject < ::Syncano::Resources::Base
5
+ # Overwritten constructor with recursive initializing associated children objects
6
+ # @param [Syncano::Clients::Base] client
7
+ # @param [Hash] attributes
8
+ def initialize(client, attributes = {})
9
+ super(client, attributes)
10
+ if self.attributes[:children].present?
11
+ self.attributes[:children] = self.attributes[:children].collect do |child|
12
+ if child.is_a?(Hash)
13
+ self.class.new(client, child)
14
+ else
15
+ child
16
+ end
17
+ end
18
+ end
19
+
20
+ if self.attributes[:user].is_a?(Hash)
21
+ self.attributes[:user] = ::Syncano::Resources::User.new(client, self.attributes[:user])
22
+ end
23
+
24
+ if self.attributes[:additional].is_a?(Hash)
25
+ self.attributes.merge!(self.attributes.delete(:additional))
26
+ end
27
+ end
28
+
29
+ # Wrapper for api "get_one" method with data_key as a key
30
+ # @param [Syncano::Clients::Base] client
31
+ # @param [String] key
32
+ # @param [Hash] scope_parameters
33
+ # @param [Hash] conditions
34
+ # @return [Syncano::Resources::DataObject]
35
+ def self.find_by_key(client, key, scope_parameters = {}, conditions = {})
36
+ perform_find(client, :key, key, scope_parameters, conditions)
37
+ end
38
+
39
+ # Wrapper for api "count" method
40
+ # @param [Syncano::Clients::Base] client
41
+ # @param [Hash] scope_parameters
42
+ # @param [Hash] conditions
43
+ # @return [Integer]
44
+ def self.count(client, scope_parameters = {}, conditions = {})
45
+ response = perform_count(client, scope_parameters, conditions)
46
+ response.data
47
+ end
48
+
49
+ # Wrapper for api "move" method
50
+ # @param [Syncano::Clients::Base] client
51
+ # @param [Hash] scope_parameters
52
+ # @param [Array] data_ids
53
+ # @param [Hash] conditions
54
+ # @param [String] new_folder
55
+ # @param [String] new_state
56
+ # @return [Array] collection of Syncano::Resource::DataObject objects
57
+ def self.move(client, scope_parameters = {}, data_ids = [], conditions = {}, new_folder = nil, new_state = nil)
58
+ response = perform_move(client, nil, scope_parameters, data_ids, conditions, new_folder, new_state)
59
+ all(client, scope_parameters, data_ids: data_ids)
60
+ end
61
+
62
+ # Batch version of "move" method
63
+ # @param [Jimson::BatchClient] batch_client
64
+ # @param [Syncano::Clients::Base] client
65
+ # @param [Hash] scope_parameters
66
+ # @param [Array] data_ids
67
+ # @param [Hash] conditions
68
+ # @param [String] new_folder
69
+ # @param [String] new_state
70
+ # @return [Syncano::Response]
71
+ def self.batch_move(batch_client, client, scope_parameters = {}, data_ids = [], conditions = {}, new_folder = nil, new_state = nil)
72
+ perform_move(client, batch_client, scope_parameters, data_ids, conditions, new_folder, new_state)
73
+ end
74
+
75
+ # Wrapper for api "move" method
76
+ # @param [String] new_folder
77
+ # @param [String] new_state
78
+ # @return [Syncano::Resource::DataObject]
79
+ def move(new_folder = nil, new_state = nil)
80
+ perform_move(client, nil, scope_parameters, [id], {}, new_folder, new_state)
81
+ reload!
82
+ end
83
+
84
+ # Batch version of "move" method
85
+ # @param [Jimson::BatchClient] batch_client
86
+ # @param [String] new_folder
87
+ # @param [String] new_state
88
+ # @return [Syncano::Response]
89
+ def batch_move(batch_client, new_folder = nil, new_state = nil)
90
+ perform_move(client, batch_client, scope_parameters, [id], {}, new_folder, new_state)
91
+ end
92
+
93
+ # Wrapper for api "copy" method
94
+ # @param [Syncano::Clients::Base] client
95
+ # @param [Hash] scope_parameters
96
+ # @param [Array] data_ids
97
+ # @return [Array] collection of Syncano::Resource::DataObject objects
98
+ def self.copy(client, scope_parameters = {}, data_ids = [])
99
+ response = perform_copy(client, nil, scope_parameters, data_ids)
100
+ response.data.collect { |attributes| self.new(client, attributes.merge(scope_parameters)) }
101
+ end
102
+
103
+ # Batch version of "move" method
104
+ # @param [Jimson::BatchClient] batch_client
105
+ # @param [Syncano::Clients::Base] client
106
+ # @param [Hash] scope_parameters
107
+ # @param [Array] data_ids
108
+ # @return [Syncano::Response]
109
+ def self.batch_copy(batch_client, client, scope_parameters = {}, data_ids = [])
110
+ perform_copy(client, batch_client, scope_parameters, data_ids)
111
+ end
112
+
113
+ # Wrapper for api "copy" method
114
+ # @return [Syncano::Resource::DataObject]
115
+ def copy
116
+ self.class.copy(client, scope_parameters, id.to_s).try(:first)
117
+ end
118
+
119
+ # Batch version of "copy" method
120
+ # @param [Jimson::BatchClient] batch_client
121
+ # @return [Syncano::Response]
122
+ def batch_copy(batch_client)
123
+ self.class.batch_copy(batch_client, client, scope_parameters, id.to_s)
124
+ end
125
+
126
+ # Wrapper for api "add_parent" method
127
+ # @param [Integer] parent_id
128
+ # @param [TrueClass, FalseClass] remove_other
129
+ # @return [Syncano::Resources::DataObject]
130
+ def add_parent(parent_id, remove_other = false)
131
+ response = perform_add_parent(nil, parent_id, remove_other)
132
+ reload!
133
+ end
134
+
135
+ # Batch version of "add_parent" method
136
+ # @param [Jimson::BatchClient] batch_client
137
+ # @param [Integer] parent_id
138
+ # @param [TrueClass, FalseClass] remove_other
139
+ # @return [Syncano::Response]
140
+ def batch_add_parent(batch_client, parent_id, remove_other = false)
141
+ perform_add_parent(batch_client, parent_id, remove_other)
142
+ end
143
+
144
+ # Wrapper for api "remove_parent" method
145
+ # @param [Integer] parent_id
146
+ # @return [Syncano::Resources::DataObject]
147
+ def remove_parent(parent_id = nil)
148
+ response = perform_remove_parent(nil, parent_id)
149
+ reload!
150
+ end
151
+
152
+ # Batch version of "remove_parent" method
153
+ # @param [Jimson::BatchClient] batch_client
154
+ # @param [Integer] parent_id
155
+ # @return [Syncano::Response]
156
+ def batch_remove_parent(batch_client, parent_id = nil)
157
+ perform_remove_parent(batch_client, parent_id)
158
+ end
159
+
160
+ # Wrapper for api "add_child" method
161
+ # @param [Integer] child_id
162
+ # @param [TrueClass, FalseClass] remove_other
163
+ # @return [Syncano::Resources::DataObject]
164
+ def add_child(child_id, remove_other = false)
165
+ perform_add_child(nil, child_id, remove_other)
166
+ reload!
167
+ end
168
+
169
+ # Batch version of "add_child" method
170
+ # @param [Jimson::BatchClient] batch_client
171
+ # @param [Integer] child_id
172
+ # @param [TrueClass, FalseClass] remove_other
173
+ # @return [Syncano::Response]
174
+ def batch_add_child(batch_client, child_id, remove_other = false)
175
+ perform_add_child(batch_client, child_id, remove_other)
176
+ end
177
+
178
+ # Wrapper for api "remove_child" method
179
+ # @param [Integer] child_id
180
+ # @return [Syncano::Resources::DataObject]
181
+ def remove_child(child_id = nil)
182
+ perform_remove_child(nil, child_id)
183
+ reload!
184
+ end
185
+
186
+ # Batch version of "remove_child" method
187
+ # @param [Jimson::BatchClient] batch_client
188
+ # @param [Integer] child_id
189
+ # @return [Syncano::Response]
190
+ def batch_remove_child(batch_client, child_id = nil)
191
+ perform_remove_child(batch_client, child_id)
192
+ end
193
+
194
+ private
195
+
196
+ self.syncano_model_name = 'data'
197
+ self.scope_parameters = [:project_id, :collection_id]
198
+
199
+ # Prepares hash with attributes used in synchronization with Syncano
200
+ # @return [Hash]
201
+ def self.attributes_to_sync(attributes)
202
+ attributes = attributes.dup
203
+
204
+ if attributes.keys.map(&:to_sym).include?(:image)
205
+ if attributes[:image].blank?
206
+ attributes[:image] = ''
207
+ elsif attributes[:image].is_a?(String)
208
+ attributes[:image] = Base64.encode64(File.read(attributes[:image]))
209
+ else
210
+ attributes.delete(:image)
211
+ end
212
+ end
213
+
214
+ attributes.delete(:user)
215
+ attributes.delete(:created_at)
216
+ attributes.delete(:updated_at)
217
+ attributes.delete(:children_count)
218
+
219
+ attributes
220
+ end
221
+
222
+ # Executes proper count request
223
+ # @param [Syncano::Clients::Base] client
224
+ # @param [Hash] scope_parameters
225
+ # @param [Hash] conditions
226
+ # @return [Syncano::Response]
227
+ def self.perform_count(client, scope_parameters, conditions)
228
+ make_request(client, nil, :count, conditions.merge(scope_parameters))
229
+ end
230
+
231
+ # Executes proper move request
232
+ # @param [Syncano::Clients::Base] client
233
+ # @param [Jimson::BatchClient] batch_client
234
+ # @param [Hash] scope_parameters
235
+ # @param [Array] data_ids
236
+ # @param [Hash] conditions
237
+ # @param [String] new_folder
238
+ # @param [String] new_state
239
+ # @return [Syncano::Response]
240
+ def self.perform_move(client, batch_client, scope_parameters, data_ids, conditions, new_folder, new_state)
241
+ move_params = { new_folder: new_folder, new_state: new_state }.delete_if { |k, v| v.nil? }
242
+ make_request(client, batch_client, :save, [conditions, { data_ids: data_ids }, move_params, scope_parameters].inject(&:merge))
243
+ end
244
+
245
+ # Executes proper copy request
246
+ # @param [Syncano::Clients::Base] client
247
+ # @param [Jimson::BatchClient] batch_client
248
+ # @param [Hash] scope_parameters
249
+ # @param [Array] data_ids
250
+ # @return [Syncano::Response]
251
+ def self.perform_copy(client, batch_client, scope_parameters, data_ids)
252
+ make_request(client, batch_client, :copy, { data_ids: data_ids }.merge(scope_parameters))
253
+ end
254
+
255
+ # Executes proper add_parent request
256
+ # @param [Jimson::BatchClient] batch_client
257
+ # @param [Integer] parent_id
258
+ # @param [TrueClass, FalseClass] remove_other
259
+ # @return [Syncano::Response]
260
+ def perform_add_parent(batch_client, parent_id, remove_other = false)
261
+ self.class.make_request(client, batch_client, :add_parent, scope_parameters.merge(
262
+ self.class.primary_key_name => primary_key,
263
+ parent_id: parent_id,
264
+ remove_other: remove_other
265
+ ))
266
+ end
267
+
268
+ # Executes proper remove_parent request
269
+ # @param [Jimson::BatchClient] batch_client
270
+ # @param [Integer] parent_id
271
+ # @return [Syncano::Response]
272
+ def perform_remove_parent(batch_client, parent_id)
273
+ self.class.make_request(client, batch_client, :remove_parent, scope_parameters.merge(
274
+ self.class.primary_key_name => primary_key,
275
+ parent_id: parent_id
276
+ ))
277
+ end
278
+
279
+ # Executes proper add_child request
280
+ # @param [Jimson::BatchClient] batch_client
281
+ # @param [Integer] child_id
282
+ # @param [TrueClass, FalseClass] remove_other
283
+ # @return [Syncano::Response]
284
+ def perform_add_child(batch_client, child_id, remove_other = false)
285
+ self.class.make_request(client, batch_client, :add_child, scope_parameters.merge(
286
+ self.class.primary_key_name => primary_key,
287
+ child_id: child_id,
288
+ remove_other: remove_other
289
+ ))
290
+ end
291
+
292
+ # Executes proper remove_child request
293
+ # @param [Jimson::BatchClient] batch_client
294
+ # @param [Integer] child_id
295
+ # @return [Syncano::Response]
296
+ def perform_remove_child(batch_client, child_id)
297
+ self.class.make_request(client, batch_client, :remove_child, scope_parameters.merge(
298
+ self.class.primary_key_name => primary_key,
299
+ child_id: child_id
300
+ ))
301
+ end
302
+ end
303
+ end
304
+ end
@@ -0,0 +1,34 @@
1
+ class Syncano
2
+ module Resources
3
+ # Folder resource
4
+ class Folder < ::Syncano::Resources::Base
5
+ # Association has_many :data_objects
6
+ # @return [Syncano::QueryBuilder] query builder for resource Syncano::Resources::DataObject
7
+ def data_objects
8
+ ::Syncano::QueryBuilder.new(client, ::Syncano::Resources::DataObject, scope_parameters.merge(folder: @saved_attributes[:name]))
9
+ end
10
+
11
+ # Wrapper for api "get_one" method with folder_name as a key
12
+ # @param [Syncano::Clients::Base] client
13
+ # @param [String] name
14
+ # @param [Hash] scope_parameters
15
+ # @param [Hash] conditions
16
+ # @return [Syncano::Resources::Folder]
17
+ def self.find_by_name(client, name, scope_parameters = {}, conditions = {})
18
+ find(client, name, scope_parameters, conditions)
19
+ end
20
+
21
+ private
22
+
23
+ self.primary_key = :name
24
+ self.scope_parameters = [:project_id, :collection_id]
25
+
26
+ # Executes proper destroy request
27
+ # @param [Jimson::BatchClient] batch_client
28
+ # @return [Syncano::Response]
29
+ def perform_destroy(batch_client)
30
+ self.class.make_request(client, batch_client, :destroy, scope_parameters.merge(name: primary_key))
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,103 @@
1
+ class Syncano
2
+ module Resources
3
+ # Module used as a scope for classes representing notifications
4
+ module Notifications
5
+ # Base notification class used for inheritance
6
+ class Base < Syncano::Resources::Base
7
+ # Constructor for Syncano::Notifications::Base object
8
+ # @param [Syncano::Clients::Base] client
9
+ # @param [Hash] attributes
10
+ def initialize(client, attributes)
11
+ if attributes.is_a?(::Syncano::Packets::Base)
12
+ super(client, {})
13
+ self.attributes = { source: attributes.source, target: attributes.target, data: attributes.data }
14
+ else
15
+ super(client, attributes)
16
+ end
17
+ end
18
+
19
+ # Proxy method for creating instance of proper subclass
20
+ # @param [Syncano::Clients::Base] client
21
+ # @param [Syncano::Packets::Base] packet
22
+ # @return [Syncano::Notifications::Base]
23
+ def self.instantize_notification(client, packet)
24
+ if packet.message?
25
+ ::Syncano::Resources::Notifications::Message.new(client, packet)
26
+ else
27
+ mapping = {
28
+ new: ::Syncano::Resources::Notifications::Create,
29
+ change: ::Syncano::Resources::Notifications::Update,
30
+ delete: ::Syncano::Resources::Notifications::Destroy
31
+ }
32
+
33
+ mapping[packet.type.to_sym].new(client, packet)
34
+ end
35
+ end
36
+
37
+ # Wrapper for api "get" method
38
+ # Returns all objects from Syncano
39
+ # @param [Syncano::Clients::Base] client
40
+ # @param [Hash] scope_parameters
41
+ # @param [Hash] conditions
42
+ # @return [Array] which contains Syncano::Resources::Base objects
43
+ def self.all(client, scope_parameters = {}, conditions = {})
44
+ mapping = {
45
+ new: ::Syncano::Resources::Notifications::Create,
46
+ change: ::Syncano::Resources::Notifications::Update,
47
+ delete: ::Syncano::Resources::Notifications::Destroy,
48
+ message: ::Syncano::Resources::Notifications::Message
49
+ }
50
+
51
+ response = perform_all(client, scope_parameters, conditions)
52
+ response.data.to_a.collect do |attributes|
53
+ type = attributes.delete(:type)
54
+ mapping[type.to_sym].new(client, attributes.merge(scope_parameters))
55
+ end
56
+ end
57
+
58
+ # Wrapper for api "send" method
59
+ # Creates object in Syncano
60
+ # @param [Syncano::Clients::Base] client
61
+ # @param [Hash] attributes
62
+ # @return [Syncano::Resources::Base]
63
+ def self.create(client, attributes)
64
+ perform_create(client, nil, attributes)
65
+ ::Syncano::Resources::Notifications::Message.new(client, map_to_scope_parameters(attributes))
66
+ end
67
+
68
+ private
69
+
70
+ self.syncano_model_name = 'notification'
71
+ self.crud_class_methods = [:all, :new, :create]
72
+ self.crud_instance_methods = [:save]
73
+
74
+ # Executes proper all request
75
+ # @param [Syncano::Clients::Base] client
76
+ # @param [Hash] scope_parameters
77
+ # @param [Hash] conditions
78
+ # @return [Syncano::Response]
79
+ def self.perform_all(client, scope_parameters, conditions)
80
+ make_request(client, nil, :get_history, conditions.merge(scope_parameters), :history)
81
+ end
82
+
83
+ # Executes proper create request
84
+ # @param [Syncano::Clients::Base] client
85
+ # @param [Jimson::BatchClient] batch_client
86
+ # @param [Hash] attributes
87
+ # @return [Syncano::Response]
88
+ def self.perform_create(client, batch_client, attributes)
89
+ make_request(client, batch_client, :send, attributes_to_sync(attributes))
90
+ end
91
+
92
+ # Executes proper save request
93
+ # @param [Jimson::BatchClient] batch_client
94
+ # @return [Syncano::Response]
95
+ def perform_save(batch_client)
96
+ if new_record?
97
+ self.class.perform_create(client, batch_client, attributes)
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,20 @@
1
+ class Syncano
2
+ module Resources
3
+ module Notifications
4
+ # Notification resource about creating data object - represents notification with type "new"
5
+ class Create < Syncano::Resources::Notifications::Base
6
+
7
+ # Constructor for Syncano::Notifications::Create object
8
+ # @param [Syncano::Clients::Base] client
9
+ # @param [Hash] attributes
10
+ def initialize(client, attributes)
11
+ super(client, attributes)
12
+
13
+ if attributes.is_a?(::Syncano::Packets::Base)
14
+ self[:channel] = attributes.channel
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end