class Syncano::Clients::Sync

Client used for communication with the Sync Server

Attributes

connection[RW]

Public Class Methods

instance(instance_name = nil, api_key = nil, auth_key = nil) click to toggle source

Getter for Singleton instance @param [String] instance_name @param [String] api_key @return [Syncano::Clients::Base]

# 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
new(instance_name, api_key, auth_key = nil) click to toggle source

Constructor for Syncano::Clients::Sync object @param [String] instance_name @param [String] api_key

Calls superclass method Syncano::Clients::Base::new
# 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

Public Instance Methods

append_callback(callback_name, &callback) click to toggle source

Appends callback for processing notifications to the end of callbacks queue @return [Syncano::QueryBuilder]

# File lib/syncano/clients/sync.rb, line 112
def append_callback(callback_name, &callback)
  connection.append_callback(callback_name, callback)
end
connect() click to toggle source

Connects with the Sync api

# 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?() click to toggle source

Checks if client is connected @return [TrueClass, FalseClass]

# File lib/syncano/clients/sync.rb, line 34
def connected?
  !connection.blank?
end
disconnect() click to toggle source

Disconnects with the Sync api

# File lib/syncano/clients/sync.rb, line 78
def disconnect
  EM.stop if EM::reactor_running?
  self.connection = nil
end
login(username, password) click to toggle source

Gets auth_key based on username and password @return [TrueClass, FalseClass]

# 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) click to toggle source

Performs request to Syncano api This should be overwritten in inherited classes @param [String] resource_name @param [String] method_name @param [Hash] params additional params sent in the request @param [String] response_key for cases when response from api is incompatible with the convention @return [Syncano::Response]

# 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
notifications() click to toggle source

Returns query builder for Syncano::Resources::Notifications::Base objects @return [Syncano::QueryBuilder]

# File lib/syncano/clients/sync.rb, line 106
def notifications
  ::Syncano::QueryBuilder.new(self, ::Syncano::Resources::Notifications::Base)
end
prepend_callback(callback_name, &callback) click to toggle source

Prepends callback for processing notifications to the beginning of callbacks queue @return [Syncano::QueryBuilder]

# File lib/syncano/clients/sync.rb, line 118
def prepend_callback(callback_name, &callback)
  connection.prepend_callback(callback_name, callback)
end
reconnect() click to toggle source

Reconnects with the Sync api

# File lib/syncano/clients/sync.rb, line 84
def reconnect
  disconnect
  connect
end
remove_callback(callback_name) click to toggle source

Removes callback from the callbacks queue @return [Syncano::QueryBuilder]

# File lib/syncano/clients/sync.rb, line 124
def remove_callback(callback_name)
  connection.remove_callback(callback_name)
end
subscriptions() click to toggle source

Returns query builder for Syncano::Resources::Subscription objects @return [Syncano::QueryBuilder]

# File lib/syncano/clients/sync.rb, line 100
def subscriptions
  ::Syncano::QueryBuilder.new(self, ::Syncano::Resources::Subscription)
end