Class: Smplkit::ManagementClient::ContextsNamespace

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

Overview


Sub-namespaces


Instance Method Summary collapse

Constructor Details

#initialize(api_client) ⇒ ContextsNamespace

Returns a new instance of ContextsNamespace.



184
185
186
187
# File 'lib/smplkit/management/client.rb', line 184

def initialize(api_client)
  @api = SmplkitGeneratedClient::App::ContextsApi.new(api_client)
  @buffer = Management::ContextRegistrationBuffer.new
end

Instance Method Details

#_save_context(ctx) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/smplkit/management/client.rb', line 231

def _save_context(ctx)
  body = SmplkitGeneratedClient::App::ContextResponse.new(
    data: SmplkitGeneratedClient::App::ContextResource.new(
      type: "context",
      id: ctx.id,
      attributes: SmplkitGeneratedClient::App::Context.new(
        name: ctx.name, context_type: ctx.type, attributes: ctx.attributes
      )
    )
  )
  response = ErrorMapping.call { @api.update_context(ctx.id, body) }
  context_from_resource(ResourceShim.from_model(response.data)).tap { |c| c._bind_client(self) }
end

#delete(id_or_type, key = nil) ⇒ Object



225
226
227
228
229
# File 'lib/smplkit/management/client.rb', line 225

def delete(id_or_type, key = nil)
  type, ckey = split_id(id_or_type, key)
  ErrorMapping.call { @api.delete_context("#{type}:#{ckey}") }
  true
end

#flushObject



196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/smplkit/management/client.rb', line 196

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

  items = batch.map do |entry|
    SmplkitGeneratedClient::App::ContextBulkItem.new(
      type: entry["type"], key: entry["key"], attributes: entry["attributes"] || {}
    )
  end
  body = SmplkitGeneratedClient::App::ContextBulkRegister.new(contexts: items)
  ErrorMapping.call { @api.bulk_register_contexts(body) }
rescue StandardError => e
  Smplkit.debug("registration", "context flush failed: #{e.class}: #{e.message}")
end

#get(id_or_type, key = nil) ⇒ Object



219
220
221
222
223
# File 'lib/smplkit/management/client.rb', line 219

def get(id_or_type, key = nil)
  type, ckey = split_id(id_or_type, key)
  response = ErrorMapping.call { @api.get_context("#{type}:#{ckey}") }
  context_from_resource(ResourceShim.from_model(response.data))
end

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



211
212
213
214
215
216
217
# File 'lib/smplkit/management/client.rb', line 211

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

#register(contexts) ⇒ Object



189
190
191
192
193
194
# File 'lib/smplkit/management/client.rb', line 189

def register(contexts)
  return if contexts.nil? || contexts.empty?

  @buffer.observe(contexts)
  flush if @buffer.pending_count >= Management::CONTEXT_BATCH_FLUSH_SIZE
end