cfc 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/cfc.rb +3 -0
- data/lib/cfc/api.rb +28 -10
- data/lib/cfc/cache.rb +1 -1
- data/lib/cfc/config.rb +9 -2
- data/lib/cfc/errors/configuration_error.rb +6 -0
- data/lib/cfc/errors/missing_property.rb +2 -6
- data/lib/cfc/objects/account.rb +0 -3
- data/lib/cfc/objects/api_token_policy.rb +8 -0
- data/lib/cfc/objects/object.rb +19 -11
- data/lib/cfc/objects/permission_group.rb +6 -0
- data/lib/cfc/objects/record.rb +3 -5
- data/lib/cfc/objects/user.rb +17 -2
- data/lib/cfc/objects/user_api_token.rb +39 -0
- data/lib/cfc/objects/zone.rb +0 -4
- data/lib/cfc/version.rb +2 -2
- metadata +8 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 820b83bedc1e954528d342f7060a43f05e1ca108293a0a6ec180604f1afbfa6b
|
4
|
+
data.tar.gz: 6425fb5bade05ada6d104b10d6ef5bbf6817c9a10a7336645dcdb39bdb7486f3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: af74f88b7fa189a2ff33a4ed8ca365e6fcfd3fc89711da0b5db369c07f1df9f1a17d08093e333c9651520c7dcf611f0a620bca49a8519e5c1083b5d028d97077
|
7
|
+
data.tar.gz: 0d0440c563e99d4bd6e36f2801c7cec242df6aa1b870229169f08d112cb7f518654f714fae3c38a6acc5a526ffa5235a74c9872032f4ca4ba5776602f33f7e03
|
data/lib/cfc.rb
CHANGED
@@ -3,8 +3,11 @@ require_relative 'cfc/config'
|
|
3
3
|
require_relative 'cfc/version'
|
4
4
|
|
5
5
|
require_relative 'cfc/objects/account'
|
6
|
+
require_relative 'cfc/objects/api_token_policy'
|
7
|
+
require_relative 'cfc/objects/permission_group'
|
6
8
|
require_relative 'cfc/objects/record'
|
7
9
|
require_relative 'cfc/objects/user'
|
10
|
+
require_relative 'cfc/objects/user_api_token'
|
8
11
|
require_relative 'cfc/objects/zone'
|
9
12
|
|
10
13
|
module CFC; end
|
data/lib/cfc/api.rb
CHANGED
@@ -4,7 +4,7 @@ require 'digest'
|
|
4
4
|
require 'yaml'
|
5
5
|
require_relative 'config'
|
6
6
|
require_relative 'cache'
|
7
|
-
require_relative 'errors/http_error
|
7
|
+
require_relative 'errors/http_error'
|
8
8
|
|
9
9
|
module CFC
|
10
10
|
class API
|
@@ -21,33 +21,51 @@ module CFC
|
|
21
21
|
request_json(Net::HTTP::Get, build_uri(path, params), headers: headers, cache: cache, expiry: expiry)
|
22
22
|
end
|
23
23
|
|
24
|
+
def delete(path, data: {}, headers: nil, cache: false, expiry: nil)
|
25
|
+
request(Net::HTTP::Delete, URI("#{@base}#{path}"), data: data, headers: headers, cache: cache, expiry: expiry)
|
26
|
+
end
|
27
|
+
|
28
|
+
def delete_to_json(path, data: {}, headers: nil, cache: false, expiry: nil)
|
29
|
+
request_json(Net::HTTP::Delete, URI("#{@base}#{path}"), data: data, headers: headers, cache: cache,
|
30
|
+
expiry: expiry)
|
31
|
+
end
|
32
|
+
|
24
33
|
[:post, :put, :patch].each do |method|
|
25
34
|
define_method method do |path, data, headers: nil, cache: false, expiry: nil|
|
26
|
-
request("Net::HTTP::#{method.capitalize}".constantize, URI("#{@base}#{path}"), data: data, headers: headers,
|
35
|
+
request("Net::HTTP::#{method.capitalize}".constantize, URI("#{@base}#{path}"), data: data, headers: headers,
|
36
|
+
cache: cache, expiry: expiry)
|
27
37
|
end
|
28
38
|
|
29
39
|
define_method "#{method}_to_json" do |path, data, headers: nil, cache: false, expiry: nil|
|
30
|
-
request_json(Object.const_get("Net::HTTP::#{method.capitalize}"), URI("#{@base}#{path}"), data: data,
|
31
|
-
expiry: expiry)
|
40
|
+
request_json(Object.const_get("Net::HTTP::#{method.capitalize}"), URI("#{@base}#{path}"), data: data,
|
41
|
+
headers: headers, cache: cache, expiry: expiry)
|
32
42
|
end
|
33
43
|
end
|
34
44
|
|
35
45
|
protected
|
36
46
|
|
37
47
|
def request(cls, uri, data: nil, headers: nil, cache: true, expiry: nil)
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
48
|
+
headers = (headers || {}).merge({ 'Content-Type' => 'application/json' })
|
49
|
+
@auth_method = CFC::Config.instance.token.nil? ? :key : :token
|
50
|
+
|
51
|
+
case @auth_method
|
52
|
+
when :token
|
53
|
+
headers.merge!({ 'Authentication' => "Bearer #{CFC::Config.instance.token}" })
|
54
|
+
when :key
|
55
|
+
headers.merge!({
|
56
|
+
'X-Auth-Key' => CFC::Config.instance.api_key,
|
57
|
+
'X-Auth-Email' => CFC::Config.instance.api_email
|
58
|
+
})
|
59
|
+
end
|
42
60
|
|
43
|
-
rq = cls.new(uri,
|
61
|
+
rq = cls.new(uri, headers)
|
44
62
|
unless data.nil? || rq.is_a?(Net::HTTP::Head)
|
45
63
|
rq.body = JSON.dump(data)
|
46
64
|
end
|
47
65
|
|
48
66
|
response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
|
49
67
|
if cache
|
50
|
-
@cache.fetch(Digest::SHA256.hexdigest("#{uri}|#{JSON.dump(
|
68
|
+
@cache.fetch(Digest::SHA256.hexdigest("#{uri}|#{JSON.dump(headers)}"), expiry: expiry) do
|
51
69
|
http.request(rq)
|
52
70
|
end
|
53
71
|
else
|
data/lib/cfc/cache.rb
CHANGED
data/lib/cfc/config.rb
CHANGED
@@ -1,12 +1,19 @@
|
|
1
1
|
require 'singleton'
|
2
|
+
require_relative 'errors/configuration_error'
|
2
3
|
|
3
4
|
module CFC
|
4
5
|
class Config
|
5
6
|
include Singleton
|
6
|
-
attr_accessor :token
|
7
|
+
attr_accessor :token, :api_key, :api_email
|
7
8
|
|
8
9
|
def self.configure
|
9
10
|
yield CFC::Config.instance
|
11
|
+
if [instance.token, instance.api_key, instance.api_email].all?(&:nil?)
|
12
|
+
raise CFC::Errors::ConfigurationError, 'Either `token` or BOTH of `api_key`, `api_email` must be set on call ' \
|
13
|
+
"to `configure'."
|
14
|
+
elsif instance.token.nil? && [instance.api_key, instance.api_email].any?(&:nil?)
|
15
|
+
raise CFC::Errors::ConfigurationError, 'Both `api_key` AND `api_email` must be set when not using token auth.'
|
16
|
+
end
|
10
17
|
end
|
11
18
|
end
|
12
|
-
end
|
19
|
+
end
|
@@ -2,12 +2,8 @@ module CFC
|
|
2
2
|
module Errors
|
3
3
|
class MissingProperty < StandardError
|
4
4
|
def self.default_message(obj, property)
|
5
|
-
"This #{obj.class.name} does not have a `#{property}' property. If you are accessing this object from a
|
6
|
-
|
7
|
-
end
|
8
|
-
|
9
|
-
def initialize(message)
|
10
|
-
super
|
5
|
+
"This #{obj.class.name} does not have a `#{property}' property. If you are accessing this object from a " \
|
6
|
+
'relationship on another object, the property may not have been fetched.'
|
11
7
|
end
|
12
8
|
end
|
13
9
|
end
|
data/lib/cfc/objects/account.rb
CHANGED
data/lib/cfc/objects/object.rb
CHANGED
@@ -5,8 +5,8 @@ module CFC
|
|
5
5
|
class APIObject
|
6
6
|
@relationships = []
|
7
7
|
|
8
|
-
|
9
|
-
|
8
|
+
class << self
|
9
|
+
attr_reader :relationships
|
10
10
|
end
|
11
11
|
|
12
12
|
def initialize(data)
|
@@ -23,7 +23,7 @@ module CFC
|
|
23
23
|
CFC::Errors::MissingProperty.default_message(self, name)
|
24
24
|
end
|
25
25
|
end
|
26
|
-
|
26
|
+
|
27
27
|
def respond_to_missing?(name, *_args, **_opts)
|
28
28
|
@data.include?(name.to_s)
|
29
29
|
end
|
@@ -32,24 +32,32 @@ module CFC
|
|
32
32
|
"#<#{self.class.name}:0x#{(object_id << 1).to_s(16)} #{@data.map { |k, v| "#{k}=#{v.inspect}" }.join(', ')}>"
|
33
33
|
end
|
34
34
|
|
35
|
-
|
36
|
-
|
37
|
-
protected
|
35
|
+
alias to_s inspect
|
38
36
|
|
39
|
-
def self.relationship(property, cls)
|
37
|
+
def self.relationship(property, cls, multiple: false)
|
40
38
|
unless defined?(@relationships)
|
41
39
|
@relationships = []
|
42
40
|
end
|
43
|
-
@relationships << [property, cls]
|
41
|
+
@relationships << [property, cls, { multiple: multiple }]
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.opts(bind)
|
45
|
+
bind.local_variables.map do |var|
|
46
|
+
[var, bind.local_variable_get(var)]
|
47
|
+
end.to_h
|
44
48
|
end
|
45
49
|
|
46
50
|
private
|
47
51
|
|
48
52
|
def initialize_relationships
|
49
53
|
(self.class.relationships || []).each do |rel|
|
50
|
-
property, cls = rel
|
51
|
-
@data[property.to_s] =
|
54
|
+
property, cls, opts = rel
|
55
|
+
@data[property.to_s] = if opts[:multiple]
|
56
|
+
@data[property.to_s].map { |o| cls.new(o) }
|
57
|
+
else
|
58
|
+
cls.new(@data[property.to_s])
|
59
|
+
end
|
52
60
|
end
|
53
61
|
end
|
54
62
|
end
|
55
|
-
end
|
63
|
+
end
|
data/lib/cfc/objects/record.rb
CHANGED
@@ -1,9 +1,7 @@
|
|
1
|
+
require_relative 'object'
|
2
|
+
|
1
3
|
module CFC
|
2
4
|
class Record < CFC::APIObject
|
3
|
-
def initialize(data)
|
4
|
-
super(data)
|
5
|
-
end
|
6
|
-
|
7
5
|
def proxy
|
8
6
|
set_proxied(true)
|
9
7
|
end
|
@@ -14,7 +12,7 @@ module CFC
|
|
14
12
|
|
15
13
|
private
|
16
14
|
|
17
|
-
def set_proxied(to)
|
15
|
+
def set_proxied(to) # rubocop:disable Naming/AccessorMethodName
|
18
16
|
@api.patch_to_json("zones/#{zone_id}/dns_records/#{id}", { proxied: to })
|
19
17
|
end
|
20
18
|
end
|
data/lib/cfc/objects/user.rb
CHANGED
@@ -2,8 +2,23 @@ require_relative 'object'
|
|
2
2
|
|
3
3
|
module CFC
|
4
4
|
class User < CFC::APIObject
|
5
|
-
|
6
|
-
|
5
|
+
@api = CFC::API.new
|
6
|
+
|
7
|
+
# Get full details of the current user.
|
8
|
+
def self.details
|
9
|
+
new(@api.get_json('user')['result'])
|
10
|
+
end
|
11
|
+
|
12
|
+
# Update part of the current user's details as permitted.
|
13
|
+
#
|
14
|
+
# @param first_name [String]
|
15
|
+
# @param last_name [String]
|
16
|
+
# @param telephone [String]
|
17
|
+
# @param country [String]
|
18
|
+
# @param zipcode [String]
|
19
|
+
def self.edit(first_name: nil, last_name: nil, telephone: nil, country: nil, zipcode: nil)
|
20
|
+
data = opts(binding)
|
21
|
+
@api.patch_to_json('user', data)
|
7
22
|
end
|
8
23
|
end
|
9
24
|
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require_relative 'object'
|
2
|
+
require_relative 'api_token_policy'
|
3
|
+
|
4
|
+
module CFC
|
5
|
+
class UserAPIToken < CFC::APIObject
|
6
|
+
relationship :policies, CFC::APITokenPolicy, multiple: true
|
7
|
+
|
8
|
+
@api = CFC::API.new
|
9
|
+
|
10
|
+
def self.list(page: nil, per_page: nil, direction: nil)
|
11
|
+
params = opts(binding).compact
|
12
|
+
@api.get_json('user/tokens', params: params)['result'].map { |o| new(o) }
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.details(identifier)
|
16
|
+
new(@api.get_json("user/tokens/#{identifier}")['result'])
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.delete(identifier)
|
20
|
+
@api.delete_to_json("user/tokens/#{identifier}")
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.roll(identifier)
|
24
|
+
@api.put_to_json("user/tokens/#{identifier}/value", {})
|
25
|
+
end
|
26
|
+
|
27
|
+
def details
|
28
|
+
CFC::UserAPIToken.details(id)
|
29
|
+
end
|
30
|
+
|
31
|
+
def delete
|
32
|
+
CFC::UserAPIToken.delete(id)
|
33
|
+
end
|
34
|
+
|
35
|
+
def roll
|
36
|
+
CFC::UserAPIToken.roll(id)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/cfc/objects/zone.rb
CHANGED
data/lib/cfc/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
module CFC
|
2
|
-
VERSION = '0.1.
|
3
|
-
end
|
2
|
+
VERSION = '0.1.3'.freeze
|
3
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cfc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- The Codidact Foundation
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-01-02 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: 'Simple API library for interacting with the Cloudflare API. See GitHub
|
14
14
|
for usage details: https://github.com/codidact/cfc'
|
@@ -23,12 +23,16 @@ files:
|
|
23
23
|
- lib/cfc/api.rb
|
24
24
|
- lib/cfc/cache.rb
|
25
25
|
- lib/cfc/config.rb
|
26
|
+
- lib/cfc/errors/configuration_error.rb
|
26
27
|
- lib/cfc/errors/http_error.rb
|
27
28
|
- lib/cfc/errors/missing_property.rb
|
28
29
|
- lib/cfc/objects/account.rb
|
30
|
+
- lib/cfc/objects/api_token_policy.rb
|
29
31
|
- lib/cfc/objects/object.rb
|
32
|
+
- lib/cfc/objects/permission_group.rb
|
30
33
|
- lib/cfc/objects/record.rb
|
31
34
|
- lib/cfc/objects/user.rb
|
35
|
+
- lib/cfc/objects/user_api_token.rb
|
32
36
|
- lib/cfc/objects/zone.rb
|
33
37
|
- lib/cfc/version.rb
|
34
38
|
homepage: https://github.com/codidact/cfc
|
@@ -43,14 +47,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
43
47
|
requirements:
|
44
48
|
- - ">="
|
45
49
|
- !ruby/object:Gem::Version
|
46
|
-
version: 2.
|
50
|
+
version: 2.4.0
|
47
51
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
52
|
requirements:
|
49
53
|
- - ">="
|
50
54
|
- !ruby/object:Gem::Version
|
51
55
|
version: '0'
|
52
56
|
requirements: []
|
53
|
-
rubygems_version: 3.0.
|
57
|
+
rubygems_version: 3.0.3
|
54
58
|
signing_key:
|
55
59
|
specification_version: 4
|
56
60
|
summary: Simple API library for interacting with the Cloudflare API.
|