Class: Syncano::Clients::Sync

Inherits:
Base
  • Object
show all
Includes:
Singleton
Defined in:
lib/syncano/clients/sync.rb

Overview

Client used for communication with the Sync Server

Instance Attribute Summary collapse

Attributes inherited from Base

#api_key, #auth_key, #instance_name

Class Method Summary collapse

Instance Method Summary collapse

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

Parameters:

  • instance_name (String)
  • api_key (String)


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

#connectionObject

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

Parameters:

  • instance_name (String) (defaults to: nil)
  • api_key (String) (defaults to: nil)

Returns:



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

#connectObject

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.message
        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

Returns:

  • (TrueClass, FalseClass)


34
35
36
# File 'lib/syncano/clients/sync.rb', line 34

def connected?
  !connection.blank?
end

#disconnectObject

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

Returns:

  • (TrueClass, FalseClass)


91
92
93
94
95
96
# File 'lib/syncano/clients/sync.rb', line 91

def (username, password)
  logout
  rest_client = ::Syncano.client(api_key: api_key)
  self.auth_key = rest_client.users.(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

Parameters:

  • resource_name (String)
  • method_name (String)
  • params (Hash) (defaults to: {})

    additional params sent in the request

  • response_key (String) (defaults to: nil)

    for cases when response from api is incompatible with the convention

Returns:

Raises:



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.message_id)
    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

#notificationsSyncano::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

#reconnectObject

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

#subscriptionsSyncano::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