Class: Smplkit::ManagementClient::ContextsNamespace
- Inherits:
-
Object
- Object
- Smplkit::ManagementClient::ContextsNamespace
- Includes:
- HttpHelpers
- Defined in:
- lib/smplkit/management/client.rb
Instance Method Summary collapse
- #_save_context(ctx) ⇒ Object
- #delete(id_or_type, key = nil) ⇒ Object
- #flush ⇒ Object
- #get(id_or_type, key = nil) ⇒ Object
-
#initialize(http) ⇒ ContextsNamespace
constructor
A new instance of ContextsNamespace.
- #list ⇒ Object
- #register(contexts) ⇒ Object
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/contexts/v1/#{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/contexts/v1/#{type}:#{ckey}") end |
#flush ⇒ Object
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/contexts/v1/bulk", body) rescue StandardError => e Smplkit.debug("registration", "context flush failed: #{e.class}: #{e.}") 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/contexts/v1/#{type}:#{ckey}") context_from_resource(resource["data"]) end |
#list ⇒ Object
151 152 153 154 |
# File 'lib/smplkit/management/client.rb', line 151 def list list_resp = http_list("/api/contexts/v1") 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 |