Class: CloudflareClient::Value

Inherits:
Namespace show all
Defined in:
lib/cloudflare_client/namespace/value.rb

Constant Summary

Constants inherited from CloudflareClient

API_BASE, POSSIBLE_API_SETTINGS, VALID_BUNDLE_METHODS, VALID_DIRECTIONS, VALID_MATCHES, VERSION

Instance Attribute Summary collapse

Attributes inherited from Namespace

#account_id

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Value

Returns a new instance of Value.



4
5
6
7
8
# File 'lib/cloudflare_client/namespace/value.rb', line 4

def initialize(args)
  @namespace_id = args.delete(:namespace_id)
  id_check(:namespace_id, namespace_id)
  super(**args)
end

Instance Attribute Details

#namespace_idObject (readonly)

Returns the value of attribute namespace_id.



2
3
4
# File 'lib/cloudflare_client/namespace/value.rb', line 2

def namespace_id
  @namespace_id
end

Instance Method Details

#delete(key:) ⇒ Object



23
24
25
# File 'lib/cloudflare_client/namespace/value.rb', line 23

def delete(key:)
  cf_delete(path: "/accounts/#{}/storage/kv/namespaces/#{namespace_id}/values/#{key}")
end

#read(key:) ⇒ Object



19
20
21
# File 'lib/cloudflare_client/namespace/value.rb', line 19

def read(key:)
  cf_get(path: "/accounts/#{}/storage/kv/namespaces/#{namespace_id}/values/#{key}", raw: true)
end

#write(key:, value:, expiration_ttl: nil, metadata: nil) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/cloudflare_client/namespace/value.rb', line 10

def write(key:, value:, expiration_ttl: nil, metadata: nil)
  if expiration_ttl
    raise RuntimeError, 'expiration_ttl must be an integer' unless expiration_ttl.kind_of?(Integer)
  end

  data =  ? { value: value, metadata: } : value
  cf_put(path: "/accounts/#{}/storage/kv/namespaces/#{namespace_id}/values/#{key}", data: data, params: {expiration_ttl: expiration_ttl})
end