From 8400a5b1c411979fee073778011860b747eed8d4 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Thu, 12 Sep 2024 00:28:31 +1200 Subject: [PATCH] Initial migration of code. --- .mailmap | 4 + cloudflare.gemspec | 2 +- gems.rb | 8 +- lib/cloudflare.rb | 32 +++--- lib/cloudflare/accounts.rb | 2 +- lib/cloudflare/connection.rb | 18 ++-- .../custom_hostname/ssl_attribute.rb | 2 +- .../custom_hostname/ssl_attribute/settings.rb | 2 +- lib/cloudflare/custom_hostnames.rb | 2 +- lib/cloudflare/dns.rb | 2 +- lib/cloudflare/firewall.rb | 2 +- lib/cloudflare/kv/namespaces.rb | 2 +- lib/cloudflare/kv/rest_wrapper.rb | 1 + lib/cloudflare/logs.rb | 2 +- lib/cloudflare/paginate.rb | 28 ++--- lib/cloudflare/representation.rb | 101 ++++++++---------- lib/cloudflare/rspec/connection.rb | 2 +- lib/cloudflare/user.rb | 2 +- lib/cloudflare/version.rb | 2 +- lib/cloudflare/zones.rb | 6 +- license.md | 10 +- spec/cloudflare/accounts_spec.rb | 1 + .../ssl_attribute/settings_spec.rb | 1 + .../custom_hostname/ssl_attribute_spec.rb | 1 + spec/cloudflare/custom_hostnames_spec.rb | 2 +- spec/cloudflare/dns_spec.rb | 2 +- spec/cloudflare/firewall_spec.rb | 2 +- spec/cloudflare/kv/namespaces_spec.rb | 2 +- spec/cloudflare/zone_spec.rb | 4 +- spec/spec_helper.rb | 4 +- 30 files changed, 124 insertions(+), 127 deletions(-) diff --git a/.mailmap b/.mailmap index 8da6766..2fb94e8 100644 --- a/.mailmap +++ b/.mailmap @@ -1,2 +1,6 @@ +Eric McKay Denis Sadomowski 莫粒 +Sherman Koa +Fedishin Nazar +Akinori Musha diff --git a/cloudflare.gemspec b/cloudflare.gemspec index 1dbf0b4..7d13b3e 100644 --- a/cloudflare.gemspec +++ b/cloudflare.gemspec @@ -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"] diff --git a/gems.rb b/gems.rb index 8a43a13..2853f3f 100644 --- a/gems.rb +++ b/gems.rb @@ -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. @@ -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 diff --git a/lib/cloudflare.rb b/lib/cloudflare.rb index 47a2c71..c018dec 100644 --- a/lib/cloudflare.rb +++ b/lib/cloudflare.rb @@ -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 diff --git a/lib/cloudflare/accounts.rb b/lib/cloudflare/accounts.rb index e552199..23bce50 100644 --- a/lib/cloudflare/accounts.rb +++ b/lib/cloudflare/accounts.rb @@ -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" diff --git a/lib/cloudflare/connection.rb b/lib/cloudflare/connection.rb index d39c02f..8704538 100644 --- a/lib/cloudflare/connection.rb +++ b/lib/cloudflare/connection.rb @@ -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 = {} @@ -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 diff --git a/lib/cloudflare/custom_hostname/ssl_attribute.rb b/lib/cloudflare/custom_hostname/ssl_attribute.rb index a60ce6c..f7479af 100644 --- a/lib/cloudflare/custom_hostname/ssl_attribute.rb +++ b/lib/cloudflare/custom_hostname/ssl_attribute.rb @@ -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" diff --git a/lib/cloudflare/custom_hostname/ssl_attribute/settings.rb b/lib/cloudflare/custom_hostname/ssl_attribute/settings.rb index d3db84f..5747867 100644 --- a/lib/cloudflare/custom_hostname/ssl_attribute/settings.rb +++ b/lib/cloudflare/custom_hostname/ssl_attribute/settings.rb @@ -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 diff --git a/lib/cloudflare/custom_hostnames.rb b/lib/cloudflare/custom_hostnames.rb index 9081927..8d3e1bd 100644 --- a/lib/cloudflare/custom_hostnames.rb +++ b/lib/cloudflare/custom_hostnames.rb @@ -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" diff --git a/lib/cloudflare/dns.rb b/lib/cloudflare/dns.rb index 4f19d9f..6697172 100644 --- a/lib/cloudflare/dns.rb +++ b/lib/cloudflare/dns.rb @@ -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. diff --git a/lib/cloudflare/firewall.rb b/lib/cloudflare/firewall.rb index d156736..7a42de7 100644 --- a/lib/cloudflare/firewall.rb +++ b/lib/cloudflare/firewall.rb @@ -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" diff --git a/lib/cloudflare/kv/namespaces.rb b/lib/cloudflare/kv/namespaces.rb index 14ad2e5..2570ba1 100644 --- a/lib/cloudflare/kv/namespaces.rb +++ b/lib/cloudflare/kv/namespaces.rb @@ -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" diff --git a/lib/cloudflare/kv/rest_wrapper.rb b/lib/cloudflare/kv/rest_wrapper.rb index 7508582..70c34a6 100644 --- a/lib/cloudflare/kv/rest_wrapper.rb +++ b/lib/cloudflare/kv/rest_wrapper.rb @@ -2,6 +2,7 @@ # Released under the MIT License. # Copyright, 2021, by Terry Kerr. +# Copyright, 2024, by Samuel Williams. require "json" diff --git a/lib/cloudflare/logs.rb b/lib/cloudflare/logs.rb index e3ece94..58aefb6 100644 --- a/lib/cloudflare/logs.rb +++ b/lib/cloudflare/logs.rb @@ -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" diff --git a/lib/cloudflare/paginate.rb b/lib/cloudflare/paginate.rb index afdcf79..6013927 100644 --- a/lib/cloudflare/paginate.rb +++ b/lib/cloudflare/paginate.rb @@ -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 diff --git a/lib/cloudflare/representation.rb b/lib/cloudflare/representation.rb index f13f094..1431921 100644 --- a/lib/cloudflare/representation.rb +++ b/lib/cloudflare/representation.rb @@ -1,96 +1,83 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2017-2018, by Samuel Williams. +# Copyright, 2017-2024, by Samuel Williams. # Copyright, 2018, by Leonhardt Wille. # Copyright, 2019, by Rob Widmer. require "json" require "async/rest/representation" +require "async/rest/wrapper/json" module Cloudflare class RequestError < StandardError def initialize(resource, errors) super("#{resource}: #{errors.map{|attributes| attributes[:message]}.join(', ')}") - + @representation = representation end - + attr_reader :representation end - - class Message - def initialize(response) - @response = response - @body = response.read - - # Some endpoints return the value instead of a message object (like KV reads) - @body = { success: true, result: @body } unless @body.is_a?(Hash) - end - - attr :response - attr :body - - def headers - @response.headers - end - - def result - @body[:result] - end - - def read - @body[:result] - end - - def results - Array(result) - end - - def errors - @body[:errors] - end - - def messages - @body[:messages] - end - - def success? - @body[:success] - end + + class Wrapper < Async::REST::Wrapper::JSON end - + class Representation < Async::REST::Representation - def process_response(*) - message = Message.new(super) - - unless message.success? - raise RequestError.new(@resource, message.errors) + WRAPPER = Wrapper.new + + def initialize(...) + super(...) + + # Some endpoints return the value instead of a message object (like KV reads) + unless @value.is_a?(Hash) + @value = {success: true, result: @value} end - - return message end - + def representation Representation end - + def represent(metadata, attributes) resource = @resource.with(path: attributes[:id]) - binding.irb - + representation.new(resource, metadata: metadata, value: attributes) end - + def represent_message(message) represent(message.headers, message.result) end - + def to_hash if value.is_a?(Hash) return value end end + + def result + value[:result] + end + + def read + value[:result] + end + + def results + Array(result) + end + + def errors + value[:errors] + end + + def messages + value[:messages] + end + + def success? + value[:success] + end end end diff --git a/lib/cloudflare/rspec/connection.rb b/lib/cloudflare/rspec/connection.rb index 0c0dae6..0c1a180 100644 --- a/lib/cloudflare/rspec/connection.rb +++ b/lib/cloudflare/rspec/connection.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2017-2020, by Samuel Williams. +# Copyright, 2017-2024, by Samuel Williams. # Copyright, 2018, by Leonhardt Wille. require "async/rspec" diff --git a/lib/cloudflare/user.rb b/lib/cloudflare/user.rb index 5405989..dde3d18 100644 --- a/lib/cloudflare/user.rb +++ b/lib/cloudflare/user.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2017-2019, by Samuel Williams. +# Copyright, 2017-2024, by Samuel Williams. # Copyright, 2018, by Leonhardt Wille. require_relative "representation" diff --git a/lib/cloudflare/version.rb b/lib/cloudflare/version.rb index edd5353..35fb796 100644 --- a/lib/cloudflare/version.rb +++ b/lib/cloudflare/version.rb @@ -2,7 +2,7 @@ # Released under the MIT License. # Copyright, 2014-2016, by Marcin Prokop. -# Copyright, 2014-2021, by Samuel Williams. +# Copyright, 2014-2024, by Samuel Williams. # Copyright, 2015, by Kyle Corbitt. # Copyright, 2018, by Leonhardt Wille. # Copyright, 2018, by Casey Lopez. diff --git a/lib/cloudflare/zones.rb b/lib/cloudflare/zones.rb index 8068439..73a12a3 100644 --- a/lib/cloudflare/zones.rb +++ b/lib/cloudflare/zones.rb @@ -1,15 +1,15 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2017-2019, by Samuel Williams. +# Copyright, 2017-2024, by Samuel Williams. # Copyright, 2017, by Denis Sadomowski. # Copyright, 2017, by 莫粒. # Copyright, 2018, by Leonhardt Wille. # Copyright, 2018, by Michael Kalygin. -# Copyright, 2018, by Sherman K. +# Copyright, 2018, by Sherman Koa. # Copyright, 2018, by Kugayama Nana. # Copyright, 2018, by Casey Lopez. -# Copyright, 2019, by Akinori MUSHA. +# Copyright, 2019, by Akinori Musha. # Copyright, 2019, by Rob Widmer. require_relative "representation" diff --git a/license.md b/license.md index 0fed9f2..2fe77a4 100644 --- a/license.md +++ b/license.md @@ -1,9 +1,9 @@ # MIT License Copyright, 2012-2017, by Marcin Prokop. -Copyright, 2013, by emckay. +Copyright, 2013, by Eric McKay. Copyright, 2014, by Jason Green. -Copyright, 2014-2021, by Samuel Williams. +Copyright, 2014-2024, by Samuel Williams. Copyright, 2014, by Greg Retkowski. Copyright, 2015, by Kyle Corbitt. Copyright, 2015, by Guillaume Leseur. @@ -12,12 +12,12 @@ Copyright, 2017, by 莫粒. Copyright, 2018, by Leonhardt Wille. Copyright, 2018, by Mike Perham. Copyright, 2018, by Michael Kalygin. -Copyright, 2018, by Sherman K. +Copyright, 2018, by Sherman Koa. Copyright, 2018, by Kugayama Nana. Copyright, 2018, by Casey Lopez. -Copyright, 2019, by Akinori MUSHA. +Copyright, 2019, by Akinori Musha. Copyright, 2019, by Rob Widmer. -Copyright, 2019, by Nazar. +Copyright, 2019, by Fedishin Nazar. Copyright, 2019, by David Wegman. Copyright, 2020, by Olle Jonsson. Copyright, 2021, by Terry Kerr. diff --git a/spec/cloudflare/accounts_spec.rb b/spec/cloudflare/accounts_spec.rb index 6c94765..c2700ff 100644 --- a/spec/cloudflare/accounts_spec.rb +++ b/spec/cloudflare/accounts_spec.rb @@ -2,6 +2,7 @@ # Released under the MIT License. # Copyright, 2019, by Rob Widmer. +# Copyright, 2024, by Samuel Williams. RSpec.describe Cloudflare::Accounts, order: :defined, timeout: 30 do include_context Cloudflare::Account diff --git a/spec/cloudflare/custom_hostname/ssl_attribute/settings_spec.rb b/spec/cloudflare/custom_hostname/ssl_attribute/settings_spec.rb index d068a09..7297a00 100644 --- a/spec/cloudflare/custom_hostname/ssl_attribute/settings_spec.rb +++ b/spec/cloudflare/custom_hostname/ssl_attribute/settings_spec.rb @@ -2,6 +2,7 @@ # Released under the MIT License. # Copyright, 2019, by Rob Widmer. +# Copyright, 2024, by Samuel Williams. RSpec.describe Cloudflare::CustomHostname::SSLAttribute::Settings do diff --git a/spec/cloudflare/custom_hostname/ssl_attribute_spec.rb b/spec/cloudflare/custom_hostname/ssl_attribute_spec.rb index 017b7e6..84653bb 100644 --- a/spec/cloudflare/custom_hostname/ssl_attribute_spec.rb +++ b/spec/cloudflare/custom_hostname/ssl_attribute_spec.rb @@ -2,6 +2,7 @@ # Released under the MIT License. # Copyright, 2019, by Rob Widmer. +# Copyright, 2024, by Samuel Williams. RSpec.describe Cloudflare::CustomHostname::SSLAttribute do diff --git a/spec/cloudflare/custom_hostnames_spec.rb b/spec/cloudflare/custom_hostnames_spec.rb index 1e0dc44..11e9411 100644 --- a/spec/cloudflare/custom_hostnames_spec.rb +++ b/spec/cloudflare/custom_hostnames_spec.rb @@ -2,7 +2,7 @@ # Released under the MIT License. # Copyright, 2019, by Rob Widmer. -# Copyright, 2019-2020, by Samuel Williams. +# Copyright, 2019-2024, by Samuel Williams. RSpec.xdescribe Cloudflare::CustomHostnames, order: :defined, timeout: 30 do include_context Cloudflare::Zone diff --git a/spec/cloudflare/dns_spec.rb b/spec/cloudflare/dns_spec.rb index 3cdfd30..28f03a6 100644 --- a/spec/cloudflare/dns_spec.rb +++ b/spec/cloudflare/dns_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2019-2020, by Samuel Williams. +# Copyright, 2019-2024, by Samuel Williams. # Copyright, 2019, by David Wegman. require "cloudflare/rspec/connection" diff --git a/spec/cloudflare/firewall_spec.rb b/spec/cloudflare/firewall_spec.rb index 72531d9..3296c49 100644 --- a/spec/cloudflare/firewall_spec.rb +++ b/spec/cloudflare/firewall_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2019-2020, by Samuel Williams. +# Copyright, 2019-2024, by Samuel Williams. require "cloudflare/rspec/connection" diff --git a/spec/cloudflare/kv/namespaces_spec.rb b/spec/cloudflare/kv/namespaces_spec.rb index 26af749..ce69086 100644 --- a/spec/cloudflare/kv/namespaces_spec.rb +++ b/spec/cloudflare/kv/namespaces_spec.rb @@ -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. RSpec.describe Cloudflare::KV::Namespaces, kv_spec: true, order: :defined, timeout: 30 do include_context Cloudflare::Account diff --git a/spec/cloudflare/zone_spec.rb b/spec/cloudflare/zone_spec.rb index 0af0cce..3bf4013 100644 --- a/spec/cloudflare/zone_spec.rb +++ b/spec/cloudflare/zone_spec.rb @@ -1,10 +1,10 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2017-2019, by Samuel Williams. +# Copyright, 2017-2024, by Samuel Williams. # Copyright, 2018, by Leonhardt Wille. # Copyright, 2018, by Michael Kalygin. -# Copyright, 2018, by Sherman K. +# Copyright, 2018, by Sherman Koa. # Copyright, 2019, by Rob Widmer. RSpec.describe Cloudflare::Zones, order: :defined, timeout: 30 do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index cff467d..5586ca8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,10 +1,10 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2017-2020, by Samuel Williams. +# Copyright, 2017-2024, by Samuel Williams. # Copyright, 2018, by Leonhardt Wille. # Copyright, 2018, by Michael Kalygin. -# Copyright, 2018, by Sherman K. +# Copyright, 2018, by Sherman Koa. # Copyright, 2019, by Rob Widmer. AUTH_EMAIL = ENV["CLOUDFLARE_EMAIL"]