dato 0.7.15 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +42 -5
  3. data/.travis.yml +1 -0
  4. data/CHANGELOG.md +4 -0
  5. data/Gemfile +1 -1
  6. data/Rakefile +2 -2
  7. data/TODO.md +28 -0
  8. data/bin/console +3 -3
  9. data/bin/rspec +6 -6
  10. data/dato.gemspec +1 -1
  11. data/exe/dato +3 -3
  12. data/lib/dato.rb +8 -8
  13. data/lib/dato/account/client.rb +2 -2
  14. data/lib/dato/api_client.rb +67 -44
  15. data/lib/dato/api_error.rb +10 -13
  16. data/lib/dato/cli.rb +18 -18
  17. data/lib/dato/dump/dsl/add_to_data_file.rb +1 -1
  18. data/lib/dato/dump/dsl/create_data_file.rb +1 -1
  19. data/lib/dato/dump/dsl/create_post.rb +1 -1
  20. data/lib/dato/dump/dsl/directory.rb +4 -4
  21. data/lib/dato/dump/dsl/root.rb +7 -7
  22. data/lib/dato/dump/format.rb +3 -3
  23. data/lib/dato/dump/format/json.rb +1 -1
  24. data/lib/dato/dump/format/toml.rb +6 -6
  25. data/lib/dato/dump/format/yaml.rb +3 -3
  26. data/lib/dato/dump/operation/add_to_data_file.rb +4 -4
  27. data/lib/dato/dump/operation/create_data_file.rb +3 -3
  28. data/lib/dato/dump/operation/create_post.rb +5 -6
  29. data/lib/dato/dump/operation/directory.rb +1 -1
  30. data/lib/dato/dump/runner.rb +5 -5
  31. data/lib/dato/dump/ssg_detector.rb +18 -20
  32. data/lib/dato/json_api_deserializer.rb +15 -16
  33. data/lib/dato/json_api_serializer.rb +39 -28
  34. data/lib/dato/json_schema_relationships.rb +19 -23
  35. data/lib/dato/json_schema_type.rb +47 -0
  36. data/lib/dato/local/entities_repo.rb +3 -3
  37. data/lib/dato/local/field_type/color.rb +11 -7
  38. data/lib/dato/local/field_type/file.rb +18 -24
  39. data/lib/dato/local/field_type/gallery.rb +1 -1
  40. data/lib/dato/local/field_type/global_seo.rb +4 -7
  41. data/lib/dato/local/field_type/lat_lon.rb +1 -1
  42. data/lib/dato/local/field_type/seo.rb +1 -1
  43. data/lib/dato/local/field_type/structured_text.rb +63 -0
  44. data/lib/dato/local/field_type/theme.rb +2 -2
  45. data/lib/dato/local/field_type/upload_id.rb +5 -5
  46. data/lib/dato/local/field_type/video.rb +9 -15
  47. data/lib/dato/local/item.rb +13 -12
  48. data/lib/dato/local/items_repo.rb +12 -18
  49. data/lib/dato/local/json_api_entity.rb +4 -3
  50. data/lib/dato/local/loader.rb +31 -32
  51. data/lib/dato/local/site.rb +5 -4
  52. data/lib/dato/paginator.rb +4 -4
  53. data/lib/dato/repo.rb +23 -30
  54. data/lib/dato/site/client.rb +5 -5
  55. data/lib/dato/upload/create_upload_path.rb +7 -10
  56. data/lib/dato/upload/file.rb +3 -3
  57. data/lib/dato/upload/image.rb +1 -1
  58. data/lib/dato/utils/build_modular_block.rb +4 -4
  59. data/lib/dato/utils/favicon_tags_builder.rb +10 -10
  60. data/lib/dato/utils/locale_value.rb +1 -1
  61. data/lib/dato/utils/meta_tags/article_modified_time.rb +3 -3
  62. data/lib/dato/utils/meta_tags/article_publisher.rb +2 -2
  63. data/lib/dato/utils/meta_tags/base.rb +6 -6
  64. data/lib/dato/utils/meta_tags/description.rb +4 -4
  65. data/lib/dato/utils/meta_tags/image.rb +4 -5
  66. data/lib/dato/utils/meta_tags/og_locale.rb +2 -2
  67. data/lib/dato/utils/meta_tags/og_site_name.rb +2 -2
  68. data/lib/dato/utils/meta_tags/og_type.rb +3 -3
  69. data/lib/dato/utils/meta_tags/robots.rb +2 -2
  70. data/lib/dato/utils/meta_tags/title.rb +6 -6
  71. data/lib/dato/utils/meta_tags/twitter_card.rb +2 -2
  72. data/lib/dato/utils/meta_tags/twitter_site.rb +2 -2
  73. data/lib/dato/utils/seo_tags_builder.rb +12 -12
  74. data/lib/dato/version.rb +1 -1
  75. metadata +9 -6
@@ -1,13 +1,15 @@
1
1
  # frozen_string_literal: true
2
- require 'dato/json_schema_relationships'
2
+
3
+ require "dato/json_schema_relationships"
4
+ require "dato/json_schema_type"
3
5
 
4
6
  module Dato
5
7
  class JsonApiSerializer
6
8
  attr_reader :link, :type
7
9
 
8
- def initialize(type, link)
10
+ def initialize(type: nil, link: nil)
9
11
  @link = link
10
- @type = type
12
+ @type = type || type_from_schema
11
13
  end
12
14
 
13
15
  def serialize(resource, id = nil)
@@ -16,19 +18,22 @@ module Dato
16
18
 
17
19
  data[:id] = id || resource[:id] if id || resource[:id]
18
20
 
19
- if resource.has_key?(:meta)
20
- resource.delete(:meta)
21
- end
21
+ resource.delete(:meta) if resource.key?(:meta)
22
22
 
23
23
  data[:type] = type
24
- data[:attributes] = serialized_attributes(resource)
25
24
 
26
- serialized_relationships = serialized_relationships(resource)
25
+ if link.schema &&
26
+ link.schema.properties["data"] &&
27
+ link.schema.properties["data"].properties.keys.include?("attributes")
27
28
 
28
- if serialized_relationships
29
- data[:relationships] = serialized_relationships
29
+ serialized_resource_attributes = serialized_attributes(resource)
30
+ data[:attributes] = serialized_resource_attributes
30
31
  end
31
32
 
33
+ serialized_relationships = serialized_relationships(resource)
34
+
35
+ data[:relationships] = serialized_relationships if serialized_relationships
36
+
32
37
  { data: data }
33
38
  end
34
39
 
@@ -61,18 +66,18 @@ module Dato
61
66
  value.symbolize_keys
62
67
  end
63
68
  else
64
- type = meta[:types].first
69
+ meta_type = meta[:types].first
65
70
  if meta[:collection]
66
71
  value.map do |id|
67
72
  {
68
- type: type,
69
- id: id.to_s
73
+ type: meta_type,
74
+ id: id.to_s,
70
75
  }
71
76
  end
72
77
  else
73
78
  {
74
- type: type,
75
- id: value.to_s
79
+ type: meta_type,
80
+ id: value.to_s,
76
81
  }
77
82
  end
78
83
  end
@@ -88,21 +93,23 @@ module Dato
88
93
  end
89
94
 
90
95
  def attributes(resource)
91
- if type == 'item'
92
- return resource.keys.map(&:to_sym) - %i[
93
- item_type
94
- id
95
- created_at
96
- updated_at
97
- creator
98
- ]
96
+ if type == "item"
97
+ return resource.keys.reject do |key|
98
+ %i[
99
+ item_type
100
+ id
101
+ created_at
102
+ updated_at
103
+ creator
104
+ ].include?(key.to_sym)
105
+ end
99
106
  end
100
107
 
101
- link_attributes['properties'].keys.map(&:to_sym)
108
+ link_attributes["properties"].keys.map(&:to_sym)
102
109
  end
103
110
 
104
111
  def required_attributes
105
- return [] if type == 'item'
112
+ return [] if type == "item"
106
113
 
107
114
  (link_attributes.required || []).map(&:to_sym)
108
115
  end
@@ -112,7 +119,7 @@ module Dato
112
119
  end
113
120
 
114
121
  def required_relationships
115
- if link.schema.properties['data'].required.include?("relationships")
122
+ if link.schema.properties["data"].required.include?("relationships")
116
123
  (link_relationships.required || []).map(&:to_sym)
117
124
  else
118
125
  []
@@ -120,11 +127,15 @@ module Dato
120
127
  end
121
128
 
122
129
  def link_attributes
123
- link.schema.properties['data'].properties['attributes']
130
+ link.schema.properties["data"].properties["attributes"]
124
131
  end
125
132
 
126
133
  def link_relationships
127
- link.schema.properties['data'].properties['relationships']
134
+ link.schema.properties["data"].properties["relationships"]
135
+ end
136
+
137
+ def type_from_schema
138
+ Dato::JsonSchemaType.new(link.schema).call
128
139
  end
129
140
  end
130
141
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Dato
2
4
  class JsonSchemaRelationships
3
5
  attr_reader :schema
@@ -7,46 +9,40 @@ module Dato
7
9
  end
8
10
 
9
11
  def relationships
10
- if !schema || !schema.properties['data']
11
- return {}
12
- end
12
+ return {} if !schema || !schema.properties["data"]
13
13
 
14
- entity = if schema.properties['data'].type.first == 'array'
15
- schema.properties['data'].items
14
+ entity = if schema.properties["data"].type.first == "array"
15
+ schema.properties["data"].items
16
16
  else
17
- schema.properties['data']
17
+ schema.properties["data"]
18
18
  end
19
19
 
20
- if !entity || !entity.properties['relationships'] || !entity.properties['relationships']
21
- return {}
22
- end
20
+ return {} if !entity || !entity.properties["relationships"] || !entity.properties["relationships"]
23
21
 
24
- relationships = entity.properties['relationships'].properties
22
+ relationships = entity.properties["relationships"].properties
25
23
 
26
24
  relationships.each_with_object({}) do |(relationship, schema), acc|
27
- is_collection = schema.properties['data'].type.first == 'array'
25
+ is_collection = schema.properties["data"].type.first == "array"
28
26
 
29
27
  types = if is_collection
30
- [type(schema.properties['data'].items)]
31
- elsif schema.properties['data'].type.first == 'object'
32
- [type(schema.properties['data'])]
33
- else
34
- schema.properties['data'].any_of.map do |option|
35
- type(option)
36
- end.compact
37
- end
28
+ [type(schema.properties["data"].items)]
29
+ elsif schema.properties["data"].type.first == "object"
30
+ [type(schema.properties["data"])]
31
+ else
32
+ schema.properties["data"].any_of.map do |option|
33
+ type(option)
34
+ end.compact
35
+ end
38
36
 
39
37
  acc[relationship.to_sym] = {
40
38
  collection: is_collection,
41
- types: types
39
+ types: types,
42
40
  }
43
41
  end
44
42
  end
45
43
 
46
44
  def type(definition)
47
- if definition.properties['type']
48
- definition.properties['type'].pattern.source.gsub(/(^\^|\$$)/, '')
49
- end
45
+ definition.properties["type"].pattern.source.gsub(/(^\^|\$$)/, "") if definition.properties["type"]
50
46
  end
51
47
  end
52
48
  end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dato
4
+ class JsonSchemaType
5
+ attr_reader :schema
6
+
7
+ def initialize(schema)
8
+ @schema = schema
9
+ end
10
+
11
+ def call
12
+ type_property = find_info_for_type_property
13
+
14
+ return nil unless type_property
15
+
16
+ type_property.pattern.to_s.gsub(/(^(\(\?-mix:\^)|(\$\))$)/, "")
17
+ end
18
+
19
+ private
20
+
21
+ def find_info_for_type_property
22
+ entity = find_entity_in_data
23
+
24
+ return nil unless entity
25
+
26
+ entity.properties["type"]
27
+ end
28
+
29
+ def find_entity_in_data
30
+ return nil if !schema || !schema.properties["data"]
31
+
32
+ if schema.properties["data"].type.first == "array"
33
+ return schema.properties["data"].items if schema.properties["data"].items
34
+
35
+ return nil
36
+ end
37
+
38
+ return schema.properties["data"] if schema.properties["data"].type.first == "object"
39
+
40
+ if schema.properties["data"].any_of
41
+ return schema.properties["data"].any_of.reject { |x| x.definitions.type.example == "job" }
42
+ end
43
+
44
+ nil
45
+ end
46
+ end
47
+ end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'dato/local/json_api_entity'
3
+ require "dato/local/json_api_entity"
4
4
 
5
5
  module Dato
6
6
  module Local
@@ -27,8 +27,8 @@ module Dato
27
27
  end
28
28
 
29
29
  def destroy_item_type(id)
30
- entities.fetch('item', {}).delete_if { |_item_id, item| item.item_type.id == id }
31
- entities.fetch('item_type', {}).delete(id)
30
+ entities.fetch("item", {}).delete_if { |_item_id, item| item.item_type.id == id }
31
+ entities.fetch("item_type", {}).delete(id)
32
32
  end
33
33
 
34
34
  def upsert_entities(*payloads)
@@ -4,14 +4,14 @@ module Dato
4
4
  module Local
5
5
  module FieldType
6
6
  class Color
7
- attr_reader :red, :green, :blue, :alpha
7
+ attr_reader :red, :green, :blue
8
8
 
9
9
  def self.parse(value, _repo)
10
10
  value && new(
11
11
  value[:red],
12
12
  value[:green],
13
13
  value[:blue],
14
- value[:alpha]
14
+ value[:alpha],
15
15
  )
16
16
  end
17
17
 
@@ -19,17 +19,21 @@ module Dato
19
19
  @red = red
20
20
  @green = green
21
21
  @blue = blue
22
- @alpha = alpha / 255.0
22
+ @alpha = alpha
23
23
  end
24
24
 
25
25
  def rgb
26
- if alpha == 1.0
26
+ if @alpha == 255
27
27
  "rgb(#{red}, #{green}, #{blue})"
28
28
  else
29
29
  "rgba(#{red}, #{green}, #{blue}, #{alpha})"
30
30
  end
31
31
  end
32
32
 
33
+ def alpha
34
+ @alpha / 255.0
35
+ end
36
+
33
37
  def hex
34
38
  r = red.to_s(16)
35
39
  g = green.to_s(16)
@@ -41,9 +45,9 @@ module Dato
41
45
  b = "0#{b}" if b.length == 1
42
46
  a = "0#{a}" if a.length == 1
43
47
 
44
- hex = '#' + r + g + b
48
+ hex = "##{r}#{g}#{b}"
45
49
 
46
- hex += a if a != 'ff'
50
+ hex += a if a != "ff"
47
51
 
48
52
  hex
49
53
  end
@@ -54,7 +58,7 @@ module Dato
54
58
  green: green,
55
59
  blue: blue,
56
60
  rgb: rgb,
57
- hex: hex
61
+ hex: hex,
58
62
  }
59
63
  end
60
64
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'imgix'
3
+ require "imgix"
4
4
 
5
5
  module Dato
6
6
  module Local
@@ -10,7 +10,7 @@ module Dato
10
10
  if value
11
11
  v = value.with_indifferent_access
12
12
 
13
- upload = repo.entities_repo.find_entity('upload', v[:upload_id])
13
+ upload = repo.entities_repo.find_entity("upload", v[:upload_id])
14
14
 
15
15
  if upload
16
16
  new(
@@ -19,7 +19,7 @@ module Dato
19
19
  v[:title],
20
20
  v[:custom_data],
21
21
  v[:focal_point],
22
- repo.site.entity.imgix_host
22
+ repo.site.entity.imgix_host,
23
23
  )
24
24
  end
25
25
  end
@@ -88,25 +88,25 @@ module Dato
88
88
  def alt
89
89
  default_metadata = @upload.default_field_metadata.deep_stringify_keys
90
90
  .fetch(I18n.locale.to_s, {})
91
- @alt || default_metadata['alt']
91
+ @alt || default_metadata["alt"]
92
92
  end
93
93
 
94
94
  def title
95
95
  default_metadata = @upload.default_field_metadata.deep_stringify_keys
96
96
  .fetch(I18n.locale.to_s, {})
97
- @title || default_metadata['title']
97
+ @title || default_metadata["title"]
98
98
  end
99
99
 
100
100
  def custom_data
101
101
  default_metadata = @upload.default_field_metadata.deep_stringify_keys
102
102
  .fetch(I18n.locale.to_s, {})
103
- @custom_data.merge(default_metadata.fetch('custom_data', {}))
103
+ @custom_data.merge(default_metadata.fetch("custom_data", {}))
104
104
  end
105
105
 
106
106
  def focal_point
107
107
  default_metadata = @upload.default_field_metadata.deep_stringify_keys
108
108
  .fetch(I18n.locale.to_s, {})
109
- @focal_point || default_metadata['focal_point']
109
+ @focal_point || default_metadata["focal_point"]
110
110
  end
111
111
 
112
112
  def tags
@@ -174,9 +174,7 @@ module Dato
174
174
  if options[:exact_res] == :low
175
175
  raw_mp4_url("low")
176
176
  elsif options[:exact_res] == :medium
177
- if %w[medium high].include?(@upload.mux_mp4_highest_res)
178
- raw_mp4_url("medium")
179
- end
177
+ raw_mp4_url("medium") if %w[medium high].include?(@upload.mux_mp4_highest_res)
180
178
  elsif @upload.mux_mp4_highest_res == :high
181
179
  raw_mp4_url("high")
182
180
  end
@@ -212,16 +210,14 @@ module Dato
212
210
  end
213
211
 
214
212
  def video
215
- if @upload.mux_playback_id
216
- VideoAttributes.new(@upload)
217
- end
213
+ VideoAttributes.new(@upload) if @upload.mux_playback_id
218
214
  end
219
215
 
220
216
  def file
221
217
  Imgix::Client.new(
222
218
  domain: @imgix_host,
223
219
  secure: true,
224
- include_library_param: false
220
+ include_library_param: false,
225
221
  ).path(path)
226
222
  end
227
223
 
@@ -229,12 +225,12 @@ module Dato
229
225
  query.deep_stringify_keys!
230
226
 
231
227
  if focal_point &&
232
- query["fit"] == "crop" &&
233
- (query["h"] || query["height"]) &&
234
- (query["w"] || query["width"]) &&
235
- [nil, "focalpoint"].include?(query["crop"]) &&
236
- query["fp-x"].nil? &&
237
- query["fp-y"].nil?
228
+ query["fit"] == "crop" &&
229
+ (query["h"] || query["height"]) &&
230
+ (query["w"] || query["width"]) &&
231
+ [nil, "focalpoint"].include?(query["crop"]) &&
232
+ query["fp-x"].nil? &&
233
+ query["fp-y"].nil?
238
234
 
239
235
  query.merge!(
240
236
  "crop" => "focalpoint",
@@ -252,9 +248,7 @@ module Dato
252
248
 
253
249
  response = Faraday.get(file.to_url(opts.merge(lqip: "blurhash")))
254
250
 
255
- if response.status == 200
256
- "data:image/jpeg;base64,#{Base64.strict_encode64(response.body)}"
257
- end
251
+ "data:image/jpeg;base64,#{Base64.strict_encode64(response.body)}" if response.status == 200
258
252
  end
259
253
 
260
254
  def to_hash(*_args)
@@ -279,7 +273,7 @@ module Dato
279
273
  mime_type: mime_type,
280
274
  colors: colors.map(&:to_hash),
281
275
  blurhash: blurhash,
282
- video: video && video.to_hash
276
+ video: video && video.to_hash,
283
277
  }
284
278
  end
285
279
  end