dato 0.7.7 → 0.7.14

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: 93c35eede758af97a20a62da18105ecf526b0727efde0e6da8e9785da0b3fb06
4
- data.tar.gz: 8884b702f26c094d226fcb33e6a14bb6316a06a6a434fb74f06cac9c03f495f0
3
+ metadata.gz: ffe0429654722ca0165930dc29c1a0350bb386cd73b6c824d2d43cc0af8bb13f
4
+ data.tar.gz: fc124f52acef1d95d507dce6a9c45fa7556b9c5a018b9f630fbd688282015eb7
5
5
  SHA512:
6
- metadata.gz: dfd86b0dd620ea5585a05d7aea34dcf6ca399436e9a532b8c9de096e16b8f43153544df8e6753b1f9da3e55275e004fd55c75b6991d7abd987dbe8acdc11d9db
7
- data.tar.gz: 799596a6912acc9fb99bab989fb1a57deefc7d04d9a1059808eb06e431c42954ba132cdce8146b97286513d20a353aa501b258ebf534cc551af53442340f15b8
6
+ metadata.gz: 7981652ff2a86c8f6081bbb3b7c4fa3d148ba2b159009e5528df476f3ee0517f95c98f7bb8f035c0d09e694f562b06d50a3df44910930a478df3676940508f77
7
+ data.tar.gz: 396828391163788c25f77da2d01c576d6c4cbad8993dda40d97b7700cb1896c7426897613dc9cc40c99177d7cdc8846a382d8ca9e2f37f0152abf3ff82636efe
@@ -1,3 +1,50 @@
1
+ # 0.7.13
2
+
3
+ Add option to pass a project's environment:
4
+
5
+ ```ruby
6
+ require "dato"
7
+ client = Dato::Site::Client.new("YOUR-API-KEY", environment: 'sandbox-foobar')
8
+ ```
9
+
10
+ # 0.7.12
11
+
12
+ Introduces `Dato::Utils::BuildModularBlock` class to help creating modular blocks.
13
+
14
+ An example usage can be:
15
+
16
+ ```ruby
17
+ description = [
18
+ Dato::Utils::BuildModularBlock.build(
19
+ item_type: "1235",
20
+ name: "Best dog in the world",
21
+ year: "2020",
22
+ picture: picture
23
+ )
24
+ ]
25
+
26
+ record = client.items.create(
27
+ item_type: "1234", # model ID
28
+ name: "Gigio",
29
+ description: description
30
+ )
31
+ ```
32
+
33
+ Find more info [on the documentation](https://www.datocms.com/docs/content-management-api/resources/item/create).
34
+
35
+ # 0.7.11 (not released)
36
+
37
+ * Updated specs cassettes after `appeareance -> appearance` typo fix
38
+ * Some style changes
39
+
40
+ # 0.7.10
41
+
42
+ * Fixed SEO title retrieval. Now it fallbacks to default SEO title is item title is blank
43
+
44
+ # 0.7.9
45
+
46
+ * Added new attributes to uploads
47
+
1
48
  # 0.7.0
2
49
 
3
50
  * Real-time events are now much more granular and the gem avoids downloading all the content every time a change occurs
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Cantiere Creativo SRL
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/Rakefile CHANGED
@@ -3,5 +3,8 @@
3
3
  require 'bundler/gem_tasks'
4
4
  require 'rspec/core/rake_task'
5
5
 
6
- RSpec::Core::RakeTask.new(:spec)
6
+ RSpec::Core::RakeTask.new(:spec) do |t|
7
+ t.rspec_opts = "-b"
8
+ end
9
+
7
10
  task default: :spec
@@ -30,6 +30,7 @@ Gem::Specification.new do |spec|
30
30
  spec.add_development_dependency 'rubocop'
31
31
  spec.add_development_dependency 'coveralls'
32
32
  spec.add_development_dependency 'pry'
33
+ spec.add_development_dependency 'pry-byebug'
33
34
  spec.add_development_dependency 'front_matter_parser'
34
35
 
35
36
  spec.add_runtime_dependency 'faraday', ['>= 0.9.0']
@@ -37,7 +38,7 @@ Gem::Specification.new do |spec|
37
38
  spec.add_runtime_dependency 'activesupport', ['>= 4.2.7']
38
39
  spec.add_runtime_dependency 'addressable'
39
40
  spec.add_runtime_dependency 'thor'
40
- spec.add_runtime_dependency 'imgix', ['>= 0.3.1']
41
+ spec.add_runtime_dependency 'imgix', ['~> 4']
41
42
  spec.add_runtime_dependency 'toml'
42
43
  spec.add_runtime_dependency 'cacert'
43
44
  spec.add_runtime_dependency 'dotenv'
@@ -8,6 +8,7 @@ require 'dato/local/site'
8
8
  require 'dato/cli'
9
9
  require 'dato/utils/seo_tags_builder'
10
10
  require 'dato/utils/favicon_tags_builder'
11
+ require 'dato/utils/build_modular_block'
11
12
 
12
13
  module Dato
13
14
  end
@@ -20,7 +20,7 @@ module Dato
20
20
  base.extend ClassMethods
21
21
 
22
22
  base.class_eval do
23
- attr_reader :token, :base_url, :schema, :extra_headers
23
+ attr_reader :token, :environment, :base_url, :schema, :extra_headers
24
24
  end
25
25
  end
26
26
 
@@ -29,27 +29,28 @@ module Dato
29
29
  define_method(:initialize) do |token, options = {}|
30
30
  @token = token
31
31
  @base_url = options[:base_url] || "https://#{subdomain}.datocms.com"
32
+ @environment = options[:environment]
32
33
  @extra_headers = options[:extra_headers] || {}
33
34
  end
34
35
 
36
+ # FOR DEV
37
+ # "http://#{subdomain}.lvh.me:3001/docs/#{subdomain}-hyperschema.json"
35
38
  response = Faraday.get(
36
- # FOR DEV
37
- # "http://#{subdomain}.lvh.me:3001/docs/#{subdomain}-hyperschema.json"
38
39
  "https://#{subdomain}.datocms.com/docs/#{subdomain}-hyperschema.json"
39
40
  )
40
41
 
41
42
  schema = JsonSchema.parse!(JSON.parse(response.body))
42
43
  schema.expand_references!
43
44
 
44
- schema.definitions.each do |type, schema|
45
- is_collection = schema.links.select { |x| x.rel === 'instances' }.any?
45
+ schema.definitions.each do |type, obj|
46
+ is_collection = obj.links.select { |x| x.rel == 'instances' }.any?
46
47
  namespace = is_collection ? type.pluralize : type
47
48
 
48
49
  define_method(namespace) do
49
50
  instance_variable_set(
50
51
  "@#{namespace}",
51
52
  instance_variable_get("@#{namespace}") ||
52
- Dato::Repo.new(self, type, schema)
53
+ Dato::Repo.new(self, type, obj)
53
54
  )
54
55
  end
55
56
  end
@@ -76,16 +77,10 @@ module Dato
76
77
  method, absolute_path, body, params = args
77
78
 
78
79
  response = connection.send(method, absolute_path, body) do |c|
79
- if params
80
- c.params = params
81
- end
80
+ c.params = params if params
82
81
  end
83
82
 
84
- if response.body.is_a?(Hash)
85
- response.body.with_indifferent_access
86
- else
87
- nil
88
- end
83
+ response.body.with_indifferent_access if response.body.is_a?(Hash)
89
84
  rescue Faraday::SSLError => e
90
85
  raise e if ENV['SSL_CERT_FILE'] == Cacert.pem
91
86
 
@@ -101,14 +96,14 @@ module Dato
101
96
  sleep(to_wait + 1)
102
97
  request(*args)
103
98
  elsif e.response[:status] == 422 && batch_data_validation?(e.response)
104
- puts "Validating items, waiting 1 second and retrying..."
99
+ puts 'Validating items, waiting 1 second and retrying...'
105
100
  sleep(1)
106
101
  request(*args)
107
102
  else
108
103
  error = ApiError.new(e.response)
109
- puts "===="
104
+ puts '===='
110
105
  puts error.message
111
- puts "===="
106
+ puts '===='
112
107
  raise error
113
108
  end
114
109
  end
@@ -123,12 +118,12 @@ module Dato
123
118
  end
124
119
 
125
120
  return false unless body
126
- return false unless body["data"]
121
+ return false unless body['data']
127
122
 
128
- body["data"].any? do |e|
129
- e["attributes"]["code"] == "BATCH_DATA_VALIDATION_IN_PROGRESS"
123
+ body['data'].any? do |e|
124
+ e['attributes']['code'] == 'BATCH_DATA_VALIDATION_IN_PROGRESS'
130
125
  end
131
- rescue
126
+ rescue StandardError
132
127
  false
133
128
  end
134
129
 
@@ -141,9 +136,13 @@ module Dato
141
136
  'X-Api-Version' => '3'
142
137
  }
143
138
 
139
+ if environment
140
+ default_headers.merge!('X-Environment' => environment)
141
+ end
142
+
144
143
  options = {
145
144
  url: base_url,
146
- headers: default_headers.merge(extra_headers),
145
+ headers: default_headers.merge(extra_headers)
147
146
  }
148
147
 
149
148
  @connection ||= Faraday.new(options) do |c|
@@ -11,8 +11,10 @@ module Dato
11
11
  desc 'dump', 'dumps DatoCMS content into local files'
12
12
  option :config, default: 'dato.config.rb'
13
13
  option :token, default: ENV['DATO_API_TOKEN'], required: true
14
+ option :environment, type: :string, required: false
14
15
  option :preview, default: false, type: :boolean
15
16
  option :watch, default: false, type: :boolean
17
+
16
18
  def dump
17
19
  config_file = File.expand_path(options[:config])
18
20
  watch_mode = options[:watch]
@@ -20,6 +22,7 @@ module Dato
20
22
 
21
23
  client = Dato::Site::Client.new(
22
24
  options[:token],
25
+ environment: options[:environment],
23
26
  extra_headers: {
24
27
  'X-Reason' => 'dump',
25
28
  'X-SSG' => Dump::SsgDetector.new(Dir.pwd).detect
@@ -38,6 +38,10 @@ module Dato
38
38
  @imgix_host = imgix_host
39
39
  end
40
40
 
41
+ def id
42
+ @upload.id
43
+ end
44
+
41
45
  def path
42
46
  @upload.path
43
47
  end
@@ -70,24 +74,143 @@ module Dato
70
74
  @upload.copyright
71
75
  end
72
76
 
77
+ def filename
78
+ @upload.filename
79
+ end
80
+
81
+ def basename
82
+ @upload.basename
83
+ end
84
+
73
85
  def alt
74
- default_metadata = @upload.default_field_metadata.deep_stringify_keys.fetch(I18n.locale.to_s, {})
75
- @alt || default_metadata["alt"]
86
+ default_metadata = @upload.default_field_metadata.deep_stringify_keys
87
+ .fetch(I18n.locale.to_s, {})
88
+ @alt || default_metadata['alt']
76
89
  end
77
90
 
78
91
  def title
79
- default_metadata = @upload.default_field_metadata.deep_stringify_keys.fetch(I18n.locale.to_s, {})
80
- @title || default_metadata["title"]
92
+ default_metadata = @upload.default_field_metadata.deep_stringify_keys
93
+ .fetch(I18n.locale.to_s, {})
94
+ @title || default_metadata['title']
81
95
  end
82
96
 
83
97
  def custom_data
84
- default_metadata = @upload.default_field_metadata.deep_stringify_keys.fetch(I18n.locale.to_s, {})
85
- @custom_data.merge(default_metadata.fetch("custom_data", {}))
98
+ default_metadata = @upload.default_field_metadata.deep_stringify_keys
99
+ .fetch(I18n.locale.to_s, {})
100
+ @custom_data.merge(default_metadata.fetch('custom_data', {}))
101
+ end
102
+
103
+ def tags
104
+ @upload.tags
105
+ end
106
+
107
+ def smart_tags
108
+ @upload.smart_tags
109
+ end
110
+
111
+ def is_image
112
+ @upload.is_image
113
+ end
114
+
115
+ def exif_info
116
+ @upload.exif_info
117
+ end
118
+
119
+ def mime_type
120
+ @upload.mime_type
121
+ end
122
+
123
+ def colors
124
+ @upload.colors.map { |color| Color.parse(color, nil) }
125
+ end
126
+
127
+ def blurhash
128
+ @upload.blurhash
129
+ end
130
+
131
+ class VideoAttributes
132
+ def initialize(upload)
133
+ @upload = upload
134
+ end
135
+
136
+ def mux_playback_id
137
+ @upload.mux_playback_id
138
+ end
139
+
140
+ def frame_rate
141
+ @upload.frame_rate
142
+ end
143
+
144
+ def duration
145
+ @upload.duration
146
+ end
147
+
148
+ def streaming_url
149
+ "https://stream.mux.com/#{@upload.mux_playback_id}.m3u8"
150
+ end
151
+
152
+ def thumbnail_url(format = :jpg)
153
+ if format == :gif
154
+ "https://image.mux.com/#{@upload.mux_playback_id}/animated.gif"
155
+ else
156
+ "https://image.mux.com/#{@upload.mux_playback_id}/thumbnail.#{format}"
157
+ end
158
+ end
159
+
160
+ def mp4_url(options = nil)
161
+ @upload.mux_mp4_highest_res or
162
+ return nil
163
+
164
+ if options && options[:exact_res]
165
+ if options[:exact_res] == :low
166
+ raw_mp4_url("low")
167
+ elsif options[:exact_res] == :medium
168
+ if %w[medium high].include?(@upload.mux_mp4_highest_res)
169
+ raw_mp4_url("medium")
170
+ end
171
+ elsif @upload.mux_mp4_highest_res == :high
172
+ raw_mp4_url("high")
173
+ end
174
+ elsif options && options[:res] == :low
175
+ raw_mp4_url("low")
176
+ elsif options && options[:res] == :medium
177
+ if %w[low medium].include?(@upload.mux_mp4_highest_res)
178
+ raw_mp4_url(@upload.mux_mp4_highest_res)
179
+ else
180
+ raw_mp4_url("medium")
181
+ end
182
+ else
183
+ raw_mp4_url(@upload.mux_mp4_highest_res)
184
+ end
185
+ end
186
+
187
+ def to_hash
188
+ {
189
+ mux_playback_id: mux_playback_id,
190
+ frame_rate: frame_rate,
191
+ duration: duration,
192
+ streaming_url: streaming_url,
193
+ thumbnail_url: thumbnail_url,
194
+ mp4_url: mp4_url,
195
+ }
196
+ end
197
+
198
+ private
199
+
200
+ def raw_mp4_url(res)
201
+ "https://stream.mux.com/#{@upload.mux_playback_id}/#{res}.mp4"
202
+ end
203
+ end
204
+
205
+ def video
206
+ if @upload.mux_playback_id
207
+ VideoAttributes.new(@upload)
208
+ end
86
209
  end
87
210
 
88
211
  def file
89
212
  Imgix::Client.new(
90
- host: @imgix_host,
213
+ domain: @imgix_host,
91
214
  secure: true,
92
215
  include_library_param: false
93
216
  ).path(path)
@@ -97,8 +220,20 @@ module Dato
97
220
  file.to_url(opts)
98
221
  end
99
222
 
223
+ def lqip_data_url(opts = {})
224
+ @imgix_host != "www.datocms-assets.com" and
225
+ raise "#lqip_data_url can only be used with www.datocms-assets.com domain"
226
+
227
+ response = Faraday.get(file.to_url(opts.merge(lqip: "blurhash")))
228
+
229
+ if response.status == 200
230
+ "data:image/jpeg;base64,#{Base64.strict_encode64(response.body)}"
231
+ end
232
+ end
233
+
100
234
  def to_hash(*_args)
101
235
  {
236
+ id: id,
102
237
  format: format,
103
238
  size: size,
104
239
  width: width,
@@ -106,7 +241,18 @@ module Dato
106
241
  alt: alt,
107
242
  title: title,
108
243
  custom_data: custom_data,
109
- url: url
244
+ url: url,
245
+ copyright: copyright,
246
+ tags: tags,
247
+ smart_tags: smart_tags,
248
+ filename: filename,
249
+ basename: basename,
250
+ is_image: is_image,
251
+ exif_info: exif_info,
252
+ mime_type: mime_type,
253
+ colors: colors.map(&:to_hash),
254
+ blurhash: blurhash,
255
+ video: video && video.to_hash
110
256
  }
111
257
  end
112
258
  end
@@ -43,7 +43,7 @@ module Dato
43
43
 
44
44
  def file
45
45
  Imgix::Client.new(
46
- host: @imgix_host,
46
+ domain: @imgix_host,
47
47
  secure: true,
48
48
  include_library_param: false
49
49
  ).path(path)
@@ -43,7 +43,13 @@ module Dato
43
43
 
44
44
  return if pusher && pusher.connected
45
45
 
46
- pusher.subscribe("private-site-#{site_id}")
46
+ channel_name = if client.environment
47
+ "private-site-#{site_id}-environment-#{environment}"
48
+ else
49
+ "private-site-#{site_id}"
50
+ end
51
+
52
+ pusher.subscribe(channel_name)
47
53
 
48
54
  bind_on_site_upsert(&block)
49
55
  bind_on_item_destroy(&block)
@@ -66,7 +72,8 @@ module Dato
66
72
  bind_on("site:upsert", block) do |data|
67
73
  threads = [
68
74
  Thread.new { Thread.current[:output] = site },
69
- Thread.new { Thread.current[:output] = all_items }
75
+ Thread.new { Thread.current[:output] = all_items },
76
+ Thread.new { Thread.current[:output] = all_uploads }
70
77
  ]
71
78
 
72
79
  results = threads.map do |t|
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dato/json_api_serializer'
4
+
5
+ module Dato
6
+ module Utils
7
+ module BuildModularBlock
8
+ def self.build(unserialized_body)
9
+ json_api_serializer = JsonApiSerializer.new('item', nil)
10
+ attributes = json_api_serializer.serialized_attributes(unserialized_body)
11
+
12
+ payload = {
13
+ type: 'item',
14
+ attributes: attributes,
15
+ relationships: {
16
+ item_type: {
17
+ data: {
18
+ id: unserialized_body[:item_type],
19
+ type: 'item_type',
20
+ },
21
+ },
22
+ },
23
+ }
24
+
25
+ payload[:id] = unserialized_body[:id] if unserialized_body[:id]
26
+
27
+ payload
28
+ end
29
+ end
30
+ end
31
+ end
@@ -28,7 +28,7 @@ module Dato
28
28
  fallback_seo_value = fallback_seo &&
29
29
  fallback_seo.send(attribute)
30
30
 
31
- item_seo_value || alternative || fallback_seo_value
31
+ item_seo_value.presence || alternative.presence || fallback_seo_value
32
32
  end
33
33
 
34
34
  def tag(tag_name, attributes)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Dato
4
- VERSION = '0.7.7'
4
+ VERSION = '0.7.14'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dato
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.7
4
+ version: 0.7.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefano Verna
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-10-14 00:00:00.000000000 Z
11
+ date: 2020-09-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -150,6 +150,20 @@ dependencies:
150
150
  - - ">="
151
151
  - !ruby/object:Gem::Version
152
152
  version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: pry-byebug
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
153
167
  - !ruby/object:Gem::Dependency
154
168
  name: front_matter_parser
155
169
  requirement: !ruby/object:Gem::Requirement
@@ -238,16 +252,16 @@ dependencies:
238
252
  name: imgix
239
253
  requirement: !ruby/object:Gem::Requirement
240
254
  requirements:
241
- - - ">="
255
+ - - "~>"
242
256
  - !ruby/object:Gem::Version
243
- version: 0.3.1
257
+ version: '4'
244
258
  type: :runtime
245
259
  prerelease: false
246
260
  version_requirements: !ruby/object:Gem::Requirement
247
261
  requirements:
248
- - - ">="
262
+ - - "~>"
249
263
  - !ruby/object:Gem::Version
250
- version: 0.3.1
264
+ version: '4'
251
265
  - !ruby/object:Gem::Dependency
252
266
  name: toml
253
267
  requirement: !ruby/object:Gem::Requirement
@@ -362,6 +376,7 @@ files:
362
376
  - CHANGELOG.md
363
377
  - CODE_OF_CONDUCT.md
364
378
  - Gemfile
379
+ - LICENSE
365
380
  - LICENSE.txt
366
381
  - README.md
367
382
  - Rakefile
@@ -428,6 +443,7 @@ files:
428
443
  - lib/dato/upload/create_upload_path.rb
429
444
  - lib/dato/upload/file.rb
430
445
  - lib/dato/upload/image.rb
446
+ - lib/dato/utils/build_modular_block.rb
431
447
  - lib/dato/utils/favicon_tags_builder.rb
432
448
  - lib/dato/utils/locale_value.rb
433
449
  - lib/dato/utils/meta_tags/article_modified_time.rb