cloudflare_storage 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +61 -0
- data/Rakefile +8 -0
- data/lib/cloudflare_storage.rb +43 -0
- data/lib/workers_kv/client.rb +37 -0
- data/lib/workers_kv/keys.rb +16 -0
- data/lib/workers_kv/values.rb +21 -0
- data/sig/cloudflare_storage.rbs +4 -0
- metadata +57 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: cb0947aadcd4d5569d1b7dfc86ac2b10d30510d26532150cff86fe1a82edce00
|
4
|
+
data.tar.gz: 9ed75b05b6492d1a7519d877fec5760adc0727917363765604b1c057d4779d26
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9903d4d58316ac04a30099b49e08d224fd9d7a4282eb897880300d5cae37712c0ce8c2cac91a5ac5a6344a3f1961e7276ef7389e3b0639e45d2bf218b5cfcef1
|
7
|
+
data.tar.gz: 5ad630b82c75ba55f3b61329acff25a853bcab4c2afbe736a9ef7f0cfe47097f9a3c30f0957451fbab9cadc8b593cc215653eac33696a2b0934707baa2c207ff
|
data/.rspec
ADDED
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Lee Gillentine
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# CloudflareStorage
|
2
|
+
|
3
|
+
CloudflareStorage is a simple gem for interacting with the Cloudflare Workers KV Storage from a Rails application.
|
4
|
+
|
5
|
+
It was designed to let you get started with minimal configuration.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
````
|
14
|
+
gem 'cloudflare_storage'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then run:
|
18
|
+
|
19
|
+
```bash
|
20
|
+
bundle
|
21
|
+
```
|
22
|
+
|
23
|
+
Or install it yourself:
|
24
|
+
|
25
|
+
```bash
|
26
|
+
gem install cloudflare_storage
|
27
|
+
```
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
Configure credentials with an initializer. For example:
|
32
|
+
```ruby
|
33
|
+
# config/initializers/cloudflare_storage.rb
|
34
|
+
CloudflareStorage.configure do |config|
|
35
|
+
config.account_id = Rails.application.credentials.cloudflare.account_id
|
36
|
+
config.namespace_id = Rails.application.credentials.cloudflare.namespace_id
|
37
|
+
config.token = Rails.application.credentials.cloudflare.token
|
38
|
+
end
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
## Development
|
43
|
+
|
44
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
45
|
+
|
46
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
47
|
+
|
48
|
+
## Contributing
|
49
|
+
|
50
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/geetotes/cloudflare_storage.
|
51
|
+
|
52
|
+
## License
|
53
|
+
|
54
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
55
|
+
|
56
|
+
## Alternatives
|
57
|
+
|
58
|
+
There are other gems out there that encompass the whole of the Cloudflare API. You should look to those if you're planning on using more than just Workers KV storage in your project.
|
59
|
+
|
60
|
+
* [Cloudflare](https://github.com/socketry/cloudflare)
|
61
|
+
* [Rubyflare](https://github.com/trev/rubyflare)
|
data/Rakefile
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require_relative "workers_kv/client"
|
3
|
+
require_relative "workers_kv/values"
|
4
|
+
require_relative "workers_kv/keys"
|
5
|
+
|
6
|
+
|
7
|
+
class CloudflareStorage
|
8
|
+
class << self
|
9
|
+
attr_accessor :account_id, :namespace_id, :token
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.configure(&block)
|
13
|
+
yield self
|
14
|
+
end
|
15
|
+
|
16
|
+
def conn
|
17
|
+
@connection ||= WorkersKV::Client.new(
|
18
|
+
CloudflareStorage.account_id,
|
19
|
+
CloudflareStorage.namespace_id,
|
20
|
+
CloudflareStorage.token
|
21
|
+
)
|
22
|
+
end
|
23
|
+
|
24
|
+
def list(prefix = nil)
|
25
|
+
resp = conn.keys.list(prefix)
|
26
|
+
resp.body.dig("result").pluck("name")
|
27
|
+
end
|
28
|
+
|
29
|
+
def read(key)
|
30
|
+
resp = conn.values.read(key)
|
31
|
+
resp.body
|
32
|
+
end
|
33
|
+
|
34
|
+
def write(key, value)
|
35
|
+
resp = conn.values.write(key, value)
|
36
|
+
resp.body
|
37
|
+
end
|
38
|
+
|
39
|
+
def delete(key)
|
40
|
+
resp = conn.values.delete(key)
|
41
|
+
resp.body
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module WorkersKV
|
2
|
+
class Client
|
3
|
+
def initialize(account_id, namespace_id, token)
|
4
|
+
@conn = Faraday.new(url: "https://api.cloudflare.com/client/v4/accounts/#{account_id}/storage/kv/namespaces/#{namespace_id}/" ) do |builder|
|
5
|
+
builder.request :authorization, "Bearer", token
|
6
|
+
builder.request :json
|
7
|
+
builder.response :json
|
8
|
+
builder.response :raise_error
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def values
|
13
|
+
@values ||= Values.new(self)
|
14
|
+
end
|
15
|
+
|
16
|
+
def keys
|
17
|
+
@keys ||= Keys.new(self)
|
18
|
+
end
|
19
|
+
|
20
|
+
protected
|
21
|
+
def get(path, params = nil)
|
22
|
+
@conn.get path do |req|
|
23
|
+
req.params = params if params.present?
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def put(path, body)
|
28
|
+
@conn.put path do |req|
|
29
|
+
req.body = body
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def conn_delete(path)
|
34
|
+
@conn.delete path
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module WorkersKV
|
2
|
+
class Values < Client
|
3
|
+
|
4
|
+
def initialize(client)
|
5
|
+
@client = client
|
6
|
+
end
|
7
|
+
|
8
|
+
# Manage Values
|
9
|
+
def write(key, value)
|
10
|
+
@client.put("values/#{key}", value)
|
11
|
+
end
|
12
|
+
|
13
|
+
def read(key)
|
14
|
+
@client.get("values/#{key}")
|
15
|
+
end
|
16
|
+
|
17
|
+
def delete(key)
|
18
|
+
@client.conn_delete("values/#{key}")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cloudflare_storage
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lee Gillentine
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-02-04 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Library for interacting with Cloudflare Workers KV Storage and configuring
|
14
|
+
connection from a Rails application.
|
15
|
+
email:
|
16
|
+
- lee@fishburd.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- ".rspec"
|
22
|
+
- CHANGELOG.md
|
23
|
+
- LICENSE.txt
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/cloudflare_storage.rb
|
27
|
+
- lib/workers_kv/client.rb
|
28
|
+
- lib/workers_kv/keys.rb
|
29
|
+
- lib/workers_kv/values.rb
|
30
|
+
- sig/cloudflare_storage.rbs
|
31
|
+
homepage: https://github.com/geetotes/cloudflare_storage
|
32
|
+
licenses:
|
33
|
+
- MIT
|
34
|
+
metadata:
|
35
|
+
homepage_uri: https://github.com/geetotes/cloudflare_storage
|
36
|
+
source_code_uri: https://github.com/geetotes/cloudflare_storage
|
37
|
+
changelog_uri: https://github.com/geetotes/cloudflare_storage/blob/main/CHANGELOG.md
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 3.0.0
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubygems_version: 3.5.16
|
54
|
+
signing_key:
|
55
|
+
specification_version: 4
|
56
|
+
summary: Library for interacting with CLoudflare Workers KV
|
57
|
+
test_files: []
|