Class: BaseCRM::SyncService

Inherits:
Object
  • Object
show all
Defined in:
lib/basecrm/services/sync_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ SyncService

Returns a new instance of SyncService.



4
5
6
# File 'lib/basecrm/services/sync_service.rb', line 4

def initialize(client)
  @client = client
end

Instance Method Details

#ack(device_uuid, ack_keys) ⇒ Boolean

Acknowledge received data

post ‘/sync/ack’

Send acknowledgement keys to let know the Sync service which data you have. As you fetch new data, you need to send acknowledgement keys.

Parameters:

  • device_uuid (String)

    Device’s UUID for which to perform synchronization.

  • ack_keys (Array<String>)

    The list of acknowledgement keys.

Returns:

  • (Boolean)

    Status of the operation.

Raises:

  • (ArgumentError)


62
63
64
65
66
67
68
69
70
71
72
# File 'lib/basecrm/services/sync_service.rb', line 62

def ack(device_uuid, ack_keys)
  validate_device!(device_uuid)
  raise ArgumentError, "ack_keys must not be nil and an array" unless ack_keys && ack_keys.is_a?(Array)
  return true if ack_keys.empty?

  payload = {
    :ack_keys => ack_keys.map(&:to_s)
  }
  status, _, _ = @client.post('/sync/ack', payload, build_headers(device_uuid))
  status == 202
end

#fetch(device_uuid, session_id, queue = 'main') ⇒ Array<Array<Meta, Model>>

Get data from queue

get ‘/sync/session_id/queues/main’

Fetch fresh data from the main queue. Using session identifier you call continously the ‘#fetch` method to drain the main queue.

Parameters:

  • device_uuid (String)

    Device’s UUID for which to perform synchronization

  • session_id (String)

    Unique identifier of a synchronization session.

  • queue (String|Symbol) (defaults to: 'main')

    Queue name.

Returns:

  • (Array<Array<Meta, Model>>)

    The list of sync’s metadata associated with data and data, or nil if nothing more to synchronize.

Raises:

  • (ArgumentError)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/basecrm/services/sync_service.rb', line 37

def fetch(device_uuid, session_id, queue='main')
  validate_device!(device_uuid)
  raise ArgumentError, "session_id must not be nil nor empty" unless session_id && !session_id.strip.empty?
  raise ArgumentError, "queue name must not be nil nor empty" unless queue && !queue.strip.empty?

  status, _, root = @client.get("/sync/#{session_id}/queues/#{queue}", {}, build_headers(device_uuid))
  return nil if status == 204

  root[:items].map do |item|
    klass = classify_type(item[:meta][:type])
    next unless klass
    [build_meta(item[:meta]), klass.new(item[:data])]
  end
end

#start(device_uuid) ⇒ SyncSession

Start synchronization flow

post ‘/sync/start’

Starts a new synchronization session. This is the first endpoint to call, in order to start a new synchronization session.

Parameters:

  • device_uuid (String)

    Device’s UUID for which to perform synchronization.

Returns:

  • (SyncSession)

    The resulting object is the synchronization session object or nil if there is nothing to synchronize.



17
18
19
20
21
22
23
24
# File 'lib/basecrm/services/sync_service.rb', line 17

def start(device_uuid)
  validate_device!(device_uuid)

  status, _, root = @client.post("/sync/start", {}, build_headers(device_uuid))
  return nil if status == 204

  build_session(root)
end