dato-rails 0.7.1 → 0.7.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 +4 -4
- data/README.md +40 -2
- data/Rakefile +1 -1
- data/app/components/dato/blockquote.html.erb +9 -7
- data/app/components/dato/dast_node.html.erb +6 -6
- data/app/components/dato/link.html.erb +5 -5
- data/app/components/dato/link.rb +15 -0
- data/app/components/dato/span.html.erb +5 -5
- data/lib/dato/client.rb +2 -1
- data/lib/dato/uploads.rb +111 -0
- data/lib/dato/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cd0388aa8f2f5230feb9b58e501d0d2b32d9a276eccf4029864061ea4fb42745
|
4
|
+
data.tar.gz: c084383123cfa44fe5834c8eb61e247d8b5482f4ce983bd9f712044e36fe1f5e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6cde762f15270f855bc106c523e6d428fca9763005b380177852fa990627ccd35445ba6abe8913d76f9899f6c7a30a132a829ef6ac00be2bda9d672faff99299
|
7
|
+
data.tar.gz: e54d6ac310cc81b0c62f65470e4a2c9ab0333e7ff71e9ca652a83f0ee1e3b9699c4889140cf3a6f8aaae421a3785433deb4eed48b01878ff9cfc5973afb7bbbd
|
data/README.md
CHANGED
@@ -168,6 +168,44 @@ Dato::Client.new.items.update(attributes: { title: 'Hello world' }, item_id: '12
|
|
168
168
|
Dato::Client.new.items.destroy(item_id: '123')
|
169
169
|
```
|
170
170
|
|
171
|
+
## File upload
|
172
|
+
|
173
|
+
Dato Rails also supports file uploads.
|
174
|
+
These can be created either from a local file or from a url.
|
175
|
+
Basically all file types are supported, as long as they are valid in the CMS.
|
176
|
+
|
177
|
+
> In addition to the binary file, also attributes and metadata can be uploaded.
|
178
|
+
Both metadata and attributes are optional.
|
179
|
+
> Provide attributes according to the [docs](https://www.datocms.com/docs/content-management-api/resources/upload)
|
180
|
+
|
181
|
+
### Upload from Url
|
182
|
+
```ruby
|
183
|
+
Dato::Client.new.uploads.create_from_url('https://picsum.photos/seed/picsum/200/300')
|
184
|
+
Dato::Client.new.uploads.create_from_url('https://picsum.photos/seed/picsum/200/300', attributes:)
|
185
|
+
```
|
186
|
+
|
187
|
+
### Upload from Local File
|
188
|
+
```ruby
|
189
|
+
file = File.open('image.png')
|
190
|
+
|
191
|
+
Dato::Client.new.uploads.create_from_file(file.path)
|
192
|
+
Dato::Client.new.uploads.create_from_file(file.path, attributes:)
|
193
|
+
```
|
194
|
+
|
195
|
+
### Optional: Attributes and metadata
|
196
|
+
|
197
|
+
```ruby
|
198
|
+
meta = { en: { title: 'Static Random Image', alt: 'some caption text' } }
|
199
|
+
attributes = { author: 'Dato Rails', default_field_metadata: meta }
|
200
|
+
```
|
201
|
+
|
202
|
+
### Optional: Filename
|
203
|
+
```ruby
|
204
|
+
Dato::Client.new.uploads.create_from_url('https://picsum.photos/seed/picsum/200/300', filename: 'test.png')
|
205
|
+
Dato::Client.new.uploads.create_from_file(file.path, filename: 'test.png')
|
206
|
+
|
207
|
+
```
|
208
|
+
|
171
209
|
## Configuration
|
172
210
|
|
173
211
|
The following options are available:
|
@@ -232,7 +270,7 @@ You can take advantage of the publish mechanism of Dato CMS to expire the cache.
|
|
232
270
|
|
233
271
|
## Development
|
234
272
|
|
235
|
-
After checking out the repo, run `
|
273
|
+
After checking out the repo, run `bundle install` to install dependencies.
|
236
274
|
|
237
275
|
You can now clone the dato-rails project
|
238
276
|
|
@@ -245,7 +283,7 @@ to consume data from your project.
|
|
245
283
|
|
246
284
|
Then, run `bundle exec rspec` to run the tests.
|
247
285
|
|
248
|
-
You can also run `
|
286
|
+
You can also run `rails console` for an interactive prompt that will allow you to experiment.
|
249
287
|
|
250
288
|
To install this gem onto your local machine, run `bundle exec rake install`.
|
251
289
|
To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`,
|
data/Rakefile
CHANGED
@@ -1,9 +1,11 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
<figure class="dato-cms-figure">
|
2
|
+
<%= content_tag generated_tag, class: "dato-cms-#{@node.type}" do %>
|
3
|
+
<% @node.children&.each do |node| %>
|
4
|
+
<%= render_node(node) %>
|
5
|
+
<% end %>
|
4
6
|
<% end %>
|
5
|
-
<% end %>
|
6
7
|
|
7
|
-
<
|
8
|
-
|
9
|
-
</
|
8
|
+
<figcaption class="dato-cms-attribution">
|
9
|
+
<%= @node.attribution %>
|
10
|
+
</figcaption>
|
11
|
+
</figure>
|
@@ -1,6 +1,6 @@
|
|
1
|
-
<%= content_tag generated_tag, class: "dato-cms-#{@node.type}" do
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
<% end
|
1
|
+
<%= content_tag generated_tag, class: "dato-cms-#{@node.type}" do -%>
|
2
|
+
<%= @node.value -%>
|
3
|
+
<% @node.children&.each do |node| -%>
|
4
|
+
<%= render_node(node) -%>
|
5
|
+
<% end -%>
|
6
|
+
<% end -%>
|
@@ -1,5 +1,5 @@
|
|
1
|
-
|
2
|
-
<% @node.children&.each do |node|
|
3
|
-
|
4
|
-
<% end
|
5
|
-
|
1
|
+
<%= tag.a(**link_attributes) do -%>
|
2
|
+
<% @node.children&.each do |node| -%>
|
3
|
+
<%= render_node(node) -%>
|
4
|
+
<% end -%>
|
5
|
+
<% end -%>
|
data/app/components/dato/link.rb
CHANGED
@@ -8,4 +8,19 @@ class Dato::Link < Dato::DastNode
|
|
8
8
|
def generated_tag
|
9
9
|
"a"
|
10
10
|
end
|
11
|
+
|
12
|
+
def link_attributes
|
13
|
+
attr = {
|
14
|
+
"href" => @node.url,
|
15
|
+
"class" => "dato-cms-#{@node.type}",
|
16
|
+
}
|
17
|
+
%w[rel target].each { |type| attr[type] = extract_meta(type) }
|
18
|
+
attr
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def extract_meta(type)
|
24
|
+
@node.meta&.find { |m| m.id == type }&.value
|
25
|
+
end
|
11
26
|
end
|
@@ -1,5 +1,5 @@
|
|
1
|
-
<%= conditional_tag 'code', code_span?, class: "dato-cms-span-wrapper" do
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
<% end
|
1
|
+
<%= conditional_tag 'code', code_span?, class: "dato-cms-span-wrapper" do -%>
|
2
|
+
<%= content_tag 'span', class: "dato-cms-span", style: styles do -%>
|
3
|
+
<%= @node.value -%>
|
4
|
+
<% end -%>
|
5
|
+
<% end -%>
|
data/lib/dato/client.rb
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
module Dato
|
2
2
|
class Client
|
3
3
|
delegate :live!, :execute!, :execute, to: :@gql
|
4
|
-
attr_reader :items, :gql
|
4
|
+
attr_reader :items, :uploads, :gql
|
5
5
|
|
6
6
|
def initialize(api_token = Dato::Config.api_token, validate_query: false, preview: false, live: false)
|
7
7
|
@api_token = api_token
|
8
8
|
@gql = Dato::Gql.new(api_token, validate_query, preview, live)
|
9
9
|
@items = Dato::Items.new(api_token)
|
10
|
+
@uploads = Dato::Uploads.new(api_token)
|
10
11
|
end
|
11
12
|
end
|
12
13
|
end
|
data/lib/dato/uploads.rb
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Dato
|
4
|
+
class Uploads
|
5
|
+
BASE_ITEM_URL = "https://site-api.datocms.com/upload-requests"
|
6
|
+
|
7
|
+
def initialize(api_token)
|
8
|
+
@auth_header = "Bearer #{api_token}"
|
9
|
+
|
10
|
+
headers = {
|
11
|
+
"Authorization" => @auth_header,
|
12
|
+
"Accept" => "application/json",
|
13
|
+
"X-Api-Version" => 3
|
14
|
+
}
|
15
|
+
@http_headers = HTTP.headers(headers)
|
16
|
+
end
|
17
|
+
|
18
|
+
def create_from_url(url, filename: nil, attributes: nil, meta: nil)
|
19
|
+
file = download_file_from_url(url)
|
20
|
+
result = create_from_file(file.path, filename:, attributes:, meta:)
|
21
|
+
file.delete
|
22
|
+
|
23
|
+
result
|
24
|
+
end
|
25
|
+
|
26
|
+
def create_from_file(path_to_file, filename: nil, attributes: nil, meta: nil)
|
27
|
+
request = request_file_upload_permission(filename || path_to_file)
|
28
|
+
upload_url = request[:url]
|
29
|
+
upload_id = request[:id]
|
30
|
+
|
31
|
+
upload_file_to_bucket(url: upload_url, path: path_to_file)
|
32
|
+
|
33
|
+
job_id = upload_file_to_dato(upload_id:, attributes:)
|
34
|
+
upload_id = retrieve_job_result(job_id).parse["data"]["attributes"]["payload"]["data"]["id"]
|
35
|
+
|
36
|
+
{upload_id:}
|
37
|
+
end
|
38
|
+
|
39
|
+
def retrieve_job_result(job_id)
|
40
|
+
headers = {
|
41
|
+
"X-Api-Version" => 3,
|
42
|
+
"Authorization" => @auth_header,
|
43
|
+
"Accept" => "application/json",
|
44
|
+
"Content-Type" => "application/vnd.api+json"
|
45
|
+
}
|
46
|
+
|
47
|
+
headers = HTTP.headers(headers)
|
48
|
+
|
49
|
+
headers.get("https://site-api.datocms.com/job-results/#{job_id}")
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def download_file_from_url(url)
|
55
|
+
uri = URI.parse(url)
|
56
|
+
file_type = File.extname(uri.path)
|
57
|
+
file_contents = Net::HTTP.get(uri)
|
58
|
+
temp_file = Tempfile.new(["downloaded_file", file_type])
|
59
|
+
temp_file.binmode
|
60
|
+
temp_file.write(file_contents)
|
61
|
+
temp_file.rewind
|
62
|
+
temp_file.close
|
63
|
+
|
64
|
+
temp_file
|
65
|
+
end
|
66
|
+
|
67
|
+
def request_file_upload_permission(filename)
|
68
|
+
data = {
|
69
|
+
type: "upload_request",
|
70
|
+
attributes: {filename:}
|
71
|
+
}
|
72
|
+
|
73
|
+
response = @http_headers.post(BASE_ITEM_URL, json: {data:})
|
74
|
+
response = response.parse["data"]
|
75
|
+
|
76
|
+
{
|
77
|
+
url: response["attributes"]["url"],
|
78
|
+
id: response["id"]
|
79
|
+
}
|
80
|
+
end
|
81
|
+
|
82
|
+
def upload_file_to_bucket(url:, path:)
|
83
|
+
s3_headers = HTTP.headers({"Content-Type" => MimeMagic.by_magic(File.open(path))&.type || "application/octet-stream"})
|
84
|
+
s3_headers.put(url, body: File.read(path))
|
85
|
+
end
|
86
|
+
|
87
|
+
def upload_file_to_dato(upload_id:, attributes: nil)
|
88
|
+
attributes ||= {}
|
89
|
+
|
90
|
+
headers = {
|
91
|
+
"Content-Type" => "application/vnd.api+json",
|
92
|
+
"X-Api-Version" => 3,
|
93
|
+
"Authorization" => @auth_header,
|
94
|
+
"Accept" => "application/json"
|
95
|
+
}
|
96
|
+
headers = HTTP.headers(headers)
|
97
|
+
|
98
|
+
data = {
|
99
|
+
type: "upload",
|
100
|
+
attributes: {
|
101
|
+
path: upload_id,
|
102
|
+
**attributes
|
103
|
+
}
|
104
|
+
}
|
105
|
+
|
106
|
+
response = headers.post("https://site-api.datocms.com/uploads", json: {data:})
|
107
|
+
|
108
|
+
response.parse["data"]["id"]
|
109
|
+
end
|
110
|
+
end
|
111
|
+
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.7.
|
4
|
+
version: 0.7.4
|
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-08-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec-rails
|
@@ -216,6 +216,7 @@ files:
|
|
216
216
|
- lib/dato/fragments/responsive_image.rb
|
217
217
|
- lib/dato/gql.rb
|
218
218
|
- lib/dato/items.rb
|
219
|
+
- lib/dato/uploads.rb
|
219
220
|
- lib/dato/version.rb
|
220
221
|
- lib/tasks/dato/rails_tasks.rake
|
221
222
|
homepage: https://github.com/renuo/dato-rails
|
@@ -239,7 +240,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
239
240
|
- !ruby/object:Gem::Version
|
240
241
|
version: '0'
|
241
242
|
requirements: []
|
242
|
-
rubygems_version: 3.
|
243
|
+
rubygems_version: 3.3.26
|
243
244
|
signing_key:
|
244
245
|
specification_version: 4
|
245
246
|
summary: Use Dato CMS in your Rails application.
|