syncano 3.1.4 → 4.0.0.alpha

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 (92) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +4 -1
  3. data/.rspec +2 -0
  4. data/.rubocop.yml +3 -0
  5. data/.ruby-version +1 -0
  6. data/Gemfile +25 -1
  7. data/Guardfile +22 -4
  8. data/README.md +68 -447
  9. data/Rakefile +48 -5
  10. data/circle.yml +10 -0
  11. data/lib/active_attr/dirty.rb +3 -17
  12. data/lib/active_attr/typecasting/hash_typecaster.rb +34 -0
  13. data/lib/active_attr/typecasting_override.rb +29 -0
  14. data/lib/syncano.rb +53 -92
  15. data/lib/syncano/api.rb +13 -0
  16. data/lib/syncano/connection.rb +97 -0
  17. data/lib/syncano/model/associations.rb +121 -0
  18. data/lib/syncano/{active_record/association → model/associations}/base.rb +5 -5
  19. data/lib/syncano/{active_record/association → model/associations}/belongs_to.rb +6 -6
  20. data/lib/syncano/{active_record/association → model/associations}/has_many.rb +15 -9
  21. data/lib/syncano/{active_record/association → model/associations}/has_one.rb +4 -4
  22. data/lib/syncano/model/base.rb +257 -0
  23. data/lib/syncano/{active_record → model}/callbacks.rb +16 -13
  24. data/lib/syncano/{active_record → model}/scope_builder.rb +53 -69
  25. data/lib/syncano/query_builder.rb +19 -129
  26. data/lib/syncano/resources.rb +126 -0
  27. data/lib/syncano/resources/base.rb +304 -300
  28. data/lib/syncano/resources/collection.rb +19 -223
  29. data/lib/syncano/resources/space.rb +29 -0
  30. data/lib/syncano/schema.rb +86 -0
  31. data/lib/syncano/schema/attribute_definition.rb +83 -0
  32. data/lib/syncano/schema/resource_definition.rb +36 -0
  33. data/lib/syncano/scope.rb +10 -0
  34. data/lib/syncano/version.rb +3 -4
  35. data/spec/integration/syncano_spec.rb +228 -0
  36. data/spec/spec_helper.rb +15 -9
  37. data/spec/unit/api_spec.rb +5 -0
  38. data/spec/unit/connection_spec.rb +137 -0
  39. data/spec/unit/query_builder_spec.rb +75 -0
  40. data/spec/unit/resources/collection_spec.rb +36 -0
  41. data/spec/unit/resources/space_spec.rb +28 -0
  42. data/spec/unit/resources_base_spec.rb +185 -0
  43. data/spec/unit/schema/attribute_definition_spec.rb +18 -0
  44. data/spec/unit/schema/resource_definition_spec.rb +25 -0
  45. data/spec/unit/schema_spec.rb +3532 -0
  46. data/spec/unit/syncano_spec.rb +63 -0
  47. data/syncano.gemspec +8 -14
  48. metadata +85 -210
  49. data/lib/generators/syncano/install_generator.rb +0 -17
  50. data/lib/generators/syncano/templates/initializers/syncano.rb +0 -7
  51. data/lib/syncano/active_record/associations.rb +0 -112
  52. data/lib/syncano/active_record/base.rb +0 -318
  53. data/lib/syncano/batch_queue.rb +0 -58
  54. data/lib/syncano/batch_queue_element.rb +0 -33
  55. data/lib/syncano/clients/base.rb +0 -123
  56. data/lib/syncano/clients/rest.rb +0 -79
  57. data/lib/syncano/clients/sync.rb +0 -164
  58. data/lib/syncano/errors.rb +0 -17
  59. data/lib/syncano/jimson_client.rb +0 -66
  60. data/lib/syncano/packets/auth.rb +0 -27
  61. data/lib/syncano/packets/base.rb +0 -70
  62. data/lib/syncano/packets/call.rb +0 -34
  63. data/lib/syncano/packets/call_response.rb +0 -33
  64. data/lib/syncano/packets/error.rb +0 -19
  65. data/lib/syncano/packets/message.rb +0 -30
  66. data/lib/syncano/packets/notification.rb +0 -39
  67. data/lib/syncano/packets/ping.rb +0 -12
  68. data/lib/syncano/resources/admin.rb +0 -26
  69. data/lib/syncano/resources/api_key.rb +0 -108
  70. data/lib/syncano/resources/data_object.rb +0 -316
  71. data/lib/syncano/resources/folder.rb +0 -88
  72. data/lib/syncano/resources/notifications/base.rb +0 -103
  73. data/lib/syncano/resources/notifications/create.rb +0 -20
  74. data/lib/syncano/resources/notifications/destroy.rb +0 -20
  75. data/lib/syncano/resources/notifications/message.rb +0 -9
  76. data/lib/syncano/resources/notifications/update.rb +0 -24
  77. data/lib/syncano/resources/project.rb +0 -96
  78. data/lib/syncano/resources/role.rb +0 -11
  79. data/lib/syncano/resources/subscription.rb +0 -12
  80. data/lib/syncano/resources/user.rb +0 -65
  81. data/lib/syncano/response.rb +0 -22
  82. data/lib/syncano/sync_connection.rb +0 -133
  83. data/spec/admins_spec.rb +0 -16
  84. data/spec/api_keys_spec.rb +0 -34
  85. data/spec/collections_spec.rb +0 -67
  86. data/spec/data_objects_spec.rb +0 -113
  87. data/spec/folders_spec.rb +0 -39
  88. data/spec/notifications_spec.rb +0 -43
  89. data/spec/projects_spec.rb +0 -35
  90. data/spec/roles_spec.rb +0 -13
  91. data/spec/sync_resources_spec.rb +0 -35
  92. data/spec/syncano_spec.rb +0 -9
@@ -1,20 +0,0 @@
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
@@ -1,20 +0,0 @@
1
- class Syncano
2
- module Resources
3
- module Notifications
4
- # Notification resource about destroying data object - represents notification with type "delete"
5
- class Destroy < 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[:target] = attributes.target
15
- end
16
- end
17
- end
18
- end
19
- end
20
- end
@@ -1,9 +0,0 @@
1
- class Syncano
2
- module Resources
3
- module Notifications
4
- # Notification resource representing general notifications
5
- class Message < Syncano::Resources::Notifications::Base
6
- end
7
- end
8
- end
9
- end
@@ -1,24 +0,0 @@
1
- class Syncano
2
- module Resources
3
- module Notifications
4
- # Notification resource about updating data object - represents notification with type "change"
5
- class Update < Syncano::Resources::Notifications::Base
6
- # Constructor for Syncano::Notifications::Update object
7
- # @param [Syncano::Clients::Base] client
8
- # @param [Hash] attributes
9
- def initialize(client, attributes)
10
- super(client, attributes)
11
-
12
- if attributes.is_a?(::Syncano::Packets::Base)
13
- self.attributes = {
14
- added: attributes.data[:added],
15
- updated: attributes.data[:updated],
16
- deleted: attributes.data[:deleted],
17
- target: attributes.target
18
- }
19
- end
20
- end
21
- end
22
- end
23
- end
24
- end
@@ -1,96 +0,0 @@
1
- class Syncano
2
- module Resources
3
- # Project resource
4
- class Project < ::Syncano::Resources::Base
5
- # Association has_many :collections
6
- # @return [Syncano::QueryBuilder] query builder for resource Syncano::Resources::Collection
7
- def collections
8
- ::Syncano::QueryBuilder.new(client, ::Syncano::Resources::Collection, project_id: id)
9
- end
10
-
11
- # Wrapper for api "subscription.subscribe_project" method
12
- # @return [Syncano::Resource::Project]
13
- def subscribe
14
- perform_subscribe
15
- reload!
16
- end
17
-
18
- # Wrapper for api "subscription.unsubscribe_project" method
19
- # @return [Syncano::Resource::Project]
20
- def unsubscribe
21
- perform_unsubscribe
22
- reload!
23
- end
24
-
25
- # Wrapper for api "authorize" method
26
- # @param [Integer] api_client_id
27
- # @param [String] permission
28
- # @return [Syncano::Resources::Base]
29
- def authorize(api_client_id, permission)
30
- perform_authorize(nil, api_client_id: api_client_id, permission: permission)
31
- self
32
- end
33
-
34
- # Wrapper for api "authorize" method
35
- # @param [Jimson::BatchClient] batch_client
36
- # @param [Integer] api_client_id
37
- # @param [String] permission
38
- # @return [Syncano::Resources::Base]
39
- def batch_authorize(batch_client, api_client_id, permission)
40
- perform_authorize(batch_client, api_client_id: api_client_id, permission: permission)
41
- self
42
- end
43
-
44
- # Wrapper for api "deauthorize" method
45
- # @param [Integer] api_client_id
46
- # @param [String] permission
47
- # @return [Syncano::Resources::Base]
48
- def deauthorize(api_client_id, permission)
49
- perform_deauthorize(nil, api_client_id: api_client_id, permission: permission)
50
- self
51
- end
52
-
53
- # Wrapper for api "deauthorize" method
54
- # @param [Jimson::BatchClient] batch_client
55
- # @param [Integer] api_client_id
56
- # @param [String] permission
57
- # @return [Syncano::Resources::Base]
58
- def batch_deauthorize(batch_client, api_client_id, permission)
59
- perform_deauthorize(batch_client, api_client_id: api_client_id, permission: permission)
60
- self
61
- end
62
-
63
- private
64
-
65
- # Executes proper subscribe request
66
- # @return [Syncano::Response]
67
- def perform_subscribe
68
- check_if_sync_client!
69
- client.make_request(:subscription, :subscribe_project, { project_id: id })
70
- end
71
-
72
- # Executes proper unsubscribe request
73
- # @return [Syncano::Response]
74
- def perform_unsubscribe
75
- check_if_sync_client!
76
- client.make_request(:subscription, :unsubscribe_project, { project_id: id })
77
- end
78
-
79
- # Executes proper authorize request
80
- # @param [Jimson::BatchClient] batch_client
81
- # @param [Hash] parameters
82
- # @return [Syncano::Response]
83
- def perform_authorize(batch_client, parameters)
84
- self.class.make_request(client, batch_client, :authorize, parameters.merge(self.class.primary_key_name.to_sym => primary_key))
85
- end
86
-
87
- # Executes proper deauthorize request
88
- # @param [Jimson::BatchClient] batch_client
89
- # @param [Hash] parameters
90
- # @return [Syncano::Response]
91
- def perform_deauthorize(batch_client, parameters)
92
- self.class.make_request(client, batch_client, :deauthorize, parameters.merge(self.class.primary_key_name.to_sym => primary_key))
93
- end
94
- end
95
- end
96
- end
@@ -1,11 +0,0 @@
1
- class Syncano
2
- module Resources
3
- # Role resource
4
- class Role < ::Syncano::Resources::Base
5
- private
6
-
7
- self.crud_class_methods = [:all]
8
- self.crud_instance_methods = []
9
- end
10
- end
11
- end
@@ -1,12 +0,0 @@
1
- class Syncano
2
- module Resources
3
- # Subscription resource
4
- class Subscription < ::Syncano::Resources::Base
5
-
6
- private
7
-
8
- self.crud_class_methods = [:all]
9
- self.crud_instance_methods = []
10
- end
11
- end
12
- end
@@ -1,65 +0,0 @@
1
- class Syncano
2
- module Resources
3
- # User resource
4
- class User < ::Syncano::Resources::Base
5
- # Wrapper for api "count" method
6
- # @param [Syncano::Clients::Base] client
7
- # @param [Hash] scope_parameters
8
- # @param [Hash] conditions
9
- # @return [Integer]
10
- def self.count(client, scope_parameters = {}, conditions = {})
11
- response = perform_count(client, scope_parameters, conditions)
12
- response.data if response.status
13
- end
14
-
15
- # Wrapper for api "login" method
16
- # @param [Syncano::Clients::Base] client
17
- # @param [String] username
18
- # @param [String] password
19
- # @return [Integer]
20
- def self.login(client, username, password)
21
- response = perform_login(client, user_name: username, password: password)
22
- response.data
23
- end
24
-
25
- private
26
-
27
- self.scope_parameters = [:project_id, :collection_id]
28
-
29
- # Prepares hash with attributes used in synchronization with Syncano
30
- # @return [Hash]
31
- def self.attributes_to_sync(attributes)
32
- attributes = attributes.dup
33
-
34
- if attributes.keys.map(&:to_sym).include?(:avatar)
35
- if attributes[:avatar].blank?
36
- attributes[:avatar] = ''
37
- elsif attributes[:avatar].is_a?(String)
38
- attributes[:avatar] = Base64.encode64(File.read(attributes[:avatar]))
39
- else
40
- attributes.delete(:image)
41
- end
42
- end
43
-
44
- attributes
45
- end
46
-
47
- # Executes proper count request
48
- # @param [Syncano::Clients::Base] client
49
- # @param [Hash] scope_parameters
50
- # @param [Hash] conditions
51
- # @return [Syncano::Response]
52
- def self.perform_count(client, scope_parameters, conditions)
53
- make_request(client, nil, :count, conditions.merge(scope_parameters))
54
- end
55
-
56
- # Executes proper login request
57
- # @param [Syncano::Clients::Base] client
58
- # @param [Hash] parameters
59
- # @return [Syncano::Response]
60
- def self.perform_login(client, parameters = {})
61
- make_request(client, nil, :login, parameters, :auth_key)
62
- end
63
- end
64
- end
65
- end
@@ -1,22 +0,0 @@
1
- class Syncano
2
- # Represents response from Syncano API
3
- class Response
4
- attr_reader :status, :data, :errors
5
-
6
- # Constructor for Syncano::Response
7
- # @param [Boolean] status
8
- # @param [Hash] data
9
- # @param [Array] errors
10
- def initialize(status = false, data = nil, errors = [])
11
- super()
12
-
13
- self.status = status
14
- self.data = data
15
- self.errors = errors
16
- end
17
-
18
- private
19
-
20
- attr_writer :status, :data, :errors
21
- end
22
- end
@@ -1,133 +0,0 @@
1
- class Syncano
2
- # Represents connection with Sync Server
3
- class SyncConnection < EventMachine::Connection
4
- attr_reader :client
5
-
6
- # Constructor for Syncano::SyncConnection object
7
- def initialize
8
- super
9
-
10
- self.callbacks = ::ActiveSupport::HashWithIndifferentAccess.new
11
- self.callbacks_queue = []
12
-
13
- self.responses = ::ActiveSupport::HashWithIndifferentAccess.new
14
- self.responses_queue = []
15
-
16
- self.client = ::Syncano::Clients::Sync.instance
17
- self.received_data = ''
18
- end
19
-
20
- # Eventmachine callback invoked after completing connection
21
- def connection_completed
22
- start_tls
23
- end
24
-
25
- # Eventmachine callback invoked after completing ssl handshake
26
- def ssl_handshake_completed
27
- auth_data = {
28
- api_key: client.api_key,
29
- instance: client.instance_name,
30
- 'user-agent' => "syncano-ruby-#{Syncano::VERSION}"
31
- }
32
-
33
- auth_data[:auth_key] = client.auth_key if client.auth_key.present?
34
-
35
- client.connection = self
36
-
37
- send_data "#{auth_data.to_json}\n"
38
- end
39
-
40
- # Eventmachine callback invoked after receiving data from socket
41
- # Data are parsed here and processed by callbacks chain
42
- def receive_data(data)
43
- self.received_data += data
44
- process_data if data.end_with?("\n")
45
- end
46
-
47
- # Appends callback method to the end of callbacks chain
48
- # @param [Symbol, String] callback_name
49
- # @param [Block] callback
50
- def append_callback(callback_name, callback)
51
- callbacks[callback_name] = callback
52
- callbacks_queue << callback_name
53
- end
54
-
55
- # Prepends callback method to the beginning of callbacks chain
56
- # @param [Symbol, String] callback_name
57
- # @param [Block] callback
58
- def prepend_callback(callback_name, callback)
59
- callbacks[callback_name] = callback
60
- callbacks_queue.unshift(callback_name)
61
- end
62
-
63
- # Removes callback from callbacks chain
64
- # @param [Symbol, String] callback_name
65
- def remove_callback(callback_name)
66
- callbacks.delete(callback_name)
67
- callbacks_queue.delete(callback_name)
68
- end
69
-
70
- # Gets call response packet from the responses queue
71
- # @param [Integer, String] message_id
72
- # @return [Syncano::Packets::CallResponse]
73
- def get_response(message_id)
74
- responses.delete(message_id.to_s)
75
- end
76
-
77
- protected
78
-
79
- attr_accessor :received_data
80
-
81
- private
82
-
83
- attr_accessor :client, :callbacks, :callbacks_queue, :responses, :responses_queue
84
-
85
- # Processes data received in the receive_data callback
86
- def process_data
87
- begin
88
- data = ::ActiveSupport::HashWithIndifferentAccess.new(JSON.parse(received_data))
89
- packet = ::Syncano::Packets::Base.instantize_packet(data)
90
-
91
- if packet.notification?
92
- notification = ::Syncano::Resources::Notifications::Base.instantize_notification(client, packet)
93
-
94
- callbacks_queue.each do |callback_name|
95
- callbacks[callback_name].call(notification)
96
- end
97
- elsif packet.call_response?
98
- queue_response(packet)
99
- elsif packet.auth?
100
- queue_response(packet)
101
- end
102
-
103
- self.received_data = ''
104
- rescue Exception => e
105
- p 'EXCEPTION!'
106
- p e.inspect
107
- end
108
- end
109
-
110
- # Adds call response packet to the responses queue
111
- # @param [Syncano::Packets::CallResponse] packet
112
- def queue_response(packet)
113
- prune_responses_queue
114
- message_id = packet.message_id.to_s
115
- responses[message_id] = packet
116
- responses_queue << message_id.to_s
117
- end
118
-
119
- # Removes old call response packets from the responses queue
120
- def prune_responses_queue
121
- while !responses_queue.empty?
122
- message_id = responses_queue.first
123
-
124
- if responses[message_id].nil? || Time.now - responses[message_id].timestamp.to_time > 2.minutes
125
- responses_queue.shift
126
- responses.delete(message_id)
127
- else
128
- break
129
- end
130
- end
131
- end
132
- end
133
- end
data/spec/admins_spec.rb DELETED
@@ -1,16 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'Syncano::Resource::Admin' do
4
- it 'should get admins' do
5
- @client.admins.all.each do |admin|
6
- admin.id.should_not be_nil
7
- admin[:email].should_not be_nil
8
- end
9
- end
10
-
11
- it 'should get one admin' do
12
- admins = @client.admins.all
13
-
14
- @client.admins.find(admins.last.id)[:name].should == admins.last[:name]
15
- end
16
- end