Sha256: 067bb30f801678944c4c039189c6e5d2be5656dce3ed5c9fa3a3ee7f9daa953c

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

# This implements the Worker KV Store API
# https://api.cloudflare.com/#workers-kv-namespace-properties

require_relative '../paginate'
require_relative '../representation'
require_relative 'rest_wrapper'

module Cloudflare
	module KV
		class Key < Representation
			def name
				value[:name]
			end
		end

		class Keys < Representation
			include Paginate

			def representation
				Key
			end
		end

		class Namespace < Representation
			def delete_value(name)
				value_representation(name).delete.success?
			end

			def id
				value[:id]
			end

			def keys
				self.with(Keys, path: 'keys')
			end

			def read_value(name)
				value_representation(name).value
			end

			def rename(new_title)
				put(title: new_title)
				value[:title] = new_title
			end

			def title
				value[:title]
			end

			def write_value(name, value)
				value_representation(name).put(value).success?
			end

			private

			def value_representation(name)
				@representation_class ||= Representation[RESTWrapper]
				self.with(@representation_class, path: "values/#{name}")
			end
		end

		class Namespaces < Representation
			include Paginate

			def representation
				Namespace
			end

			def create(title)
				represent_message(post(title: title))
			end

			def find_by_title(title)
				each.find {|namespace| namespace.title == title }
			end
		end
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cloudflare-4.3.0 lib/cloudflare/kv/namespaces.rb