cfc 0.1.3 → 0.1.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 820b83bedc1e954528d342f7060a43f05e1ca108293a0a6ec180604f1afbfa6b
4
- data.tar.gz: 6425fb5bade05ada6d104b10d6ef5bbf6817c9a10a7336645dcdb39bdb7486f3
3
+ metadata.gz: 3f5f2aefe1d6f36747241422a791bfa619b4d6dc53b9ef5e93a191b3162eed1a
4
+ data.tar.gz: 3a0d745cdc585a8290ab9bd0623299968e70fc344a638586115bc16cc35008ad
5
5
  SHA512:
6
- metadata.gz: af74f88b7fa189a2ff33a4ed8ca365e6fcfd3fc89711da0b5db369c07f1df9f1a17d08093e333c9651520c7dcf611f0a620bca49a8519e5c1083b5d028d97077
7
- data.tar.gz: 0d0440c563e99d4bd6e36f2801c7cec242df6aa1b870229169f08d112cb7f518654f714fae3c38a6acc5a526ffa5235a74c9872032f4ca4ba5776602f33f7e03
6
+ metadata.gz: e0d898e56d580631ab901c37453419d54d7a079378932af2518220294769122f3803d0db4b65b90f99fc1671294edc0973fcf244aee7c854be62ec5a4d37ba21
7
+ data.tar.gz: 66859b84c931ec9519afe931edb9ec48662607c886a030151dbc82f633056245b5a269a7e7c861745fdca1733da86e21d62bca7fa8ff07e177a2e96aeb9bad01
data/README.md CHANGED
@@ -19,10 +19,39 @@ CFC::Config.configure do |config|
19
19
  end
20
20
  ```
21
21
 
22
+ Alternatively, you can authenticate with your API key and email address:
23
+
24
+ ```ruby
25
+ CFC::Config.configure do |config|
26
+ config.api_key = 'your_api_key_here'
27
+ config.api_email = 'your_email_here'
28
+ end
29
+ ```
30
+
22
31
  You can then use the library either by instantiating `CFC::API` and using that class to send API requests directly,
23
- or, for simpler tasks, you can use pre-provided objects in `lib/objects/`, which represent a data type from the
24
- Cloudflare API and may provide methods to perform common or simple tasks on those types. See
25
- `lib/scripts/clear_cache.rb` for an example of how this may be done with `CFC::Zone` objects.
32
+ or, you can use pre-provided objects in `lib/objects/`, which represent a data type from the Cloudflare API and may
33
+ provide methods to perform common or simple tasks on those types. For instance, to list DNS records in all zones you
34
+ administer (assuming you've already configured the gem as above):
35
+
36
+ ```ruby
37
+ CFC::Zone.list.each do |zone|
38
+ puts zone.records
39
+ end
40
+ ```
41
+
42
+ Or if you need to roll all of your current API tokens:
43
+
44
+ ```ruby
45
+ new_tokens = CFC::UserAPIToken.list.each do |token|
46
+ token.roll['result']
47
+ end
48
+
49
+ # Once you've rolled tokens, of course, your existing token won't work, so you
50
+ # probably want to update that.
51
+ CFC::Config.configure do |config|
52
+ config.token = new_tokens[0]
53
+ end
54
+ ```
26
55
 
27
56
  ## Contributing
28
57
  As with all Codidact projects, contributions are welcome and must adhere to the
@@ -1,8 +1,17 @@
1
+ require 'json'
1
2
  require_relative 'object'
2
3
  require_relative 'permission_group'
3
4
 
4
5
  module CFC
5
6
  class APITokenPolicy < CFC::APIObject
6
7
  relationship :permission_groups, CFC::PermissionGroup, multiple: true
8
+
9
+ def self.build(effect:, resources:, permission_groups:)
10
+ new(JSON.parse(JSON.dump({
11
+ effect: effect,
12
+ resources: resources&.map { |k, v| [k.to_json, v] }&.to_h,
13
+ permission_groups: permission_groups
14
+ }.compact)))
15
+ end
7
16
  end
8
17
  end
@@ -34,6 +34,16 @@ module CFC
34
34
 
35
35
  alias to_s inspect
36
36
 
37
+ def to_h
38
+ @data
39
+ end
40
+
41
+ alias as_json to_h
42
+
43
+ def to_json(*args)
44
+ as_json.to_json(*args)
45
+ end
46
+
37
47
  def self.relationship(property, cls, multiple: false)
38
48
  unless defined?(@relationships)
39
49
  @relationships = []
@@ -1,6 +1,16 @@
1
+ require 'json'
1
2
  require_relative 'object'
2
3
 
3
4
  module CFC
4
5
  class PermissionGroup < CFC::APIObject
6
+ @api = CFC::API.new
7
+
8
+ def self.list
9
+ @api.get_json('user/tokens/permission_groups')['result'].map { |pg| new(pg) }
10
+ end
11
+
12
+ def to_json(*_args)
13
+ JSON.dump({ id: id, name: name })
14
+ end
5
15
  end
6
16
  end
@@ -24,6 +24,12 @@ module CFC
24
24
  @api.put_to_json("user/tokens/#{identifier}/value", {})
25
25
  end
26
26
 
27
+ def self.create(name:, policies:, not_before: nil, expires_on: nil, condition: nil)
28
+ data = { name: name, policies: policies, not_before: not_before, expires_on: expires_on, condition: condition }
29
+ data = data.compact
30
+ new(@api.post_to_json('user/tokens', data)['result'])
31
+ end
32
+
27
33
  def details
28
34
  CFC::UserAPIToken.details(id)
29
35
  end
@@ -1,3 +1,4 @@
1
+ require 'json'
1
2
  require_relative 'object'
2
3
  require_relative 'user'
3
4
  require_relative 'account'
@@ -23,5 +24,9 @@ module CFC
23
24
  data = @api.get_json("zones/#{id}/dns_records")['result']
24
25
  data.map { |r| CFC::Record.new(r) }
25
26
  end
27
+
28
+ def to_json(*_args)
29
+ "com.cloudflare.api.account.zone.#{id}"
30
+ end
26
31
  end
27
32
  end
@@ -1,3 +1,3 @@
1
1
  module CFC
2
- VERSION = '0.1.3'.freeze
2
+ VERSION = '0.1.4'.freeze
3
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.3
4
+ version: 0.1.4
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: 2021-01-02 00:00:00.000000000 Z
11
+ date: 2021-01-07 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'