Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/cloudflare/accounts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
# Released under the MIT License.
# Copyright, 2018-2024, by Samuel Williams.
# Copyright, 2019, by Rob Widmer.
# Copyright, 2025, by Ivan Vergés.

require_relative "representation"
require_relative "paginate"
require_relative "kv/namespaces"
require_relative "r2/buckets"

module Cloudflare
class Account < Representation
Expand All @@ -17,6 +19,10 @@ def id
def kv_namespaces
self.with(KV::Namespaces, path: "storage/kv/namespaces")
end

def r2_buckets
self.with(R2::Buckets, path: "r2/buckets")
end
end

class Accounts < Representation
Expand Down
64 changes: 64 additions & 0 deletions lib/cloudflare/r2/buckets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2025, by Ivan Vergés.

require_relative "../paginate"
require_relative "../representation"
require_relative "domains"
require_relative "cors"

module Cloudflare
module R2
class Bucket < Representation
include Async::REST::Representation::Mutable

def name
result[:name]
end

def domains
self.with(Domains, path: "#{name}/domains/custom")
end

def cors
self.with(Cors, path: "#{name}/cors")
end

def create_cors(**options)
self.class.put(@resource.with(path: "#{name}/cors"), options) do |resource, response|
if response.success?
cors
else
raise RequestError.new(resource, response.read)
end
end
end
end

class Buckets < Representation
include Paginate

def representation
Bucket
end

def result
value[:result][:buckets]
end

def create(name, **options)
payload = {name: name, **options}
self.class.post(@resource, payload) do |resource, response|
value = response.read

Bucket.new(resource, value: value, metadata: response.headers)
end
end

def find_by_name(name)
each.find {|bucket| bucket.name == name }
end
end
end
end
17 changes: 17 additions & 0 deletions lib/cloudflare/r2/cors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2025, by Ivan Vergés.

require_relative "../paginate"
require_relative "../representation"

module Cloudflare
module R2
class Cors < Representation
def rules
result[:rules]
end
end
end
end
43 changes: 43 additions & 0 deletions lib/cloudflare/r2/domains.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2025, by Ivan Vergés.

require_relative "../paginate"
require_relative "../representation"

module Cloudflare
module R2
class Domain < Representation
def name
result[:domain]
end
end

class Domains < Representation
include Paginate

def representation
R2::Domain
end

def result
value[:result][:domains]
end

def attach(domain, **options)
payload = {domain:, **options}

self.class.post(@resource, payload) do |resource, response|
value = response.read

Domain.new(resource, value: value, metadata: response.headers)
end
end

def find_by_name(name)
each.find {|domain| domain.name == name }
end
end
end
end
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ end

### Using a Bearer Token

You can read more about [bearer tokens here](https://blog.cloudflare.com/api-tokens-general-availability/). This allows you to limit priviledges.
You can read more about [bearer tokens here](https://blog.cloudflare.com/api-tokens-general-availability/). This allows you to limit privileges.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks :p


``` ruby
require 'cloudflare'
Expand Down
8 changes: 8 additions & 0 deletions test/cloudflare/accounts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,12 @@

expect(namespace.resource.reference.path).to be(:end_with?, "/#{account.id}/storage/kv/namespaces")
end

it "can generate a representation for the R2 bucket endpoint" do
buckets = connection.accounts.find_by_id(account.id).r2_buckets

expect(buckets).to be_a(Cloudflare::R2::Buckets)

expect(buckets.resource.reference.path).to be(:end_with?, "/#{account.id}/r2/buckets")
end
end