Class: Syncano::Clients::Sync
- Includes:
- Singleton
- Defined in:
- lib/syncano/clients/sync.rb
Overview
Client used for communication with the Sync Server
Instance Attribute Summary collapse
-
#connection ⇒ Object
Returns the value of attribute connection.
Attributes inherited from Base
#api_key, #auth_key, #instance_name
Class Method Summary collapse
-
.instance(instance_name = nil, api_key = nil, auth_key = nil) ⇒ Syncano::Clients::Base
Getter for Singleton instance.
Instance Method Summary collapse
-
#append_callback(callback_name, &callback) ⇒ Syncano::QueryBuilder
Appends callback for processing notifications to the end of callbacks queue.
-
#connect ⇒ Object
Connects with the Sync api.
-
#connected? ⇒ TrueClass, FalseClass
Checks if client is connected.
-
#disconnect ⇒ Object
Disconnects with the Sync api.
-
#initialize(instance_name, api_key, auth_key = nil) ⇒ Sync
constructor
Constructor for Syncano::Clients::Sync object.
-
#login(username, password) ⇒ TrueClass, FalseClass
Gets auth_key based on username and password.
-
#make_request(resource_name, method_name, params = {}, response_key = nil) ⇒ Syncano::Response
Performs request to Syncano api This should be overwritten in inherited classes.
-
#notifications ⇒ Syncano::QueryBuilder
Returns query builder for Syncano::Resources::Notifications::Base objects.
-
#prepend_callback(callback_name, &callback) ⇒ Syncano::QueryBuilder
Prepends callback for processing notifications to the beginning of callbacks queue.
-
#reconnect ⇒ Object
Reconnects with the Sync api.
-
#remove_callback(callback_name) ⇒ Syncano::QueryBuilder
Removes callback from the callbacks queue.
-
#subscriptions ⇒ Syncano::QueryBuilder
Returns query builder for Syncano::Resources::Subscription objects.
Methods inherited from Base
#admins, #api_keys, #collections, #data_objects, #folders, #logout, #make_batch_request, #projects, #roles, #users
Constructor Details
#initialize(instance_name, api_key, auth_key = nil) ⇒ Sync
Constructor for Syncano::Clients::Sync object
12 13 14 15 16 |
# File 'lib/syncano/clients/sync.rb', line 12 def initialize(instance_name, api_key, auth_key = nil) super(instance_name, api_key, auth_key) self.connection = nil self.auth_key = auth_key if auth_key.present? end |
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
7 8 9 |
# File 'lib/syncano/clients/sync.rb', line 7 def connection @connection end |
Class Method Details
.instance(instance_name = nil, api_key = nil, auth_key = nil) ⇒ Syncano::Clients::Base
Getter for Singleton instance
22 23 24 25 26 27 28 29 30 |
# File 'lib/syncano/clients/sync.rb', line 22 def self.instance(instance_name = nil, api_key = nil, auth_key = nil) unless @singleton__instance__ @singleton__mutex__.synchronize do return @singleton__instance__ if @singleton__instance__ @singleton__instance__ = new(instance_name, api_key, auth_key) end end @singleton__instance__ end |
Instance Method Details
#append_callback(callback_name, &callback) ⇒ Syncano::QueryBuilder
Appends callback for processing notifications to the end of callbacks queue
112 113 114 |
# File 'lib/syncano/clients/sync.rb', line 112 def append_callback(callback_name, &callback) connection.append_callback(callback_name, callback) end |
#connect ⇒ Object
Connects with the Sync api
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/syncano/clients/sync.rb', line 39 def connect unless connected? hostname = 'api.syncano.com' port = 8200 sleep(3) Thread.new do begin EM.run do EM.connect(hostname, port, Syncano::SyncConnection) end rescue Exception => e p e. p e.backtrace end end timeout = 30 while connection.blank? && timeout > 0 timeout -= 1 sleep 1 end raise ::Syncano::ConnectionError.new('Connection timeout') unless timeout > 0 timeout = 300 while (response = connection.get_response('auth')).blank? && timeout > 0 timeout -= 1 sleep 1.0/10.0 end raise ::Syncano::ConnectionError.new(response.error) if response.status == 'NOK' end end |
#connected? ⇒ TrueClass, FalseClass
Checks if client is connected
34 35 36 |
# File 'lib/syncano/clients/sync.rb', line 34 def connected? !connection.blank? end |
#disconnect ⇒ Object
Disconnects with the Sync api
78 79 80 81 |
# File 'lib/syncano/clients/sync.rb', line 78 def disconnect EM.stop if EM::reactor_running? self.connection = nil end |
#login(username, password) ⇒ TrueClass, FalseClass
Gets auth_key based on username and password
91 92 93 94 95 96 |
# File 'lib/syncano/clients/sync.rb', line 91 def login(username, password) logout rest_client = ::Syncano.client(api_key: api_key) self.auth_key = rest_client.users.login(username, password) !self.auth_key.nil? end |
#make_request(resource_name, method_name, params = {}, response_key = nil) ⇒ Syncano::Response
Performs request to Syncano api This should be overwritten in inherited classes
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/syncano/clients/sync.rb', line 135 def make_request(resource_name, method_name, params = {}, response_key = nil) raise(::Syncano::ConnectionError.new('Not connected to the Sync Server!')) unless connected? response_key ||= resource_name packet = ::Syncano::Packets::Call.new(resource_name: resource_name, method_name: method_name, data: params) connection.send_data("#{packet.to_json}\n") response_packet = nil timer = 600 while timer > 0 response_packet = connection.get_response(packet.) if response_packet.nil? timer -= 1 sleep 1.0 / 10.0 else break end end raise(::Syncano::ApiError.new('Request timeout error!')) if timer == 0 response = self.class.parse_response(response_key, response_packet.to_response) response.errors.present? ? raise(::Syncano::ApiError.new(response.errors)) : response end |
#notifications ⇒ Syncano::QueryBuilder
Returns query builder for Syncano::Resources::Notifications::Base objects
106 107 108 |
# File 'lib/syncano/clients/sync.rb', line 106 def notifications ::Syncano::QueryBuilder.new(self, ::Syncano::Resources::Notifications::Base) end |
#prepend_callback(callback_name, &callback) ⇒ Syncano::QueryBuilder
Prepends callback for processing notifications to the beginning of callbacks queue
118 119 120 |
# File 'lib/syncano/clients/sync.rb', line 118 def prepend_callback(callback_name, &callback) connection.prepend_callback(callback_name, callback) end |
#reconnect ⇒ Object
Reconnects with the Sync api
84 85 86 87 |
# File 'lib/syncano/clients/sync.rb', line 84 def reconnect disconnect connect end |
#remove_callback(callback_name) ⇒ Syncano::QueryBuilder
Removes callback from the callbacks queue
124 125 126 |
# File 'lib/syncano/clients/sync.rb', line 124 def remove_callback(callback_name) connection.remove_callback(callback_name) end |
#subscriptions ⇒ Syncano::QueryBuilder
Returns query builder for Syncano::Resources::Subscription objects
100 101 102 |
# File 'lib/syncano/clients/sync.rb', line 100 def subscriptions ::Syncano::QueryBuilder.new(self, ::Syncano::Resources::Subscription) end |