dato-rails 0.5.0 → 0.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +37 -3
- data/app/components/dato/live.html.erb +2 -6
- data/app/components/dato/live.rb +0 -8
- data/lib/dato/cache.rb +25 -0
- data/lib/dato/client.rb +6 -27
- 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: 7753883f681ceec3735a3ada67751e4ef59256775f99ddc4916c469e42119d83
|
4
|
+
data.tar.gz: 721f2b1c13b67796a74626bb9844b9d07aa6b0911f068e926fa1154cb4a76960
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 24b79eac4a15ca805d42151faa392f4174cdb53ccd5a9f253021d6f37c80736ace8a14ce04331e22acdaeb855abe39d99a7b3702fee140d49c6d586448d10791
|
7
|
+
data.tar.gz: fcf8912566cc3a242290a53b82e8337fe3306a7d6aea2073caba9ed8027923424ab173048974abdbd799f5843bb095711ada2ab415f6956e80a54ab479105e26
|
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:
|
@@ -153,7 +187,7 @@ end
|
|
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
|
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,12 @@
|
|
1
1
|
module Dato
|
2
|
-
class Client
|
2
|
+
class Client
|
3
|
+
delegate :live!, :execute!, :execute, to: :@gql
|
4
|
+
attr_reader :items, :gql
|
5
|
+
|
3
6
|
def initialize(api_token = Dato::Config.api_token, validate_query: false, preview: false, live: false)
|
4
7
|
@api_token = api_token
|
5
|
-
|
6
|
-
|
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
|
-
|
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)
|
8
|
+
@gql = Dato::Gql.new(api_token, validate_query, preview, live)
|
9
|
+
@items = Dato::Items.new(api_token)
|
31
10
|
end
|
32
11
|
end
|
33
12
|
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.1
|
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-02
|
11
|
+
date: 2023-05-02 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
|