Class: Smplkit::Platform::ContextsClient
- Inherits:
-
Object
- Object
- Smplkit::Platform::ContextsClient
- Defined in:
- lib/smplkit/platform/client.rb
Overview
Sync context registration + read/delete (client.platform.contexts).
Instance Method Summary collapse
- #_save_context(ctx) ⇒ Object private
-
#delete(id_or_type, key = nil) ⇒ void
Delete a single context, identified by composite id or by type and key.
-
#flush ⇒ void
Send any pending observations to the server.
-
#get(id_or_type, key = nil) ⇒ Context
Fetch a single context, identified by composite id or by type and key.
-
#initialize(app_http, buffer) ⇒ ContextsClient
constructor
A new instance of ContextsClient.
-
#list(type, page_number: nil, page_size: nil) ⇒ Array<Context>
List all contexts of a given type.
-
#pending_count ⇒ Integer
Number of observations queued and awaiting flush.
-
#register(items, flush: false) ⇒ void
Buffer one or more contexts for registration.
Constructor Details
#initialize(app_http, buffer) ⇒ ContextsClient
Returns a new instance of ContextsClient.
369 370 371 372 |
# File 'lib/smplkit/platform/client.rb', line 369 def initialize(app_http, buffer) @api = SmplkitGeneratedClient::App::ContextsApi.new(app_http) @buffer = buffer end |
Instance Method Details
#_save_context(ctx) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
465 466 467 468 469 |
# File 'lib/smplkit/platform/client.rb', line 465 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) ⇒ void
This method returns an undefined value.
Delete a single context, identified by composite id or by type and key.
458 459 460 461 462 |
# File 'lib/smplkit/platform/client.rb', line 458 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 |
#flush ⇒ void
This method returns an undefined value.
Send any pending observations to the server.
401 402 403 404 405 406 407 |
# File 'lib/smplkit/platform/client.rb', line 401 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) ⇒ Context
Fetch a single context, identified by composite id or by type and key.
443 444 445 446 447 |
# File 'lib/smplkit/platform/client.rb', line 443 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) ⇒ Array<Context>
List all contexts of a given type.
425 426 427 428 429 430 431 |
# File 'lib/smplkit/platform/client.rb', line 425 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_count ⇒ Integer
Number of observations queued and awaiting flush.
412 413 414 |
# File 'lib/smplkit/platform/client.rb', line 412 def pending_count @buffer.pending_count end |
#register(items, flush: false) ⇒ void
This method returns an undefined value.
Buffer one or more contexts for registration.
Buffered contexts are sent in batches: a background flush kicks in once enough have accumulated, and any remainder is sent on the next explicit flush. Pass flush: true to send everything buffered right away.
386 387 388 389 390 391 392 393 394 395 396 |
# File 'lib/smplkit/platform/client.rb', line 386 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 |