dato-rails 0.7.3 → 0.7.4

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: e08eaabd558518c83178db908c5d4c0e7829f5d28241b05a9eb3c9ea3449d84d
4
- data.tar.gz: 22173dc5421c8c1fbd2fc3b844eebdfcc1876dbfd04f9c04c573878ca1694efe
3
+ metadata.gz: cd0388aa8f2f5230feb9b58e501d0d2b32d9a276eccf4029864061ea4fb42745
4
+ data.tar.gz: c084383123cfa44fe5834c8eb61e247d8b5482f4ce983bd9f712044e36fe1f5e
5
5
  SHA512:
6
- metadata.gz: e0109bd3b767231c28917403f0ca96e9a207dbc9fb593cb6a41c83dc853c33ef58018a5a8cd643382c38ed7f85c8c5a817d3f4cb6d73570f1434fd5ec49fdcaa
7
- data.tar.gz: 5c6b4b06e93b9a79379c7e70fecb578518090047aeb8afa869e08c39275842f158503ff4ed4906d06cccdf144947168d0310d28247b4f626bc4bcadc019ad315
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:
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
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Dato
2
- VERSION = "0.7.3"
2
+ VERSION = "0.7.4"
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.7.3
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-07-14 00:00:00.000000000 Z
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.4.10
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.