dato-rails 0.4.0 → 0.7.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/README.md +38 -4
- data/app/components/dato/live.html.erb +2 -6
- data/app/components/dato/live.rb +0 -8
- data/app/controllers/dato/publish_controller.rb +4 -2
- data/lib/dato/cache.rb +25 -0
- data/lib/dato/client.rb +8 -28
- data/lib/dato/config.rb +2 -1
- data/lib/dato/gql.rb +24 -0
- data/lib/dato/items.rb +46 -0
- data/lib/dato/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 749f20170663bb4bccf768abb34aa420530affa26573f9b5f86fe3b6aa69d169
|
4
|
+
data.tar.gz: d6125ac3828889b22f0c4434b765f5a2570c5200fa3e2aec8e44440bab2aa866
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 15760b48a1ca878ac2ca1bedea696d2d55d6c144f1fa1ac614000806d4da9e5c45825252e6f8be074d9b4ae813ac67b3feb5a87c47ef9dfb91aba90ee38a5e4b
|
7
|
+
data.tar.gz: fcf53744435481c917eb80bdc8cddf5e6bc3fa507e1049b457dad62cda150405c8abbdc40d1e8656d89baeb2273b871f3442b1290809d7fbcf3994dc7d6e22c5
|
data/README.md
CHANGED
@@ -26,7 +26,7 @@ The gem is made of two parts:
|
|
26
26
|
|
27
27
|
## GraphQL client
|
28
28
|
|
29
|
-
The GraphQL client based on [GQLi](https://github.com/contentful-labs/gqli.rb) allows to perform
|
29
|
+
The GraphQL client based on [GQLi](https://github.com/contentful-labs/gqli.rb) allows to perform
|
30
30
|
queries to the GraphQL endpoint.
|
31
31
|
Look at GQLi documentation for more information about the syntax of queries.
|
32
32
|
You can also find some examples in specs of this library.
|
@@ -104,7 +104,7 @@ Dato::Config.overrides = {
|
|
104
104
|
}.with_indifferent_access
|
105
105
|
```
|
106
106
|
|
107
|
-
|
107
|
+
## Preview and live
|
108
108
|
|
109
109
|
The `Dato::Client` supports both [preview](https://www.datocms.com/docs/pro-tips/how-to-manage-a-live-and-a-preview-site) and [live updates](https://www.datocms.com/docs/real-time-updates-api) features from Dato CMS.
|
110
110
|
|
@@ -134,6 +134,40 @@ render(Dato::Live.new(MyComponent, my_query, preview: true, live: true))
|
|
134
134
|
|
135
135
|
and your component will come to life with live updates 🎉 (requires turbo).
|
136
136
|
|
137
|
+
## CRUD Operations
|
138
|
+
|
139
|
+
The library also provides a set of helpers to perform CRUD operations on Dato CMS.
|
140
|
+
|
141
|
+
### All
|
142
|
+
|
143
|
+
```ruby
|
144
|
+
Dato::Client.new.items.all(item_type_id: '456')
|
145
|
+
```
|
146
|
+
|
147
|
+
### Find
|
148
|
+
|
149
|
+
```ruby
|
150
|
+
Dato::Client.new.items.find(item_id: '123')
|
151
|
+
```
|
152
|
+
|
153
|
+
### Create
|
154
|
+
|
155
|
+
```ruby
|
156
|
+
Dato::Client.new.items.create(attributes: { title: 'Hello world' }, item_type_id: '456')
|
157
|
+
```
|
158
|
+
|
159
|
+
### Update
|
160
|
+
|
161
|
+
```ruby
|
162
|
+
Dato::Client.new.items.update(attributes: { title: 'Hello world' }, item_id: '123')
|
163
|
+
```
|
164
|
+
|
165
|
+
### Destroy
|
166
|
+
|
167
|
+
```ruby
|
168
|
+
Dato::Client.new.items.destroy(item_id: '123')
|
169
|
+
```
|
170
|
+
|
137
171
|
## Configuration
|
138
172
|
|
139
173
|
The following options are available:
|
@@ -147,13 +181,13 @@ Dato::Config.configure do |config|
|
|
147
181
|
config.cache = false # default: false
|
148
182
|
config.cache_namespace = 'dato-rails' # default: 'dato-rails'
|
149
183
|
config.publish_key = ENV['DATO_PUBLISH_KEY'] # default: ENV['DATO_PUBLISH_KEY']
|
150
|
-
config.
|
184
|
+
config.build_trigger_id = ENV['DATO_BUILD_TRIGGER_ID'] # default: ENV['DATO_BUILD_TRIGGER_ID']
|
151
185
|
end
|
152
186
|
```
|
153
187
|
|
154
188
|
## Caching
|
155
189
|
|
156
|
-
The library supports caching of the rendered components.
|
190
|
+
The library supports caching of the rendered components.
|
157
191
|
If you enable caching, the components rendered using `Dato::Live`, will be cached.
|
158
192
|
|
159
193
|
To enable caching, you need to set the `cache` option in the configuration.
|
@@ -16,11 +16,7 @@
|
|
16
16
|
|
17
17
|
<%= turbo_frame_tag frame_id %>
|
18
18
|
<% else %>
|
19
|
-
|
20
|
-
|
21
|
-
<% render(component_klass.new(data)) %>
|
22
|
-
<% end %>
|
23
|
-
<% else %>
|
24
|
-
<%= render(component_klass.new(data)) %>
|
19
|
+
<%= Dato::Cache.fetch(cache_key) do %>
|
20
|
+
<% render(component_klass.new(data)) %>
|
25
21
|
<% end %>
|
26
22
|
<% end %>
|
data/app/components/dato/live.rb
CHANGED
@@ -17,18 +17,10 @@ module Dato
|
|
17
17
|
@live = live
|
18
18
|
end
|
19
19
|
|
20
|
-
def cache?
|
21
|
-
Dato::Config.cache.present?
|
22
|
-
end
|
23
|
-
|
24
20
|
def cache_key
|
25
21
|
@cache_key ||= Digest::MD5.hexdigest(query.to_gql)
|
26
22
|
end
|
27
23
|
|
28
|
-
def cache_time
|
29
|
-
@cache_time ||= Dato::Config.cache.is_a?(Integer) ? Dato::Config.cache : 60.minutes
|
30
|
-
end
|
31
|
-
|
32
24
|
def data
|
33
25
|
@data ||= dato_fetch(query, preview: preview, live: live)
|
34
26
|
end
|
@@ -23,14 +23,16 @@ module Dato
|
|
23
23
|
private
|
24
24
|
|
25
25
|
def notify_success
|
26
|
-
if Dato::Config.
|
26
|
+
if Dato::Config.build_trigger_id.present?
|
27
27
|
Thread.new do
|
28
28
|
sleep 5 # wait for the build to finish
|
29
|
-
uri = URI("https://webhooks.datocms.com/#{Dato::Config.
|
29
|
+
uri = URI("https://webhooks.datocms.com/#{Dato::Config.build_trigger_id}/deploy-results")
|
30
30
|
req = Net::HTTP::Post.new(uri, "Content-Type" => "application/json")
|
31
31
|
req.body = {status: "success"}.to_json
|
32
32
|
Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http| http.request(req) }
|
33
33
|
end
|
34
|
+
else
|
35
|
+
Rails.logger.info "Dato::Config.build_trigger_id is not set, skipping notification."
|
34
36
|
end
|
35
37
|
end
|
36
38
|
end
|
data/lib/dato/cache.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
module Dato
|
2
|
+
class Cache
|
3
|
+
def self.fetch(cache_key, &block)
|
4
|
+
if active?
|
5
|
+
Rails.cache.fetch(cache_key, expires_in: expires_in, namespace: namespace) do
|
6
|
+
block.call
|
7
|
+
end
|
8
|
+
else
|
9
|
+
block.call
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.active?
|
14
|
+
Dato::Config.cache.present?
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.expires_in
|
18
|
+
Dato::Config.cache.is_a?(Integer) ? Dato::Config.cache : 60.minutes
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.namespace
|
22
|
+
Dato::Config.cache_namespace
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/dato/client.rb
CHANGED
@@ -1,33 +1,13 @@
|
|
1
1
|
module Dato
|
2
|
-
class Client
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
"https://graphql#{"-listen" if live}.datocms.com/#{"preview" if preview}",
|
7
|
-
headers: {
|
8
|
-
"Authorization" => @api_token
|
9
|
-
},
|
10
|
-
validate_query: validate_query && !live
|
11
|
-
)
|
12
|
-
end
|
13
|
-
|
14
|
-
def live!(query)
|
15
|
-
http_response = request.post(@url, params: @params, json: {query: query.to_gql})
|
16
|
-
|
17
|
-
fail "Error: #{http_response.reason}\nBody: #{http_response.body}" if http_response.status >= 300
|
2
|
+
class Client
|
3
|
+
delegate :live!, to: :@gql_client
|
4
|
+
delegate :execute!, to: :@gql_client
|
5
|
+
attr_reader :items
|
18
6
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
def search(query)
|
25
|
-
url = "https://site-api.datocms.com/search-results?query=#{query}&filter[build_trigger_id]=''"
|
26
|
-
response = HTTP.headers({
|
27
|
-
"Authorization" => "Bearer #{@api_token}",
|
28
|
-
"Accept" => "application/json",
|
29
|
-
"X-Api-Version" => 3
|
30
|
-
}).timeout(timeout_options).get(url)
|
7
|
+
def initialize(api_token = Dato::Config.api_token, validate_query: false, preview: false, live: false)
|
8
|
+
@api_token = api_token
|
9
|
+
@gql_client = Dato::Gql.new(api_token, validate_query, preview, live)
|
10
|
+
@items = Dato::Items.new(api_token)
|
31
11
|
end
|
32
12
|
end
|
33
13
|
end
|
data/lib/dato/config.rb
CHANGED
@@ -6,7 +6,8 @@ module Dato
|
|
6
6
|
config_accessor(:blocks) { {} }
|
7
7
|
config_accessor(:cache) { false }
|
8
8
|
config_accessor(:cache_namespace) { "dato-rails" }
|
9
|
+
config_accessor(:api_token) { ENV["DATO_API_TOKEN"] }
|
9
10
|
config_accessor(:publish_key) { ENV["DATO_PUBLISH_KEY"] }
|
10
|
-
config_accessor(:
|
11
|
+
config_accessor(:build_trigger_id) { ENV["DATO_BUILD_TRIGGER_ID"] }
|
11
12
|
end
|
12
13
|
end
|
data/lib/dato/gql.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
module Dato
|
2
|
+
class Gql < GQLi::Client
|
3
|
+
def initialize(api_token, validate_query, preview, live)
|
4
|
+
@api_token = api_token
|
5
|
+
super(
|
6
|
+
"https://graphql#{"-listen" if live}.datocms.com/#{"preview" if preview}",
|
7
|
+
headers: {
|
8
|
+
"Authorization" => @api_token
|
9
|
+
},
|
10
|
+
validate_query: validate_query && !live
|
11
|
+
)
|
12
|
+
end
|
13
|
+
|
14
|
+
def live!(query)
|
15
|
+
http_response = request.post(@url, params: @params, json: {query: query.to_gql})
|
16
|
+
|
17
|
+
fail "Error: #{http_response.reason}\nBody: #{http_response.body}" if http_response.status >= 300
|
18
|
+
|
19
|
+
parsed_response = JSON.parse(http_response.to_s)
|
20
|
+
errors = parsed_response["errors"]
|
21
|
+
GQLi::Response.new(parsed_response, errors, query)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/dato/items.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
module Dato
|
2
|
+
class Items
|
3
|
+
BASE_ITEM_URL = "https://site-api.datocms.com/items"
|
4
|
+
|
5
|
+
def initialize(api_token)
|
6
|
+
@http_headers = HTTP.headers({"Authorization" => "Bearer #{api_token}",
|
7
|
+
"Accept" => "application/json",
|
8
|
+
"X-Api-Version" => 3})
|
9
|
+
end
|
10
|
+
|
11
|
+
def find(item_id:)
|
12
|
+
@http_headers.get("#{BASE_ITEM_URL}/#{item_id}")
|
13
|
+
end
|
14
|
+
|
15
|
+
def all(item_type_id:)
|
16
|
+
@http_headers.get("#{BASE_ITEM_URL}?filter[type]=#{item_type_id}")
|
17
|
+
end
|
18
|
+
|
19
|
+
def create(item_type_id:, attributes:, meta: nil)
|
20
|
+
data = create_attributes(attributes, item_type_id)
|
21
|
+
data[:meta] = meta if meta
|
22
|
+
@http_headers.post(BASE_ITEM_URL, json: {data: data})
|
23
|
+
end
|
24
|
+
|
25
|
+
def update(attributes:, item_id:)
|
26
|
+
data = {type: "item", attributes: attributes, id: item_id}
|
27
|
+
@http_headers.put("#{BASE_ITEM_URL}/#{item_id}", json: {data: data})
|
28
|
+
end
|
29
|
+
|
30
|
+
def destroy(item_id:)
|
31
|
+
@http_headers.delete("#{BASE_ITEM_URL}/#{item_id}")
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def create_attributes(attributes, item_type_id)
|
37
|
+
{
|
38
|
+
type: "item",
|
39
|
+
attributes: attributes,
|
40
|
+
relationships: {
|
41
|
+
item_type: {data: {type: "item_type", id: item_type_id}}
|
42
|
+
}
|
43
|
+
}
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/lib/dato/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dato-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alessandro Rodi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-04-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec-rails
|
@@ -209,10 +209,13 @@ files:
|
|
209
209
|
- app/views/dato/live/show.html.erb
|
210
210
|
- config/routes.rb
|
211
211
|
- lib/dato.rb
|
212
|
+
- lib/dato/cache.rb
|
212
213
|
- lib/dato/client.rb
|
213
214
|
- lib/dato/config.rb
|
214
215
|
- lib/dato/engine.rb
|
215
216
|
- lib/dato/fragments/responsive_image.rb
|
217
|
+
- lib/dato/gql.rb
|
218
|
+
- lib/dato/items.rb
|
216
219
|
- lib/dato/version.rb
|
217
220
|
- lib/tasks/dato/rails_tasks.rake
|
218
221
|
homepage: https://github.com/renuo/dato-rails
|