Class: Cloudflare::KVNamespace
- Inherits:
-
Object
- Object
- Cloudflare::KVNamespace
- Defined in:
- lib/cloudflare_workers.rb
Instance Method Summary collapse
-
#delete(key) ⇒ Object
Delete a key.
-
#get(key) ⇒ Object
KV#get returns a JS Promise resolving to a String or nil.
-
#initialize(js) ⇒ KVNamespace
constructor
A new instance of KVNamespace.
-
#put(key, value, expiration_ttl: nil) ⇒ Object
Put a value.
Constructor Details
#initialize(js) ⇒ KVNamespace
Returns a new instance of KVNamespace.
654 655 656 |
# File 'lib/cloudflare_workers.rb', line 654 def initialize(js) @js = js end |
Instance Method Details
#delete(key) ⇒ Object
Delete a key. Returns a JS Promise.
681 682 683 684 685 |
# File 'lib/cloudflare_workers.rb', line 681 def delete(key) js_kv = @js err_cls = Cloudflare::KVError `#{js_kv}.delete(#{key}).catch(function(e) { #{Kernel}.$raise(#{err_cls}.$new(e.message || String(e), Opal.hash({binding_type: 'KV', operation: 'delete'}))); })` end |
#get(key) ⇒ Object
KV#get returns a JS Promise resolving to a String or nil.
659 660 661 662 663 |
# File 'lib/cloudflare_workers.rb', line 659 def get(key) js_kv = @js err_cls = Cloudflare::KVError `#{js_kv}.get(#{key}, "text").then(function(v) { return v == null ? nil : v; }).catch(function(e) { #{Kernel}.$raise(#{err_cls}.$new(e.message || String(e), Opal.hash({binding_type: 'KV', operation: 'get'}))); })` end |
#put(key, value, expiration_ttl: nil) ⇒ Object
Put a value. ‘expiration_ttl:` (seconds) maps to the Workers KV `expirationTtl` option so callers can set TTLs without reaching for backticks. Returns a JS Promise.
668 669 670 671 672 673 674 675 676 677 678 |
# File 'lib/cloudflare_workers.rb', line 668 def put(key, value, expiration_ttl: nil) js_kv = @js err_cls = Cloudflare::KVError ttl = expiration_ttl if ttl.nil? `#{js_kv}.put(#{key}, #{value}).catch(function(e) { #{Kernel}.$raise(#{err_cls}.$new(e.message || String(e), Opal.hash({binding_type: 'KV', operation: 'put'}))); })` else ttl_int = ttl.to_i `#{js_kv}.put(#{key}, #{value}, { expirationTtl: #{ttl_int} }).catch(function(e) { #{Kernel}.$raise(#{err_cls}.$new(e.message || String(e), Opal.hash({binding_type: 'KV', operation: 'put'}))); })` end end |