bridgetown_directus 0.1.3 → 0.3.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.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +23 -0
- data/.github/workflows/release-please.yml +20 -0
- data/.github/workflows/release.yml +28 -0
- data/.gitignore +3 -0
- data/.release-please-config.json +10 -0
- data/.release-please-manifest.json +3 -0
- data/CHANGELOG.md +17 -0
- data/README.md +75 -116
- data/Rakefile +1 -1
- data/bridgetown.automation.rb +50 -48
- data/bridgetown_directus.gemspec +2 -2
- data/example/bridgetown.config.yml +24 -0
- data/example/config/initializers.rb +34 -0
- data/lib/bridgetown_directus/builder.rb +216 -70
- data/lib/bridgetown_directus/client.rb +71 -0
- data/lib/bridgetown_directus/configuration.rb +82 -0
- data/lib/bridgetown_directus/data_mapper.rb +160 -0
- data/lib/bridgetown_directus/utils.rb +6 -1
- data/lib/bridgetown_directus/version.rb +1 -1
- data/lib/bridgetown_directus.rb +21 -19
- metadata +19 -14
- data/lib/bridgetown_directus/api_client.rb +0 -57
data/lib/bridgetown_directus.rb
CHANGED
|
@@ -2,36 +2,38 @@
|
|
|
2
2
|
|
|
3
3
|
require "bridgetown"
|
|
4
4
|
require_relative "bridgetown_directus/utils"
|
|
5
|
-
require_relative "bridgetown_directus/
|
|
5
|
+
require_relative "bridgetown_directus/client"
|
|
6
|
+
require_relative "bridgetown_directus/data_mapper"
|
|
7
|
+
require_relative "bridgetown_directus/configuration"
|
|
6
8
|
require_relative "bridgetown_directus/builder"
|
|
7
9
|
|
|
8
10
|
module BridgetownDirectus
|
|
9
11
|
# Bridgetown initializer for the plugin
|
|
10
|
-
Bridgetown.initializer :bridgetown_directus do |config
|
|
11
|
-
config.bridgetown_directus
|
|
12
|
-
config.bridgetown_directus
|
|
13
|
-
config.bridgetown_directus.token ||= token || ENV.fetch("DIRECTUS_API_TOKEN")
|
|
12
|
+
Bridgetown.initializer :bridgetown_directus do |config|
|
|
13
|
+
# Only assign config.bridgetown_directus if not already set
|
|
14
|
+
config.bridgetown_directus ||= Configuration.new
|
|
14
15
|
|
|
15
|
-
#
|
|
16
|
-
config.bridgetown_directus.
|
|
17
|
-
config.bridgetown_directus.
|
|
18
|
-
config.bridgetown_directus.translations ||= config.directus["translations"]
|
|
16
|
+
# Set up configuration directly (leave to user initializer if possible)
|
|
17
|
+
config.bridgetown_directus.api_url ||= ENV["DIRECTUS_API_URL"]
|
|
18
|
+
config.bridgetown_directus.token ||= ENV["DIRECTUS_API_TOKEN"] || ENV["DIRECTUS_TOKEN"]
|
|
19
19
|
|
|
20
20
|
# Register the builder
|
|
21
21
|
config.builder BridgetownDirectus::Builder
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
class Configuration
|
|
25
|
+
attr_accessor :api_url, :token
|
|
26
|
+
attr_reader :collections
|
|
22
27
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
translatable_fields = config.bridgetown_directus.translations["fields"] || []
|
|
26
|
-
Bridgetown.logger.info "Directus translations enabled for fields: #{translatable_fields.join(', ')}"
|
|
27
|
-
else
|
|
28
|
-
Bridgetown.logger.info "Directus translations are disabled"
|
|
28
|
+
def initialize
|
|
29
|
+
@collections = {}
|
|
29
30
|
end
|
|
30
31
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
def register_collection(name, &block)
|
|
33
|
+
collection = CollectionConfig.new(name)
|
|
34
|
+
collection.instance_eval(&block) if block_given?
|
|
35
|
+
@collections[name] = collection
|
|
36
|
+
collection
|
|
35
37
|
end
|
|
36
38
|
end
|
|
37
39
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: bridgetown_directus
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Munkun
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: bridgetown
|
|
@@ -16,20 +15,20 @@ dependencies:
|
|
|
16
15
|
requirements:
|
|
17
16
|
- - ">="
|
|
18
17
|
- !ruby/object:Gem::Version
|
|
19
|
-
version:
|
|
18
|
+
version: 2.0.0.beta4
|
|
20
19
|
- - "<"
|
|
21
20
|
- !ruby/object:Gem::Version
|
|
22
|
-
version: '
|
|
21
|
+
version: '3.0'
|
|
23
22
|
type: :runtime
|
|
24
23
|
prerelease: false
|
|
25
24
|
version_requirements: !ruby/object:Gem::Requirement
|
|
26
25
|
requirements:
|
|
27
26
|
- - ">="
|
|
28
27
|
- !ruby/object:Gem::Version
|
|
29
|
-
version:
|
|
28
|
+
version: 2.0.0.beta4
|
|
30
29
|
- - "<"
|
|
31
30
|
- !ruby/object:Gem::Version
|
|
32
|
-
version: '
|
|
31
|
+
version: '3.0'
|
|
33
32
|
- !ruby/object:Gem::Dependency
|
|
34
33
|
name: faraday
|
|
35
34
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -120,14 +119,14 @@ dependencies:
|
|
|
120
119
|
requirements:
|
|
121
120
|
- - "~>"
|
|
122
121
|
- !ruby/object:Gem::Version
|
|
123
|
-
version:
|
|
122
|
+
version: 0.0.2
|
|
124
123
|
type: :development
|
|
125
124
|
prerelease: false
|
|
126
125
|
version_requirements: !ruby/object:Gem::Requirement
|
|
127
126
|
requirements:
|
|
128
127
|
- - "~>"
|
|
129
128
|
- !ruby/object:Gem::Version
|
|
130
|
-
version:
|
|
129
|
+
version: 0.0.2
|
|
131
130
|
- !ruby/object:Gem::Dependency
|
|
132
131
|
name: minitest-reporters
|
|
133
132
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -156,13 +155,17 @@ dependencies:
|
|
|
156
155
|
- - "~>"
|
|
157
156
|
- !ruby/object:Gem::Version
|
|
158
157
|
version: '3.0'
|
|
159
|
-
description:
|
|
160
158
|
email: development@munkun.com
|
|
161
159
|
executables: []
|
|
162
160
|
extensions: []
|
|
163
161
|
extra_rdoc_files: []
|
|
164
162
|
files:
|
|
163
|
+
- ".github/workflows/ci.yml"
|
|
164
|
+
- ".github/workflows/release-please.yml"
|
|
165
|
+
- ".github/workflows/release.yml"
|
|
165
166
|
- ".gitignore"
|
|
167
|
+
- ".release-please-config.json"
|
|
168
|
+
- ".release-please-manifest.json"
|
|
166
169
|
- ".rubocop.yml"
|
|
167
170
|
- CHANGELOG.md
|
|
168
171
|
- Gemfile
|
|
@@ -171,9 +174,13 @@ files:
|
|
|
171
174
|
- Rakefile
|
|
172
175
|
- bridgetown.automation.rb
|
|
173
176
|
- bridgetown_directus.gemspec
|
|
177
|
+
- example/bridgetown.config.yml
|
|
178
|
+
- example/config/initializers.rb
|
|
174
179
|
- lib/bridgetown_directus.rb
|
|
175
|
-
- lib/bridgetown_directus/api_client.rb
|
|
176
180
|
- lib/bridgetown_directus/builder.rb
|
|
181
|
+
- lib/bridgetown_directus/client.rb
|
|
182
|
+
- lib/bridgetown_directus/configuration.rb
|
|
183
|
+
- lib/bridgetown_directus/data_mapper.rb
|
|
177
184
|
- lib/bridgetown_directus/utils.rb
|
|
178
185
|
- lib/bridgetown_directus/version.rb
|
|
179
186
|
- package.json
|
|
@@ -185,7 +192,6 @@ metadata:
|
|
|
185
192
|
bug_tracker_uri: https://github.com/munkun-estudio/bridgetown_directus/issues
|
|
186
193
|
changelog_uri: https://github.com/munkun-estudio/bridgetown_directus/releases
|
|
187
194
|
homepage_uri: https://github.com/munkun-estudio/bridgetown_directus
|
|
188
|
-
post_install_message:
|
|
189
195
|
rdoc_options: []
|
|
190
196
|
require_paths:
|
|
191
197
|
- lib
|
|
@@ -200,8 +206,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
200
206
|
- !ruby/object:Gem::Version
|
|
201
207
|
version: '0'
|
|
202
208
|
requirements: []
|
|
203
|
-
rubygems_version: 3.
|
|
204
|
-
signing_key:
|
|
209
|
+
rubygems_version: 3.7.2
|
|
205
210
|
specification_version: 4
|
|
206
211
|
summary: Use Directus as headless CMS for Bridgetown
|
|
207
212
|
test_files: []
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module BridgetownDirectus
|
|
4
|
-
class APIClient
|
|
5
|
-
def initialize(site)
|
|
6
|
-
@site = site
|
|
7
|
-
@api_url = site.config.bridgetown_directus.api_url
|
|
8
|
-
@api_token = site.config.bridgetown_directus.token
|
|
9
|
-
|
|
10
|
-
raise StandardError, "Invalid Directus configuration: missing API token or URL" if @api_token.nil? || @api_url.nil?
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
# Main method to fetch posts
|
|
14
|
-
def fetch_posts
|
|
15
|
-
Utils.log_directus "Request URL: #{@api_url}/items/#{@site.config.bridgetown_directus.collection}"
|
|
16
|
-
|
|
17
|
-
response = connection.get("/items/#{@site.config.bridgetown_directus.collection}") do |req|
|
|
18
|
-
req.params['filter'] = { status: { _eq: "published" } }.to_json
|
|
19
|
-
req.params['fields'] = '*,translations.*'
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
if response.success?
|
|
23
|
-
JSON.parse(response.body) # Return the parsed posts
|
|
24
|
-
elsif response.status == 401
|
|
25
|
-
raise RuntimeError, "Unauthorized access to Directus API"
|
|
26
|
-
else
|
|
27
|
-
raise "Error fetching posts: #{response.status} - #{response.body}"
|
|
28
|
-
end
|
|
29
|
-
rescue Faraday::TimeoutError
|
|
30
|
-
raise Faraday::TimeoutError, "The request to fetch posts timed out"
|
|
31
|
-
rescue JSON::ParserError
|
|
32
|
-
raise JSON::ParserError, "The response from Directus was not valid JSON"
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
# Setup Faraday connection with authorization headers
|
|
36
|
-
def connection
|
|
37
|
-
Faraday.new(url: @api_url) do |faraday|
|
|
38
|
-
faraday.options.timeout = 5
|
|
39
|
-
faraday.options.open_timeout = 2
|
|
40
|
-
faraday.headers['Authorization'] = "Bearer #{@api_token}"
|
|
41
|
-
faraday.headers['Content-Type'] = 'application/json'
|
|
42
|
-
faraday.adapter Faraday.default_adapter
|
|
43
|
-
end
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
# New method for validating the data structure
|
|
47
|
-
private def validate_posts_data(posts_data)
|
|
48
|
-
if posts_data.is_a?(Hash) && posts_data.key?("data") && posts_data["data"].is_a?(Array)
|
|
49
|
-
posts_data["data"]
|
|
50
|
-
elsif posts_data.is_a?(Array)
|
|
51
|
-
posts_data
|
|
52
|
-
else
|
|
53
|
-
raise "Invalid posts data structure: #{posts_data.inspect}"
|
|
54
|
-
end
|
|
55
|
-
end
|
|
56
|
-
end
|
|
57
|
-
end
|