Skip to content

Commit

Permalink
Initial migration of code.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Sep 11, 2024
1 parent 1c7da35 commit 8400a5b
Show file tree
Hide file tree
Showing 30 changed files with 124 additions and 127 deletions.
4 changes: 4 additions & 0 deletions .mailmap
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
Eric McKay <[email protected]>
Denis Sadomowski <[email protected]>
莫粒 <[email protected]>
Sherman Koa <[email protected]>
Fedishin Nazar <[email protected]>
Akinori Musha <[email protected]>
2 changes: 1 addition & 1 deletion cloudflare.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Gem::Specification.new do |spec|
spec.version = Cloudflare::VERSION

spec.summary = "A Ruby wrapper for the Cloudflare API."
spec.authors = ["Samuel Williams", "Marcin Prokop", "Leonhardt Wille", "Rob Widmer", "Akinori MUSHA", "Sherman K", "Michael Kalygin", "Denis Sadomowski", "Nazar", "emckay", "Casey Lopez", "David Wegman", "Greg Retkowski", "Guillaume Leseur", "Jason Green", "Kugayama Nana", "Kyle Corbitt", "Mike Perham", "Olle Jonsson", "Terry Kerr", "莫粒"]
spec.authors = ["Samuel Williams", "Marcin Prokop", "Leonhardt Wille", "Rob Widmer", "Akinori Musha", "Sherman Koa", "Michael Kalygin", "Denis Sadomowski", "Eric McKay", "Fedishin Nazar", "Casey Lopez", "David Wegman", "Greg Retkowski", "Guillaume Leseur", "Jason Green", "Kugayama Nana", "Kyle Corbitt", "Mike Perham", "Olle Jonsson", "Terry Kerr", "莫粒"]
spec.license = "MIT"

spec.cert_chain = ["release.cert"]
Expand Down
8 changes: 5 additions & 3 deletions gems.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2014-2020, by Samuel Williams.
# Copyright, 2014-2024, by Samuel Williams.
# Copyright, 2014, by Marcin Prokop.
# Copyright, 2018, by Leonhardt Wille.

Expand All @@ -19,13 +19,15 @@

group :test do
gem "rspec"
gem "covered"
gem "decode"
gem "rubocop"

gem "async-rspec"

gem "coveralls", require: false
gem "simplecov"
gem "sinatra"
gem "webmock"

gem "bake-test"
gem "bake-test-external"
end
32 changes: 15 additions & 17 deletions lib/cloudflare.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,31 @@

# Released under the MIT License.
# Copyright, 2012-2016, by Marcin Prokop.
# Copyright, 2013, by emckay.
# Copyright, 2013, by Eric McKay.
# Copyright, 2014, by Jason Green.
# Copyright, 2014-2019, by Samuel Williams.
# Copyright, 2014-2024, by Samuel Williams.
# Copyright, 2014, by Greg Retkowski.
# Copyright, 2018, by Leonhardt Wille.
# Copyright, 2019, by Akinori MUSHA.
# Copyright, 2019, by Akinori Musha.

require "async"
require_relative "cloudflare/connection"

module Cloudflare
DEFAULT_ENDPOINT = Async::HTTP::Endpoint.parse("https://api.cloudflare.com/client/v4/")

def self.connect(endpoint = DEFAULT_ENDPOINT, **auth_info)
representation = Connection.for(endpoint)

if !auth_info.empty?
representation = representation.authenticated(**auth_info)
end

return representation unless block_given?

Async do
def self.connect(*arguments, **auth_info)
Sync do
connection = Connection.open(*arguments)

if !auth_info.empty?
connection = connection.authenticated(**auth_info)
end

return connection unless block_given?

begin
yield representation
yield connection
ensure
representation.close
connection.close
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/cloudflare/accounts.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2018-2019, by Samuel Williams.
# Copyright, 2018-2024, by Samuel Williams.
# Copyright, 2019, by Rob Widmer.

require_relative "representation"
Expand Down
18 changes: 10 additions & 8 deletions lib/cloudflare/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@

# Released under the MIT License.
# Copyright, 2014-2016, by Marcin Prokop.
# Copyright, 2014-2019, by Samuel Williams.
# Copyright, 2014-2024, by Samuel Williams.
# Copyright, 2015, by Kyle Corbitt.
# Copyright, 2015, by Guillaume Leseur.
# Copyright, 2018, by Leonhardt Wille.
# Copyright, 2018, by Michael Kalygin.
# Copyright, 2018, by Sherman K.
# Copyright, 2019, by Akinori MUSHA.
# Copyright, 2018, by Sherman Koa.
# Copyright, 2019, by Akinori Musha.

require_relative "representation"
require "async/rest/resource"

require_relative "zones"
require_relative "accounts"
require_relative "user"

module Cloudflare
class Connection < Representation
class Connection < Async::REST::Resource
ENDPOINT = Async::HTTP::Endpoint.parse("https://api.cloudflare.com/client/v4/")

def authenticated(token: nil, key: nil, email: nil)
headers = {}

Expand All @@ -36,15 +38,15 @@ def authenticated(token: nil, key: nil, email: nil)
end

def zones
self.with(Zones, path: "zones/")
Zones.new(self.with(path: "zones/"))
end

def accounts
self.with(Accounts, path: "accounts")
Accounts.new(self.with(path: "accounts"))
end

def user
self.with(User, path: "user")
User.new(self.with(path: "user"))
end
end
end
2 changes: 1 addition & 1 deletion lib/cloudflare/custom_hostname/ssl_attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Released under the MIT License.
# Copyright, 2019, by Rob Widmer.
# Copyright, 2019, by Samuel Williams.
# Copyright, 2019-2024, by Samuel Williams.

require_relative "./ssl_attribute/settings"

Expand Down
2 changes: 1 addition & 1 deletion lib/cloudflare/custom_hostname/ssl_attribute/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Released under the MIT License.
# Copyright, 2019, by Rob Widmer.
# Copyright, 2019, by Samuel Williams.
# Copyright, 2019-2024, by Samuel Williams.

module Cloudflare
class CustomHostname < Representation
Expand Down
2 changes: 1 addition & 1 deletion lib/cloudflare/custom_hostnames.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Released under the MIT License.
# Copyright, 2019, by Rob Widmer.
# Copyright, 2019, by Samuel Williams.
# Copyright, 2019-2024, by Samuel Williams.

require_relative "custom_hostname/ssl_attribute"
require_relative "paginate"
Expand Down
2 changes: 1 addition & 1 deletion lib/cloudflare/dns.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2019, by Samuel Williams.
# Copyright, 2019-2024, by Samuel Williams.
# Copyright, 2019, by Rob Widmer.
# Copyright, 2019, by David Wegman.

Expand Down
2 changes: 1 addition & 1 deletion lib/cloudflare/firewall.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2019, by Samuel Williams.
# Copyright, 2019-2024, by Samuel Williams.
# Copyright, 2019, by Rob Widmer.

require_relative "representation"
Expand Down
2 changes: 1 addition & 1 deletion lib/cloudflare/kv/namespaces.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Released under the MIT License.
# Copyright, 2019, by Rob Widmer.
# Copyright, 2019, by Samuel Williams.
# Copyright, 2019-2024, by Samuel Williams.
# Copyright, 2021, by Terry Kerr.

require_relative "../paginate"
Expand Down
1 change: 1 addition & 0 deletions lib/cloudflare/kv/rest_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Released under the MIT License.
# Copyright, 2021, by Terry Kerr.
# Copyright, 2024, by Samuel Williams.

require "json"

Expand Down
2 changes: 1 addition & 1 deletion lib/cloudflare/logs.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2019, by Samuel Williams.
# Copyright, 2019-2024, by Samuel Williams.

require_relative "representation"
require_relative "paginate"
Expand Down
28 changes: 14 additions & 14 deletions lib/cloudflare/paginate.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2018-2019, by Samuel Williams.
# Copyright, 2018-2024, by Samuel Williams.
# Copyright, 2019, by Rob Widmer.

module Cloudflare
module Paginate
include Enumerable

def each(page: 1, per_page: 50, **parameters)
return to_enum(:each, page: page, per_page: per_page, **parameters) unless block_given?

while true
zones = @resource.get(self.class, page: page, per_page: per_page, **parameters)

break if zones.empty?

Array(zones.value).each do |attributes|
yield represent(zones.metadata, attributes)
response = self.class.get(@resource, {page: page, per_page: per_page, **parameters})
break if response.empty?
response.results.each do |attributes|
yield represent(response.metadata, attributes)
end

page += 1

# Was this the last page?
break if zones.value.size < per_page
break if response.results.size < per_page
end
end

def empty?
self.value.empty?
end

def find_by_id(id)
representation.new(@resource.with(path: "#{id}/"))
end
Expand Down
Loading

0 comments on commit 8400a5b

Please sign in to comment.