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.



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.

Parameters:

  • id_or_type (String)

    Either the composite context id “type:key” (when key is omitted) or just the context type (when key is supplied).

  • key (String, nil) (defaults to: nil)

    The context key. Provide it to use the two-argument form; omit it when id_or_type already carries the composite id.



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

#flushvoid

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.

Parameters:

  • id_or_type (String)

    Either the composite context id “type:key” (when key is omitted) or just the context type (when key is supplied).

  • key (String, nil) (defaults to: nil)

    The context key. Provide it to use the two-argument form; omit it when id_or_type already carries the composite id.

Returns:

  • (Context)

    The matching context.

Raises:



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.

Parameters:

  • type (String)

    Context type to list (for example “user”).

  • page_number (Integer, nil) (defaults to: nil)

    1-based page to fetch. Defaults to the first page.

  • page_size (Integer, nil) (defaults to: nil)

    Maximum number of contexts per page. Defaults to the server’s page size.

Returns:

  • (Array<Context>)

    The contexts of the given type on the requested page.



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_countInteger

Number of observations queued and awaiting flush.

Returns:

  • (Integer)

    The count of observations pending 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.

Parameters:

  • items (Context, Array<Context>)

    A single context or a list of contexts to register.

  • flush (Boolean) (defaults to: false)

    When true, send all buffered contexts immediately rather than waiting for the batch threshold. Defaults to false.



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