dato-rails 0.5.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 911f15a149ea496d7b60b9d1890fbc5014152c8f2c87e86d62025ca974415a6b
4
- data.tar.gz: e043584a63573e2ba261a4a314b09a33de91f8259a9bc5ecb070590e9e6762bc
3
+ metadata.gz: 749f20170663bb4bccf768abb34aa420530affa26573f9b5f86fe3b6aa69d169
4
+ data.tar.gz: d6125ac3828889b22f0c4434b765f5a2570c5200fa3e2aec8e44440bab2aa866
5
5
  SHA512:
6
- metadata.gz: 9693562a097f1663f46df5736ebb61beee103ef34127eff8dc19e681390612cb66da1b5bde29dd62cbdc0c3d764d782d1201e7483b0f70659bdd44f6d5de7064
7
- data.tar.gz: efd0f16c08406b2c2e0440d5980f70c1a783189e280ccf53fac37149a8308e9ace91eeecd59790d3e21278915348c1d1263bc074330cd801783210d2cd01e1e2
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
- # Preview and live
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
- <% if cache? %>
20
- <%= Rails.cache.fetch(cache_key, expires_in: cache_time, namespace: Dato::Config.cache_namespace) do %>
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 %>
@@ -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,13 @@
1
1
  module Dato
2
- class Client < GQLi::Client
2
+ class Client
3
+ delegate :live!, to: :@gql_client
4
+ delegate :execute!, to: :@gql_client
5
+ attr_reader :items
6
+
3
7
  def initialize(api_token = Dato::Config.api_token, validate_query: false, preview: false, live: false)
4
8
  @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
-
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)
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/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
@@ -1,3 +1,3 @@
1
1
  module Dato
2
- VERSION = "0.5.0"
2
+ VERSION = "0.7.0"
3
3
  end
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.5.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-02-14 00:00:00.000000000 Z
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