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.



149
150
151
152
# File 'lib/smplkit/management/client.rb', line 149

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

Instance Method Details

#_save_context(ctx) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/smplkit/management/client.rb', line 193

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



187
188
189
190
191
# File 'lib/smplkit/management/client.rb', line 187

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



161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/smplkit/management/client.rb', line 161

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



181
182
183
184
185
# File 'lib/smplkit/management/client.rb', line 181

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

#listObject



176
177
178
179
# File 'lib/smplkit/management/client.rb', line 176

def list
  response = ErrorMapping.call { @api.list_contexts }
  (response.data || []).map { |r| context_from_resource(ResourceShim.from_model(r)) }
end

#register(contexts) ⇒ Object



154
155
156
157
158
159
# File 'lib/smplkit/management/client.rb', line 154

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

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