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,7 @@
1
+ class Syncano
2
+ module Packets
3
+ # Class representing auth packets used in communication with the Sync Server
4
+ class Auth < Syncano::Packets::Base
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,64 @@
1
+ class Syncano
2
+ # Module used as a scope for classes representing packets
3
+ module Packets
4
+ # Base class for representing packets used in communication with the Sync Server
5
+ class Base
6
+ attr_reader :object, :timestamp
7
+
8
+ # Constructor for Syncano::Packets::Base object
9
+ # @param [Hash] attributes
10
+ def initialize(attributes)
11
+ super()
12
+ self.timestamp = attributes[:timestamp]
13
+ self.object = attributes[:object]
14
+ end
15
+
16
+ # Proxy method for creating instance of proper subclass
17
+ # @param [Hash] data
18
+ # @return [Syncano::Packets::Base]
19
+ def self.instantize_packet(data)
20
+ mapping = {
21
+ auth: ::Syncano::Packets::Auth,
22
+ call: ::Syncano::Packets::Call,
23
+ callresponse: ::Syncano::Packets::CallResponse,
24
+ error: ::Syncano::Packets::Error,
25
+ message: ::Syncano::Packets::Message,
26
+ new: ::Syncano::Packets::Notification,
27
+ change: ::Syncano::Packets::Notification,
28
+ delete: ::Syncano::Packets::Notification,
29
+ ping: ::Syncano::Packets::Ping
30
+ }
31
+
32
+ mapping[data[:type].to_sym].new(data)
33
+ end
34
+
35
+ # Returns true if is a notification packet
36
+ # @return [TrueClass, FalseClass]
37
+ def notification?
38
+ false
39
+ end
40
+
41
+ # Returns true if is a ping packet
42
+ # @return [TrueClass, FalseClass]
43
+ def ping?
44
+ false
45
+ end
46
+
47
+ # Returns true if is a call response packet
48
+ # @return [TrueClass, FalseClass]
49
+ def call_response?
50
+ false
51
+ end
52
+
53
+ # Returns true if is a message packet
54
+ # @return [TrueClass, FalseClass]
55
+ def message?
56
+ false
57
+ end
58
+
59
+ private
60
+
61
+ attr_writer :object, :timestamp
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,34 @@
1
+ class Syncano
2
+ module Packets
3
+ # Class representing call packets used in communication with the Sync Server
4
+ class Call < Syncano::Packets::Base
5
+ attr_reader :message_id, :resource_name, :method_name, :data
6
+
7
+ # Constructor for Syncano::Packets::Call object
8
+ # @param [Hash] attributes
9
+ def initialize(attributes)
10
+ super(attributes)
11
+ self.resource_name = attributes[:resource_name]
12
+ self.method_name = attributes[:method_name]
13
+ self.data = attributes[:data]
14
+ self.message_id = attributes[:message_id] || rand(10**12)
15
+ end
16
+
17
+ # Overwritten method for preparing hash for json serialization
18
+ # @param [Hash] options
19
+ # @return [Hash]
20
+ def as_json(options = {})
21
+ {
22
+ type: 'call',
23
+ method: "#{resource_name}.#{method_name}",
24
+ params: data,
25
+ message_id: message_id.to_s
26
+ }
27
+ end
28
+
29
+ private
30
+
31
+ attr_writer :message_id, :resource_name, :method_name, :data
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,33 @@
1
+ class Syncano
2
+ module Packets
3
+ # Class representing call response packets used in communication with the Sync Server
4
+ class CallResponse < Syncano::Packets::Base
5
+ attr_reader :message_id, :data, :result
6
+
7
+ # Constructor for Syncano::Packets::CallResponse object
8
+ # @param [Hash] attributes
9
+ def initialize(attributes)
10
+ super(attributes)
11
+ self.message_id = attributes[:message_id]
12
+ self.data = attributes[:data]
13
+ self.result = attributes[:result]
14
+ end
15
+
16
+ # Prepares hash in response format
17
+ # @return [Hash]
18
+ def to_response
19
+ data.merge(result: result)
20
+ end
21
+
22
+ # Returns true if is a call response packet
23
+ # @return [TrueClass, FalseClass]
24
+ def call_response?
25
+ true
26
+ end
27
+
28
+ private
29
+
30
+ attr_writer :message_id, :data, :result
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,19 @@
1
+ class Syncano
2
+ module Packets
3
+ # Class representing error packets used in communication with the Sync Server
4
+ class Error < Syncano::Packets::Base
5
+ attr_reader :error
6
+
7
+ # Constructor for Syncano::Packets::Error object
8
+ # @param [Hash] attributes
9
+ def initialize(attributes)
10
+ super(attributes)
11
+ self.error = attributes[:error]
12
+ end
13
+
14
+ private
15
+
16
+ attr_writer :error
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,30 @@
1
+ class Syncano
2
+ module Packets
3
+ # Class representing message packets used in communication with the Sync Server
4
+ class Message < Syncano::Packets::Base
5
+ attr_accessor :id, :source, :target, :data
6
+
7
+ # Constructor for Syncano::Packets::Message object
8
+ # @param [Hash] attributes
9
+ def initialize(attributes)
10
+ super(attributes)
11
+ self.id = attributes[:id]
12
+ self.source = attributes[:source]
13
+ self.target = attributes[:target]
14
+ self.data = attributes[:data]
15
+ end
16
+
17
+ # Returns true if is a notification packet
18
+ # @return [TrueClass, FalseClass]
19
+ def notification?
20
+ true
21
+ end
22
+
23
+ # Returns true if is a message packet
24
+ # @return [TrueClass, FalseClass]
25
+ def message?
26
+ true
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,39 @@
1
+ class Syncano
2
+ module Packets
3
+ # Class representing notification packets used in communication with the Sync Server
4
+ class Notification < Syncano::Packets::Base
5
+ attr_reader :id, :type, :channel, :source, :target, :data
6
+
7
+ # Constructor for Syncano::Packets::Notification object
8
+ # @param [Hash] attributes
9
+ def initialize(attributes)
10
+ super(attributes)
11
+ self.id = attributes[:id]
12
+ self.type = attributes[:type]
13
+ self.channel = attributes[:channel]
14
+ self.source = attributes[:source]
15
+ self.target = attributes[:target]
16
+
17
+ if type == 'change'
18
+ self.data = {
19
+ added: attributes[:add],
20
+ updated: attributes[:replace],
21
+ deleted: attributes[:delete]
22
+ }
23
+ else
24
+ self.data = attributes[:data]
25
+ end
26
+ end
27
+
28
+ # Returns true if is a notification packet
29
+ # @return [TrueClass, FalseClass]
30
+ def notification?
31
+ true
32
+ end
33
+
34
+ private
35
+
36
+ attr_writer :id, :type, :channel, :source, :target, :data
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,12 @@
1
+ class Syncano
2
+ module Packets
3
+ # Class representing ping packets used in communication with the Sync Server
4
+ class Ping < Syncano::Packets::Base
5
+ # Returns true if is a ping packet
6
+ # @return [TrueClass, FalseClass]
7
+ def ping?
8
+ true
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,144 @@
1
+ class Syncano
2
+ # Proxy class for creating proper requests to api through ActiveRecord pattern
3
+ class QueryBuilder
4
+ # Constructor for Syncano::QueryBuilder object
5
+ # @param [Syncano::Clients::Base] client
6
+ # @param [String] resource_class
7
+ # @param [Hash] scope_parameters
8
+ def initialize(client, resource_class, scope_parameters = {})
9
+ self.client = client
10
+ self.resource_class = resource_class
11
+ self.scope_parameters = scope_parameters
12
+ end
13
+
14
+ # Proxy for preparing batch requests
15
+ # ie. query_builder.batch.create will prepare BatchQueueElement
16
+ # which invokes batch_create method on query builder object
17
+ # @return [Syncano::BatchQueueElement]
18
+ def batch
19
+ ::Syncano::BatchQueueElement.new(self)
20
+ end
21
+
22
+ # Proxy for calling "all" method on the resource object
23
+ # @param [Hash] conditions
24
+ # @return [Array] collection of Syncano::Resources::Base objects
25
+ def all(conditions = {})
26
+ resource_class.all(client, conditions.merge(scope_parameters))
27
+ end
28
+
29
+ # Proxy for calling "count" method on the resource object
30
+ # @param [Hash] conditions
31
+ # @return [Integer]
32
+ def count(conditions = {})
33
+ resource_class.count(client, conditions.merge(scope_parameters))
34
+ end
35
+
36
+ # Returns first element from all returned by "all" method
37
+ # @param [Hash] conditions
38
+ # @return [Syncano::Resources::Base]
39
+ def first(conditions = {})
40
+ all(conditions).first
41
+ end
42
+
43
+ # Returns last element from all returned by "all" method
44
+ # @param [Hash] conditions
45
+ # @return [Syncano::Resources::Base]
46
+ def last(conditions = {})
47
+ all(conditions).last
48
+ end
49
+
50
+ # Proxy for calling "find" method on the resource object
51
+ # @param [Integer, String] key
52
+ # @param [Hash] conditions
53
+ # @return [Syncano::Resources::Base]
54
+ def find(key, conditions = {})
55
+ resource_class.find(client, key, scope_parameters, conditions)
56
+ end
57
+
58
+ # Proxy for calling "find_by_key" method on the resource object
59
+ # @param [String] key
60
+ # @param [Hash] conditions
61
+ # @return [Syncano::Resources::Base]
62
+ def find_by_key(key, conditions = {})
63
+ resource_class.find_by_key(client, key, scope_parameters, conditions)
64
+ end
65
+
66
+ # Proxy for calling "find_by_name" method on the resource object
67
+ # @param [String] name
68
+ # @param [Hash] conditions
69
+ # @return [Syncano::Resources::Base]
70
+ def find_by_name(name, conditions = {})
71
+ resource_class.find_by_name(client, name, scope_parameters, conditions)
72
+ end
73
+
74
+ # Proxy for calling "find_by_email" method on the resource object
75
+ # @param [String] email
76
+ # @param [Hash] conditions
77
+ # @return [Syncano::Resources::Base]
78
+ def find_by_email(email, conditions = {})
79
+ resource_class.find_by_email(client, email, scope_parameters, conditions)
80
+ end
81
+
82
+ # Proxy for calling "new" method on the resource object
83
+ # @param [Hash] attributes
84
+ # @return [Syncano::Resources::Base]
85
+ def new(attributes = {})
86
+ resource_class.new(client, attributes.merge(scope_parameters))
87
+ end
88
+
89
+ # Proxy for calling "create" method on the resource object
90
+ # @param [Hash] attributes
91
+ # @return [Syncano::Resources::Base]
92
+ def create(attributes)
93
+ resource_class.create(client, attributes.merge(scope_parameters))
94
+ end
95
+
96
+ # Proxy for calling "batch_create" method on the resource object
97
+ # @param [Jimson::Client] batch_client
98
+ # @param [Hash] attributes
99
+ # @return [Syncano::Response]
100
+ def batch_create(batch_client, attributes)
101
+ resource_class.batch_create(batch_client, client, attributes.merge(scope_parameters))
102
+ end
103
+
104
+ # Proxy for calling "copy" method on the resource object
105
+ # @param [Array] ids
106
+ # @return [Array] collection of Syncano::Resource objects
107
+ def copy(ids)
108
+ resource_class.copy(client, scope_parameters, ids)
109
+ end
110
+
111
+ # Proxy for calling "batch_copy" method on the resource object
112
+ # @param [Jimson::Client] batch_client
113
+ # @param [Array] ids
114
+ # @return [Syncano::Response]
115
+ def batch_copy(batch_client, ids)
116
+ resource_class.batch_copy(batch_client, scope_parameters, ids)
117
+ end
118
+
119
+ # Proxy for calling "move" method on the resource object
120
+ # @param [Array] ids
121
+ # @param [Hash] conditions
122
+ # @param [String] new_folder
123
+ # @param [String] new_state
124
+ # @return [Array] collection of Syncano::Resource objects
125
+ def move(ids, conditions = {}, new_folder = nil, new_state = nil)
126
+ resource_class.move(client, scope_parameters, ids, conditions, new_folder, new_state)
127
+ end
128
+
129
+ # Proxy for calling "batch_move" method on the resource object
130
+ # @param [Jimson::Client] batch_client
131
+ # @param [Array] ids
132
+ # @param [Hash] conditions
133
+ # @param [String] new_folder
134
+ # @param [String] new_state
135
+ # @return [Syncano::Response]
136
+ def batch_move(batch_client, ids, conditions = {}, new_folder = nil, new_state = nil)
137
+ resource_class.batch_move(batch_client, scope_parameters, ids, conditions, new_folder, new_state)
138
+ end
139
+
140
+ private
141
+
142
+ attr_accessor :client, :resource_class, :scope_parameters
143
+ end
144
+ end
@@ -0,0 +1,26 @@
1
+ class Syncano
2
+ module Resources
3
+ # Admin resource
4
+ class Admin < ::Syncano::Resources::Base
5
+ # Wrapper for api "get_one" method with admin_email as a key
6
+ # @param [Syncano::Clients::Base] client
7
+ # @param [String] email
8
+ # @param [Hash] scope_parameters
9
+ # @param [Hash] conditions
10
+ # @return [Syncano::Resources::Admin]
11
+ def self.find_by_email(client, email, scope_parameters = {}, conditions = {})
12
+ perform_find(client, :admin_email, email, scope_parameters, conditions)
13
+ end
14
+
15
+ # Wrapper for api "new" method
16
+ # Creates object in Syncano
17
+ # @param [Syncano::Clients::Base] client
18
+ # @param [Hash] attributes
19
+ # @return [Syncano::Resources::Base]
20
+ def self.create(client, attributes)
21
+ perform_create(client, nil, attributes)
22
+ all(client, map_to_scope_parameters(attributes)).last
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,46 @@
1
+ class Syncano
2
+ module Resources
3
+ # Api key resource
4
+ class ApiKey < ::Syncano::Resources::Base
5
+ # Overwritten constructor with initializing associated role object
6
+ # @param [Syncano::Clients::Base] client
7
+ # @param [Hash] attributes
8
+ def initialize(client, attributes = {})
9
+ super(client, attributes)
10
+ if @attributes[:role].is_a?(Hash)
11
+ @attributes[:role] = ::Syncano::Resources::Role.new(@attributes[:role])
12
+ end
13
+
14
+ if @saved_attributes[:role].is_a?(Hash)
15
+ @saved_attributes[:role] = ::Syncano::Resources::Role.new(@saved_attributes[:role])
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ # Prepares attributes to synchronizing with Syncano
22
+ # @param [Hash] attributes
23
+ # @return [Hash] prepared attributes
24
+ def self.attributes_to_sync(attributes)
25
+ attributes = attributes.dup
26
+ attributes.delete(:role)
27
+
28
+ attributes
29
+ end
30
+
31
+ # Name of attribute used as primary key
32
+ # @return [Symbol]
33
+ def self.primary_key_name
34
+ :api_client_id
35
+ end
36
+
37
+ # Executes proper update request
38
+ # @param [Jimson::BatchClient] batch_client
39
+ # @param [Hash] attributes
40
+ # @return [Syncano::Response]
41
+ def perform_update(batch_client, attributes)
42
+ self.class.make_request(client, batch_client, :update_description, self.class.attributes_to_sync(attributes).merge(self.class.primary_key_name.to_sym => primary_key))
43
+ end
44
+ end
45
+ end
46
+ end