Class: BaseCRM::SyncService
- Inherits:
-
Object
- Object
- BaseCRM::SyncService
- Defined in:
- lib/basecrm/services/sync_service.rb
Instance Method Summary collapse
-
#ack(device_uuid, ack_keys) ⇒ Boolean
Acknowledge received data.
-
#fetch(device_uuid, session_id, queue = 'main') ⇒ Array<Array<Meta, Model>>
Get data from queue.
-
#initialize(client) ⇒ SyncService
constructor
A new instance of SyncService.
-
#start(device_uuid) ⇒ SyncSession
Start synchronization flow.
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.
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.
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 [(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.
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 |