Class: Smplkit::Platform::ContextsClient

Inherits:
Object
  • Object
show all
Defined in:
lib/smplkit/platform/client.rb

Overview

Sync context registration + read/delete (client.platform.contexts).

Instance Method Summary collapse

Constructor Details

#initialize(app_http, buffer) ⇒ ContextsClient

Returns a new instance of ContextsClient.



287
288
289
290
# File 'lib/smplkit/platform/client.rb', line 287

def initialize(app_http, buffer)
  @api = SmplkitGeneratedClient::App::ContextsApi.new(app_http)
  @buffer = buffer
end

Instance Method Details

#_save_context(ctx) ⇒ Object



340
341
342
343
344
# File 'lib/smplkit/platform/client.rb', line 340

def _save_context(ctx)
  body = ctx_to_resource(ctx)
  response = ApiSupport::ErrorMapping.call { @api.update_context(ctx.id, body) }
  context_from_resource(ApiSupport::ResourceShim.from_model(response.data))
end

#delete(id_or_type, key = nil) ⇒ Object



334
335
336
337
338
# File 'lib/smplkit/platform/client.rb', line 334

def delete(id_or_type, key = nil)
  ctx_type, ctx_key = Platform.split_context_id(id_or_type, key)
  ApiSupport::ErrorMapping.call { @api.delete_context("#{ctx_type}:#{ctx_key}") }
  nil
end

#flushObject

Send any pending observations to the server.



306
307
308
309
310
311
312
# File 'lib/smplkit/platform/client.rb', line 306

def flush
  batch = @buffer.drain
  return if batch.empty?

  body = build_bulk_register_body(batch)
  ApiSupport::ErrorMapping.call { @api.bulk_register_contexts(body) }
end

#get(id_or_type, key = nil) ⇒ Object



328
329
330
331
332
# File 'lib/smplkit/platform/client.rb', line 328

def get(id_or_type, key = nil)
  ctx_type, ctx_key = Platform.split_context_id(id_or_type, key)
  response = ApiSupport::ErrorMapping.call { @api.get_context("#{ctx_type}:#{ctx_key}") }
  context_from_resource(ApiSupport::ResourceShim.from_model(response.data))
end

#list(type, page_number: nil, page_size: nil) ⇒ Object

List all contexts of a given type.



320
321
322
323
324
325
326
# File 'lib/smplkit/platform/client.rb', line 320

def list(type, page_number: nil, page_size: nil)
  opts = { filter_context_type: type }
  opts[:page_number] = page_number unless page_number.nil?
  opts[:page_size] = page_size unless page_size.nil?
  response = ApiSupport::ErrorMapping.call { @api.list_contexts(opts) }
  (response.data || []).map { |r| context_from_resource(ApiSupport::ResourceShim.from_model(r)) }
end

#pending_countObject

Number of observations queued and awaiting flush.



315
316
317
# File 'lib/smplkit/platform/client.rb', line 315

def pending_count
  @buffer.pending_count
end

#register(items, flush: false) ⇒ Object

Buffer contexts for registration; optionally flush immediately.



293
294
295
296
297
298
299
300
301
302
303
# File 'lib/smplkit/platform/client.rb', line 293

def register(items, flush: false)
  batch = items.is_a?(Array) ? items : [items]
  @buffer.observe(batch)
  if flush
    self.flush
    return
  end
  return unless @buffer.pending_count >= CONTEXT_BATCH_FLUSH_SIZE

  Thread.new { threshold_flush }
end