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
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1b60ab5d1367c53ed95d4be884d3e4db7bde5259
4
+ data.tar.gz: 393e5d874af1e291d5281a4072b95654024aae2b
5
+ SHA512:
6
+ metadata.gz: 4a2f19cb61eea646089ec03a2fbd2dc21e84b3425d699e2ca8b29afa8eacbd3e4c868ca8a00bcaa0e91fca80e25a4c29041dc1deb49168a32c060cdbbe8ad0d7
7
+ data.tar.gz: 49755d9f9a68ac8c6ca5d4778e4d0925ae0f8447b7e976399e3cd69972d37876ac23e450525784013e7f07dece271c7d739cd63ef65b81b8972be7765695f36f
data/.gitignore ADDED
@@ -0,0 +1,25 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ .DS_Store
24
+ .com.apple.timemachine.supported
25
+ .idea/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in syncano.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,5 @@
1
+ guard :rspec do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Piotr Zadrożny
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,304 @@
1
+ # Syncano
2
+
3
+ Syncano ruby gem provides communication with Syncano ([www.syncano.com](www.syncano.com)) via HTTPS RESTful interface and TCP sockets.
4
+
5
+ The full source code can be found on [Github](https://github.com/Syncano/syncano-ruby) - feel free to browse or contribute.
6
+
7
+ Click here to learn more about [Syncano](www.syncano.com) or [create an account](https://login.syncano.com/sign_up)!
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'syncano'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install syncano
22
+
23
+ At the end generate initializer with api key and api instance name:
24
+
25
+ $ rails g syncano:install
26
+
27
+ Initializer is not obligatory - you can provide both parameters directly in the client's constructor.
28
+
29
+ ## Usage
30
+
31
+ ### Clients
32
+
33
+ There are two different class of clients. One for JSON RPC interface and one for socket connections with Sync Server. You can use both in quite similar way:
34
+
35
+ ```ruby
36
+ client = Syncano.client
37
+
38
+ client = Syncano.sync_client
39
+ ```
40
+
41
+ You can provide specific api credentials when you are initializing your client:
42
+
43
+ ```ruby
44
+ client = Syncano.client(api_key: 'api key', instance_name: 'instance name')
45
+
46
+ client = Syncano.sync_client(api_key: 'api key', instance_name: 'instance name')
47
+ ```
48
+
49
+ Sync client has some additional features like:
50
+
51
+ * managing connections
52
+ ```ruby
53
+ client.connect
54
+ client.reconnect
55
+ client.disconnect
56
+ ```
57
+ * managing callbacks for handling notifications (it is described later in this document)
58
+
59
+ ### Resources
60
+
61
+ Syncano gem utilizes an ActiveRecord pattern for managing resources. You can use it in similar way with both type of clients.
62
+
63
+ Below is a list of standard methods implemented in resources.
64
+
65
+ * objects.all(parameters)
66
+ * objects.count(parameters)
67
+ * objects.first(parameters)
68
+ * objects.last(parameters)
69
+ * objects.find(id)
70
+ * objects.new(attributes)
71
+ * objects.create(attributes)
72
+ * object.update(attributes)
73
+ * object.save
74
+ * object.destroy
75
+
76
+ Some of resources do not implement all standard methods and others have some custom methods, ie. data_object.copy.
77
+
78
+ Every resource has attributes which can be accessed as a hash ie.:
79
+
80
+ * object[:attribute]
81
+ * object[:attribute] = 'value'
82
+ * object.attributes = { attribute: 'value' }
83
+
84
+ Below is a list of all implemented resources with information about what methods are implemented and usage examples.
85
+
86
+ #### Project
87
+
88
+ Implements all standard methods.
89
+
90
+ ##### Examples
91
+
92
+ * Getting all projects
93
+
94
+ ```ruby
95
+ Syncano.projects.all
96
+ ```
97
+
98
+ * Creating a project
99
+
100
+ ```ruby
101
+ Syncano.projects.create(name: 'Project name')
102
+ ```
103
+
104
+ * Updating a project
105
+
106
+ ```ruby
107
+ project[:description] = 'Lorem ipsum'
108
+ project.save
109
+ ```
110
+
111
+ #### Collection
112
+
113
+ Implements all standard methods and following custom:
114
+
115
+ * collections.find_by_key(collection_key)
116
+ * collection.activate
117
+ * collection.deactivate
118
+ * collection.add_tag(tag, weight, remove_others)
119
+ * collection.delete_tag(tag)
120
+
121
+ ##### Examples
122
+
123
+ * Getting all
124
+
125
+ ```ruby
126
+ project.collections.all
127
+ ```
128
+
129
+ * Finding by key
130
+
131
+ ```ruby
132
+ project.collections.find_by_key(collection_key)
133
+ ```
134
+
135
+ * Activating
136
+
137
+ ```ruby
138
+ collection.activate
139
+ ```
140
+
141
+ * Adding tags
142
+
143
+ ```ruby
144
+ collection1.add_tags(['tag1', 'tag2'], 3)
145
+ collection2.add_tags('tag3', 1, true)
146
+ ```
147
+
148
+ #### Folder
149
+
150
+ Implements all standard methods and following custom:
151
+
152
+ * folders.find_by_name(folder_name)
153
+
154
+ Find method uses folder name as a key.
155
+
156
+ ##### Examples
157
+
158
+ * Getting one
159
+
160
+ ```ruby
161
+ collection.folders.find(folder_name)
162
+ collection.folders.find_by_name(folder_name)
163
+ ```
164
+
165
+ #### Data object
166
+
167
+ Implements all standard methods and following custom:
168
+
169
+ * data_objects.find_by_key(data_object_key)
170
+ * data_objects.move(data_object_ids, new_folder, new_state)
171
+ * data_object.move(new_folder, new_state)
172
+ * data_objects.copy(data_object_ids)
173
+ * data_object.copy
174
+ * data_object.add_parent(parent_id, remove_other)
175
+ * data_object.remove_parent(parent_id)
176
+ * data_object.add_child(parent_id, remove_other)
177
+ * data_object.remove_child(parent_id)
178
+
179
+ ##### Examples
180
+
181
+ * Moving data object to the new folder
182
+
183
+ ```ruby
184
+ data_object.move('new_folder')
185
+ ```
186
+
187
+ * Copying two data objects
188
+
189
+ ```ruby
190
+ collection.data_objects.copy([112, 3871])
191
+ ```
192
+
193
+ * Adding parent to the data object
194
+
195
+ ```ruby
196
+ data_object.add_parent(parent_object_id, true)
197
+ ```
198
+
199
+ #### Admin
200
+
201
+ Implements all standard methods and following custom:
202
+
203
+ * admin.find_by_email(email)
204
+
205
+ #### Api key
206
+
207
+ Implements all standard methods.
208
+
209
+ #### Role
210
+
211
+ Implements only following standard methods:
212
+
213
+ * role.all
214
+ * role.first
215
+ * role.last
216
+ * role.count
217
+
218
+ #### User
219
+
220
+ Implements all standard methods.
221
+
222
+ ### Batch requests
223
+
224
+ It is possible to make batch requests to the JSON RPC endpoint. You do not have to care about batch requests limits specified in the Syncano api docs. This library will care about queuing for you.
225
+
226
+ ```ruby
227
+ client = Syncano.client
228
+ responses = client.batch do |queue|
229
+ queue << collection.batch.save
230
+ queue << collection.data_objects.batch.create(title: 'Lorem ipsum')
231
+ queue.add(data_object.batch.destroy)
232
+ end
233
+ ```
234
+
235
+ There is no difference between "queue.add" and "queue <<" methods.
236
+
237
+ In the above example variable responses will contain three Syncano::Response objects.
238
+ Remember that batch responses do not change objects used in batch requests. If you want to see changes you have to reload them:
239
+
240
+ ```ruby
241
+ collection.reload
242
+ ```
243
+
244
+ ### Notifications
245
+
246
+ Main advantage of using Sync Server are real time notifications. This concept is well described in the Syncano api documentation.
247
+
248
+ #### Subscriptions
249
+
250
+ Before you will receive any notification, you have to subscribe to some project or collection:
251
+
252
+ ```ruby
253
+ client = Syncano.sync_client
254
+ project = client.project.find(project_id)
255
+ project.subscribe
256
+ ```
257
+
258
+ If you want to stop receiving notifications, you have to unsubscribe:
259
+ ```ruby
260
+ project.unsubscribe
261
+ ```
262
+
263
+ You can also list all active subscriptions:
264
+ ```ruby
265
+ client.subscriptions.all
266
+ ```
267
+
268
+ #### Handling notifications
269
+
270
+ Notifications are handled by callbacks passed to the sync client:
271
+
272
+ ```ruby
273
+ client.append_callback(:callback_name) do |notification|
274
+ p "We have received a new notification #{notification.inspect}! Yaaay!"
275
+ end
276
+ ```
277
+
278
+ Callbacks form a queue. You can add new callback to the end of the queue (like above) or to the beginning:
279
+
280
+ ```ruby
281
+ client.prepend_callback(:callback_name) do |notification|
282
+ p "We have received a new notification #{notification.inspect}! Yaaay!"
283
+ end
284
+ ```
285
+
286
+ To delete callback from the queue just call remove_callback method:
287
+
288
+ ```ruby
289
+ client.remove_callback(:callback_name)
290
+ ```
291
+
292
+ ### Errors and exceptions
293
+
294
+ This library does not implement any validations. All errors from the api will cause throwing an exception.
295
+ It is thought that user will create his own validation mechanisms specific not only for restrictions imposed by the Syncano, but also for his own logic.
296
+ It can be compared to the exceptions after violating constraints in the MySQL database.
297
+
298
+ ## Contributing
299
+
300
+ 1. Fork it
301
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
302
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
303
+ 4. Push to the branch (`git push origin my-new-feature`)
304
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task default: :spec
7
+ task test: :spec
@@ -0,0 +1,17 @@
1
+ module Syncano
2
+ # Module for generators used implemented in the gem
3
+ module Generators
4
+ # Install generator used for initializing gem in a Rails application
5
+ class InstallGenerator < Rails::Generators::Base
6
+ source_root File.expand_path("../templates", __FILE__)
7
+
8
+ # Copies templates for initializers
9
+ def create_initializers
10
+ Dir["#{self.class.source_root}/initializers/*.rb"].each do |filepath|
11
+ name = File.basename(filepath)
12
+ template "initializers/#{name}", "config/initializers/#{name}"
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,4 @@
1
+ # Syncano instance name
2
+ SYNCANO_INSTANCE_NAME = 'instance_name'
3
+ # Syncano api key
4
+ SYNCANO_API_KEY = 'api_key'
data/lib/syncano.rb ADDED
@@ -0,0 +1,92 @@
1
+ require 'syncano/version'
2
+
3
+ # Main class used for instantizing clients and as scope for other classes
4
+ class Syncano
5
+ # Used for initializing Syncano Rest Client
6
+ # @param [Hash] options with keys: instance_name, api_key which can be also provided as constants in the initializer
7
+ # @return [Syncano::Clients::Rest] Syncano client.
8
+ def self.client(options = {})
9
+ auth_data = self.auth_data(options)
10
+ Syncano::Clients::Rest.new(auth_data[:instance_name], auth_data[:api_key])
11
+ end
12
+
13
+ # Used for initializing Syncano Sync Client
14
+ # @param [Hash] options with keys: instance_name, api_key which can be also provided as constants in the initializer
15
+ # @return [Syncano::Clients::Rest] Syncano client.
16
+ def self.sync_client(options = {})
17
+ auth_data = self.auth_data(options)
18
+ client = Syncano::Clients::Sync.instance(auth_data[:instance_name], auth_data[:api_key])
19
+ client.connect
20
+ client
21
+ end
22
+
23
+ private
24
+
25
+ # Prepares hash with auth data from options or constants in initializer
26
+ # @param [Hash] options with keys: instance_name, api_key which can be also provided as constants in the initializer
27
+ # @return [Hash]
28
+ def self.auth_data(options = {})
29
+ instance_name = options[:instance_name] || ::SYNCANO_INSTANCE_NAME
30
+ raise 'Syncano instance name cannot be blank!' if instance_name.nil?
31
+
32
+ api_key = options[:api_key] || ::SYNCANO_API_KEY
33
+ raise 'Syncano api key cannot be blank!' if api_key.nil?
34
+
35
+ { instance_name: instance_name, api_key: api_key }
36
+ end
37
+ end
38
+
39
+ # Jimson client
40
+ require 'jimson/client'
41
+ require 'syncano/jimson_client'
42
+
43
+ # Multi Json
44
+ require 'multi_json'
45
+
46
+ # Eventmachine
47
+ require 'eventmachine'
48
+
49
+ # Singleton
50
+ require 'singleton'
51
+
52
+ # ActiveSupport
53
+ require 'active_support/core_ext/hash/indifferent_access'
54
+ require 'active_support/core_ext/class/attribute.rb'
55
+ require 'active_support/core_ext/object/blank.rb'
56
+ require 'active_support/json/decoding.rb'
57
+ require 'active_support/json/encoding.rb'
58
+ require 'active_support/time_with_zone.rb'
59
+
60
+ # Syncano
61
+ require 'syncano/errors'
62
+ require 'syncano/clients/base'
63
+ require 'syncano/clients/rest'
64
+ require 'syncano/clients/sync'
65
+ require 'syncano/sync_connection'
66
+ require 'syncano/query_builder'
67
+ require 'syncano/batch_queue'
68
+ require 'syncano/batch_queue_element'
69
+ require 'syncano/response'
70
+ require 'syncano/resources/base'
71
+ require 'syncano/resources/admin'
72
+ require 'syncano/resources/api_key'
73
+ require 'syncano/resources/data_object'
74
+ require 'syncano/resources/collection'
75
+ require 'syncano/resources/folder'
76
+ require 'syncano/resources/project'
77
+ require 'syncano/resources/role'
78
+ require 'syncano/resources/subscription'
79
+ require 'syncano/resources/user'
80
+ require 'syncano/packets/base'
81
+ require 'syncano/packets/auth'
82
+ require 'syncano/packets/call'
83
+ require 'syncano/packets/call_response'
84
+ require 'syncano/packets/error'
85
+ require 'syncano/packets/message'
86
+ require 'syncano/packets/notification'
87
+ require 'syncano/packets/ping'
88
+ require 'syncano/resources/notifications/base'
89
+ require 'syncano/resources/notifications/create'
90
+ require 'syncano/resources/notifications/update'
91
+ require 'syncano/resources/notifications/destroy'
92
+ require 'syncano/resources/notifications/message'