Class: Smplkit::ManagementClient::ContextsNamespace

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

Instance Method Summary collapse

Constructor Details

#initialize(http) ⇒ ContextsNamespace

Returns a new instance of ContextsNamespace.



129
130
131
132
# File 'lib/smplkit/management/client.rb', line 129

def initialize(http)
  @http = http
  @buffer = Management::ContextRegistrationBuffer.new
end

Instance Method Details

#_save_context(ctx) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/smplkit/management/client.rb', line 167

def _save_context(ctx)
  body = {
    "data" => {
      "type" => "context",
      "id" => ctx.id,
      "attributes" => { "type" => ctx.type, "key" => ctx.key, "attributes" => ctx.attributes }.compact
    }
  }
  resp = http_put("/api/v1/contexts/#{ctx.id}", body)
  context_from_resource(resp["data"]).tap { |c| c._bind_client(self) }
end

#delete(id_or_type, key = nil) ⇒ Object



162
163
164
165
# File 'lib/smplkit/management/client.rb', line 162

def delete(id_or_type, key = nil)
  type, ckey = split_id(id_or_type, key)
  http_delete("/api/v1/contexts/#{type}:#{ckey}")
end

#flushObject



141
142
143
144
145
146
147
148
149
# File 'lib/smplkit/management/client.rb', line 141

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

  body = { "data" => { "type" => "context_bulk_register", "attributes" => { "contexts" => batch } } }
  http_post("/api/v1/contexts/bulk", body)
rescue StandardError => e
  Smplkit.debug("registration", "context flush failed: #{e.class}: #{e.message}")
end

#get(id_or_type, key = nil) ⇒ Object



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

def get(id_or_type, key = nil)
  type, ckey = split_id(id_or_type, key)
  resource = http_get("/api/v1/contexts/#{type}:#{ckey}")
  context_from_resource(resource["data"])
end

#listObject



151
152
153
154
# File 'lib/smplkit/management/client.rb', line 151

def list
  list_resp = http_list("/api/v1/contexts")
  list_resp.map { |r| context_from_resource(r) }
end

#register(contexts) ⇒ Object



134
135
136
137
138
139
# File 'lib/smplkit/management/client.rb', line 134

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

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