cloudflare 4.3.0 → 4.5.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.
@@ -1,70 +1,59 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
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.
3
+ # Released under the MIT License.
4
+ # Copyright, 2019-2024, by Samuel Williams.
5
+ # Copyright, 2019, by Rob Widmer.
22
6
 
23
- require_relative 'representation'
24
- require_relative 'paginate'
7
+ require_relative "representation"
8
+ require_relative "paginate"
25
9
 
26
10
  module Cloudflare
27
11
  module Firewall
28
12
  class Rule < Representation
13
+ include Async::REST::Representation::Mutable
14
+
29
15
  def mode
30
- value[:mode]
16
+ result[:mode]
31
17
  end
32
-
18
+
33
19
  def notes
34
- value[:notes]
20
+ result[:notes]
35
21
  end
36
-
22
+
37
23
  def configuration
38
- value[:configuration]
24
+ result[:configuration]
39
25
  end
40
-
26
+
41
27
  def to_s
42
28
  "#{configuration[:value]} - #{mode} - #{notes}"
43
29
  end
44
30
  end
45
-
31
+
46
32
  class Rules < Representation
47
33
  include Paginate
48
-
34
+
49
35
  def representation
50
36
  Rule
51
37
  end
52
-
53
- def set(mode, value, notes: nil, target: 'ip')
38
+
39
+ def set(mode, value, notes: nil, target: "ip")
54
40
  notes ||= "cloudflare gem [#{mode}] #{Time.now.strftime('%m/%d/%y')}"
55
-
56
- message = self.post({
57
- mode: mode.to_s,
58
- notes: notes,
59
- configuration: {
60
- target: target,
61
- value: value.to_s,
62
- }
63
- })
64
-
65
- represent_message(message)
41
+
42
+ payload = {mode: mode.to_s, notes: notes, configuration: {target: target, value: value.to_s}}
43
+
44
+ Rule.post(@resource, payload) do |resource, response|
45
+ value = response.read
46
+ result = value[:result]
47
+ metadata = response.headers
48
+
49
+ if id = result[:id]
50
+ resource = resource.with(path: id)
51
+ end
52
+
53
+ Rule.new(resource, value: value, metadata: metadata)
54
+ end
66
55
  end
67
-
56
+
68
57
  def each_by_value(value, &block)
69
58
  each(configuration_value: value, &block)
70
59
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "representation"
4
+
5
+ module Cloudflare
6
+ class IPs < Representation
7
+ def cidrs(ipv: nil)
8
+ if ipv
9
+ result[:"ipv#{ipv}_cidrs"]
10
+ else
11
+ result[:ipv4_cidrs].to_a + result[:ipv6_cidrs].to_a
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,79 +1,113 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # This implements the Worker KV Store API
4
- # https://api.cloudflare.com/#workers-kv-namespace-properties
3
+ # Released under the MIT License.
4
+ # Copyright, 2019, by Rob Widmer.
5
+ # Copyright, 2019-2024, by Samuel Williams.
6
+ # Copyright, 2021, by Terry Kerr.
5
7
 
6
- require_relative '../paginate'
7
- require_relative '../representation'
8
- require_relative 'rest_wrapper'
8
+ require_relative "../paginate"
9
+ require_relative "../representation"
10
+ require_relative "wrapper"
9
11
 
10
12
  module Cloudflare
11
13
  module KV
12
14
  class Key < Representation
13
15
  def name
14
- value[:name]
16
+ result[:name]
15
17
  end
16
18
  end
17
-
19
+
20
+ class Value < Representation[Wrapper]
21
+ include Async::REST::Representation::Mutable
22
+
23
+ def put(value)
24
+ self.class.put(@resource, value) do |resource, response|
25
+ value = response.read
26
+
27
+ return value[:success]
28
+ end
29
+ end
30
+ end
31
+
18
32
  class Keys < Representation
19
33
  include Paginate
20
-
34
+
21
35
  def representation
22
36
  Key
23
37
  end
24
38
  end
25
-
39
+
26
40
  class Namespace < Representation
41
+ include Async::REST::Representation::Mutable
42
+
27
43
  def delete_value(name)
28
44
  value_representation(name).delete.success?
29
45
  end
30
-
46
+
31
47
  def id
32
- value[:id]
48
+ result[:id]
33
49
  end
34
-
50
+
35
51
  def keys
36
- self.with(Keys, path: 'keys')
52
+ self.with(Keys, path: "keys")
37
53
  end
38
-
54
+
39
55
  def read_value(name)
40
56
  value_representation(name).value
41
57
  end
42
-
58
+
43
59
  def rename(new_title)
44
- put(title: new_title)
45
- value[:title] = new_title
60
+ self.class.put(@resource, title: new_title) do |resource, response|
61
+ value = response.read
62
+
63
+ if value[:success]
64
+ result[:title] = new_title
65
+ else
66
+ raise RequestError.new(resource, value)
67
+ end
68
+ end
46
69
  end
47
-
70
+
48
71
  def title
49
- value[:title]
72
+ result[:title]
50
73
  end
51
-
74
+
52
75
  def write_value(name, value)
53
- value_representation(name).put(value).success?
76
+ value_representation(name).put(value)
54
77
  end
55
-
78
+
56
79
  private
57
-
80
+
58
81
  def value_representation(name)
59
- @representation_class ||= Representation[RESTWrapper]
60
- self.with(@representation_class, path: "values/#{name}")
82
+ self.with(Value, path: "values/#{name}/")
61
83
  end
62
84
  end
63
-
85
+
64
86
  class Namespaces < Representation
65
87
  include Paginate
66
-
88
+
67
89
  def representation
68
90
  Namespace
69
91
  end
70
-
71
- def create(title)
72
- represent_message(post(title: title))
92
+
93
+ def create(title, **options)
94
+ payload = {title: title, **options}
95
+
96
+ Namespace.post(@resource, payload) do |resource, response|
97
+ value = response.read
98
+ result = value[:result]
99
+ metadata = response.headers
100
+
101
+ if id = result[:id]
102
+ resource = resource.with(path: id)
103
+ end
104
+
105
+ Namespace.new(resource, value: value, metadata: metadata)
106
+ end
73
107
  end
74
-
108
+
75
109
  def find_by_title(title)
76
- each.find {|namespace| namespace.title == title }
110
+ each.find{|namespace| namespace.title == title}
77
111
  end
78
112
  end
79
113
  end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Released under the MIT License.
4
+ # Copyright, 2021, by Terry Kerr.
5
+ # Copyright, 2024, by Samuel Williams.
6
+
7
+ require "json"
8
+
9
+ module Cloudflare
10
+ module KV
11
+ class Wrapper < Cloudflare::Wrapper
12
+ APPLICATION_OCTET_STREAM = "application/octet-stream"
13
+ def prepare_request(request, payload)
14
+ request.headers.add("accept", APPLICATION_OCTET_STREAM)
15
+
16
+ if payload
17
+ request.headers["content-type"] = APPLICATION_OCTET_STREAM
18
+
19
+ request.body = ::Protocol::HTTP::Body::Buffered.new([payload.to_s])
20
+ end
21
+ end
22
+
23
+ def parser_for(response)
24
+ if response.headers["content-type"].start_with?(APPLICATION_OCTET_STREAM)
25
+ OctetParser
26
+ else
27
+ super
28
+ end
29
+ end
30
+
31
+ class OctetParser < ::Protocol::HTTP::Body::Wrapper
32
+ def join
33
+ super.force_encoding(Encoding::BINARY)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,34 +1,19 @@
1
- # Copyright, 2019, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
- #
3
- # Permission is hereby granted, free of charge, to any person obtaining a copy
4
- # of this software and associated documentation files (the "Software"), to deal
5
- # in the Software without restriction, including without limitation the rights
6
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- # copies of the Software, and to permit persons to whom the Software is
8
- # furnished to do so, subject to the following conditions:
9
- #
10
- # The above copyright notice and this permission notice shall be included in
11
- # all copies or substantial portions of the Software.
12
- #
13
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
- # THE SOFTWARE.
1
+ # frozen_string_literal: true
20
2
 
21
- require_relative 'representation'
22
- require_relative 'paginate'
3
+ # Released under the MIT License.
4
+ # Copyright, 2019-2024, by Samuel Williams.
5
+
6
+ require_relative "representation"
7
+ require_relative "paginate"
23
8
 
24
9
  module Cloudflare
25
10
  module Logs
26
11
  class Entry < Representation
27
12
  def to_s
28
- "#{value[:rayid]}-#{value[:ClientRequestURI]}"
13
+ "#{result[:rayid]}-#{result[:ClientRequestURI]}"
29
14
  end
30
15
  end
31
-
16
+
32
17
  class Received < Representation
33
18
  include Paginate
34
19
 
@@ -38,4 +23,3 @@ module Cloudflare
38
23
  end
39
24
  end
40
25
  end
41
-
@@ -1,52 +1,40 @@
1
- # Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
- #
3
- # Permission is hereby granted, free of charge, to any person obtaining a copy
4
- # of this software and associated documentation files (the "Software"), to deal
5
- # in the Software without restriction, including without limitation the rights
6
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- # copies of the Software, and to permit persons to whom the Software is
8
- # furnished to do so, subject to the following conditions:
9
- #
10
- # The above copyright notice and this permission notice shall be included in
11
- # all copies or substantial portions of the Software.
12
- #
13
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
- # THE SOFTWARE.
1
+ # frozen_string_literal: true
2
+
3
+ # Released under the MIT License.
4
+ # Copyright, 2018-2024, by Samuel Williams.
5
+ # Copyright, 2019, by Rob Widmer.
20
6
 
21
7
  module Cloudflare
22
8
  module Paginate
23
9
  include Enumerable
24
-
10
+
25
11
  def each(page: 1, per_page: 50, **parameters)
26
12
  return to_enum(:each, page: page, per_page: per_page, **parameters) unless block_given?
27
-
13
+
28
14
  while true
29
- zones = @resource.get(self.class, page: page, per_page: per_page, **parameters)
30
-
31
- break if zones.empty?
32
-
33
- Array(zones.value).each do |attributes|
34
- yield represent(zones.metadata, attributes)
15
+ resource = @resource.with(parameters: {page: page, per_page: per_page, **parameters})
16
+
17
+ response = self.class.get(resource)
18
+
19
+ break if response.empty?
20
+
21
+ response.results.each do |attributes|
22
+ yield represent(response.metadata, attributes)
35
23
  end
36
-
24
+
37
25
  page += 1
38
-
26
+
39
27
  # Was this the last page?
40
- break if zones.value.size < per_page
28
+ break if response.results.size < per_page
41
29
  end
42
30
  end
43
-
31
+
44
32
  def empty?
45
33
  self.value.empty?
46
34
  end
47
-
35
+
48
36
  def find_by_id(id)
49
- representation.new(@resource.with(path: id))
37
+ representation.new(@resource.with(path: "#{id}/"))
50
38
  end
51
39
  end
52
40
  end
@@ -1,111 +1,73 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright, 2012, by Marcin Prokop.
4
- # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
5
- #
6
- # Permission is hereby granted, free of charge, to any person obtaining a copy
7
- # of this software and associated documentation files (the "Software"), to deal
8
- # in the Software without restriction, including without limitation the rights
9
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
- # copies of the Software, and to permit persons to whom the Software is
11
- # furnished to do so, subject to the following conditions:
12
- #
13
- # The above copyright notice and this permission notice shall be included in
14
- # all copies or substantial portions of the Software.
15
- #
16
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
- # THE SOFTWARE.
3
+ # Released under the MIT License.
4
+ # Copyright, 2017-2024, by Samuel Williams.
5
+ # Copyright, 2018, by Leonhardt Wille.
6
+ # Copyright, 2019, by Rob Widmer.
23
7
 
24
- require 'json'
8
+ require "json"
25
9
 
26
- require 'async/rest/representation'
10
+ require "async/rest/representation"
11
+ require "async/rest/wrapper/json"
27
12
 
28
- module Cloudflare
29
- class RequestError < StandardError
30
- def initialize(resource, errors)
31
- super("#{resource}: #{errors.map{|attributes| attributes[:message]}.join(', ')}")
13
+ require_relative "request_error"
32
14
 
33
- @representation = representation
15
+ module Cloudflare
16
+ class Wrapper < Async::REST::Wrapper::JSON
17
+ def process_response(request, response)
18
+ super
19
+
20
+ if response.failure?
21
+ raise RequestError.new(request, response.read)
22
+ end
34
23
  end
35
-
36
- attr_reader :representation
37
24
  end
38
-
39
- class Message
40
- def initialize(response)
41
- @response = response
42
- @body = response.read
43
-
44
- # Some endpoints return the value instead of a message object (like KV reads)
45
- @body = { success: true, result: @body } unless @body.is_a?(Hash)
25
+
26
+ class Representation < Async::REST::Representation
27
+ WRAPPER = Wrapper.new
28
+
29
+ def representation
30
+ Representation
46
31
  end
47
-
48
- attr :response
49
- attr :body
50
-
51
- def headers
52
- @response.headers
32
+
33
+ def represent(metadata, attributes)
34
+ resource = @resource.with(path: attributes[:id])
35
+
36
+ representation.new(resource, metadata: metadata, value: {
37
+ success: true, result: attributes
38
+ })
53
39
  end
54
-
40
+
41
+ def represent_message(message)
42
+ represent(message.headers, message.result)
43
+ end
44
+
55
45
  def result
56
- @body[:result]
46
+ value[:result]
57
47
  end
58
-
59
- def read
60
- @body[:result]
48
+
49
+ def to_hash
50
+ result
61
51
  end
62
-
52
+
53
+ def to_id
54
+ {id: result[:id]}
55
+ end
56
+
63
57
  def results
64
58
  Array(result)
65
59
  end
66
-
60
+
67
61
  def errors
68
- @body[:errors]
62
+ value[:errors]
69
63
  end
70
-
64
+
71
65
  def messages
72
- @body[:messages]
66
+ value[:messages]
73
67
  end
74
-
68
+
75
69
  def success?
76
- @body[:success]
77
- end
78
- end
79
-
80
- class Representation < Async::REST::Representation
81
- def process_response(*)
82
- message = Message.new(super)
83
-
84
- unless message.success?
85
- raise RequestError.new(@resource, message.errors)
86
- end
87
-
88
- return message
89
- end
90
-
91
- def representation
92
- Representation
93
- end
94
-
95
- def represent(metadata, attributes)
96
- resource = @resource.with(path: attributes[:id])
97
-
98
- representation.new(resource, metadata: metadata, value: attributes)
99
- end
100
-
101
- def represent_message(message)
102
- represent(message.headers, message.result)
103
- end
104
-
105
- def to_hash
106
- if value.is_a?(Hash)
107
- return value
108
- end
70
+ value[:success]
109
71
  end
110
72
  end
111
73
  end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Released under the MIT License.
4
+ # Copyright, 2017-2024, by Samuel Williams.
5
+
6
+ module Cloudflare
7
+ class RequestError < StandardError
8
+ def self.error_string_for(value)
9
+ if value.is_a?(Hash)
10
+ if error = value[:error]
11
+ return error
12
+ elsif errors = value[:errors]
13
+ return errors.map{|attributes| attributes[:message]}.join(", ")
14
+ end
15
+ end
16
+
17
+ return value.inspect
18
+ end
19
+
20
+ def initialize(request, value)
21
+ super("#{request}: #{self.class.error_string_for(value)}")
22
+
23
+ @value = value
24
+ end
25
+
26
+ attr :value
27
+ end
28
+ end
@@ -1,36 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright, 2012, by Marcin Prokop.
4
- # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
5
- #
6
- # Permission is hereby granted, free of charge, to any person obtaining a copy
7
- # of this software and associated documentation files (the "Software"), to deal
8
- # in the Software without restriction, including without limitation the rights
9
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
- # copies of the Software, and to permit persons to whom the Software is
11
- # furnished to do so, subject to the following conditions:
12
- #
13
- # The above copyright notice and this permission notice shall be included in
14
- # all copies or substantial portions of the Software.
15
- #
16
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
- # THE SOFTWARE.
3
+ # Released under the MIT License.
4
+ # Copyright, 2017-2024, by Samuel Williams.
5
+ # Copyright, 2018, by Leonhardt Wille.
23
6
 
24
- require_relative 'representation'
7
+ require_relative "representation"
25
8
 
26
9
  module Cloudflare
27
10
  class User < Representation
28
11
  def id
29
- value[:id]
12
+ result[:id]
30
13
  end
31
14
 
32
15
  def email
33
- value[:email]
16
+ result[:email]
34
17
  end
35
18
  end
36
19
  end