dato 0.6.12 → 0.6.14

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '0888e8962c3b18b01b3040c781dcb73288d94f4ae68b6b8e031569b26bdc979a'
4
- data.tar.gz: e3dfd7de8de4b2829ef561c7accaadb27230792626152f4a605d02e015c868e4
3
+ metadata.gz: 95b2f3c8bcf22fedf8b578ae80e935c7992a325587592e32e6d8de0eb42d2723
4
+ data.tar.gz: f0d31b23cf1a1c78a1d229601f88905e6a3e3d1097954d722d7454a402d2e645
5
5
  SHA512:
6
- metadata.gz: be30d00c30de4a9e5f2855c19944c2bdc990024eea1a1e669973bbd332a27f47afca4d96198501501be9fd9ca1afdfa54f04cc2c8971c2fc75b174bbbb4efdfc
7
- data.tar.gz: 7d2e9f18cc0b9f9e2a064d3234adee067a304654ab842e5e0dc4c39bd47545b16db7b1285eb952380cf6db9982c0c75307ff99599536147aab3123c90f451db0
6
+ metadata.gz: 5e08e8c062c574e18fb638ed415ab63752230c41a30472ad9c69b82d1d8d8f5eebf905a8a511e0996531ce97c58d39228b9a574e267ff357d2cb8cf64e3f0c17
7
+ data.tar.gz: 58b5b1c0dc0c0638b6b83e86238eb4b465db65950e4fb6866959b4c16786513a370edb62aa7f2c9f23f26ae2da897aa9743379d475d6b180adff11cb98f26bd5
@@ -1,7 +1,14 @@
1
1
  # frozen_string_literal: true
2
+ require 'dato/json_schema_relationships'
2
3
 
3
4
  module Dato
4
5
  class JsonApiDeserializer
6
+ attr_reader :link
7
+
8
+ def initialize(link)
9
+ @link = link
10
+ end
11
+
5
12
  def deserialize(data)
6
13
  return nil unless data
7
14
 
@@ -20,21 +27,37 @@ module Dato
20
27
  result[:meta] = data[:meta] if data[:meta]
21
28
  result.merge!(data[:attributes]) if data[:attributes]
22
29
 
23
- relationships = data.delete(:relationships)
24
-
25
- if relationships
26
- relationships.each do |key, handle|
27
- handle_data = handle['data']
28
- value = if handle_data.is_a? Array
29
- handle_data.map { |ref| ref['id'] }
30
- elsif handle_data.is_a? Hash
31
- handle_data[:id]
32
- end
33
- result[key] = value
30
+ if data[:relationships]
31
+ relationships.each do |relationship, meta|
32
+ if data[:relationships][relationship]
33
+ rel_data = data[:relationships][relationship][:data]
34
+
35
+ result[relationship] = if meta[:types].length > 1
36
+ rel_data
37
+ else
38
+ if !rel_data
39
+ nil
40
+ elsif meta[:collection]
41
+ rel_data.map { |ref| ref[:id] }
42
+ else
43
+ rel_data[:id]
44
+ end
45
+ end
46
+ end
34
47
  end
35
48
  end
36
49
 
37
50
  result.with_indifferent_access
38
51
  end
52
+
53
+ def relationships
54
+ @relationships ||= JsonSchemaRelationships.new(link_relationships).relationships
55
+ end
56
+
57
+ def link_relationships
58
+ if link.target_schema
59
+ link.target_schema.properties['data'].properties['relationships']
60
+ end
61
+ end
39
62
  end
40
63
  end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+ require 'dato/json_schema_relationships'
2
3
 
3
4
  module Dato
4
5
  class JsonApiSerializer
@@ -51,16 +52,31 @@ module Dato
51
52
  value = resource[relationship]
52
53
 
53
54
  data = if value
54
- if meta[:collection]
55
- value.map do |id|
56
- { type: meta[:type], id: id.to_s }
55
+ if meta[:types].length > 1
56
+ if meta[:collection]
57
+ value.map(&:symbolize_keys)
58
+ else
59
+ value.symbolize_keys
57
60
  end
58
61
  else
59
- { type: meta[:type], id: value.to_s }
62
+ type = meta[:types].first
63
+ if meta[:collection]
64
+ value.map do |id|
65
+ {
66
+ type: type,
67
+ id: id.to_s
68
+ }
69
+ end
70
+ else
71
+ {
72
+ type: type,
73
+ id: value.to_s
74
+ }
75
+ end
60
76
  end
61
77
  end
62
- result[relationship] = { data: data }
63
78
 
79
+ result[relationship] = { data: data }
64
80
  elsif required_relationships.include?(relationship)
65
81
  throw "Required attribute: #{relationship}"
66
82
  end
@@ -76,6 +92,7 @@ module Dato
76
92
  id
77
93
  created_at
78
94
  updated_at
95
+ creator
79
96
  ]
80
97
  end
81
98
 
@@ -89,42 +106,10 @@ module Dato
89
106
  end
90
107
 
91
108
  def relationships
92
- if type == 'item'
93
- if link.rel == :create
94
- return { item_type: { collection: false, type: 'item_type' } }
95
- else
96
- {}
97
- end
98
- end
99
-
100
- return {} unless link_relationships
101
-
102
- link_relationships.properties.each_with_object({}) do |(relationship, schema), acc|
103
- is_collection = schema.properties['data'].type.first == 'array'
104
-
105
- definition = if is_collection
106
- schema.properties['data'].items
107
- elsif schema.properties['data'].type.first == 'object'
108
- schema.properties['data']
109
- else
110
- schema.properties['data'].any_of.find do |option|
111
- option.type.first == 'object'
112
- end
113
- end
114
-
115
- type = definition.properties['type']
116
- .pattern.source.gsub(/(^\^|\$$)/, '')
117
-
118
- acc[relationship.to_sym] = {
119
- collection: is_collection,
120
- type: type
121
- }
122
- end
109
+ @relationships ||= JsonSchemaRelationships.new(link_relationships).relationships
123
110
  end
124
111
 
125
112
  def required_relationships
126
- return %i[item_type] if type == 'item'
127
-
128
113
  (link_relationships.required || []).map(&:to_sym)
129
114
  end
130
115
 
@@ -0,0 +1,38 @@
1
+ module Dato
2
+ class JsonSchemaRelationships
3
+ attr_reader :link
4
+
5
+ def initialize(link)
6
+ @link = link
7
+ end
8
+
9
+ def relationships
10
+ return {} unless link
11
+
12
+ link.properties.each_with_object({}) do |(relationship, schema), acc|
13
+ is_collection = schema.properties['data'].type.first == 'array'
14
+
15
+ types = if is_collection
16
+ [type(schema.properties['data'].items)]
17
+ elsif schema.properties['data'].type.first == 'object'
18
+ [type(schema.properties['data'])]
19
+ else
20
+ schema.properties['data'].any_of.map do |option|
21
+ type(option)
22
+ end.compact
23
+ end
24
+
25
+ acc[relationship.to_sym] = {
26
+ collection: is_collection,
27
+ types: types
28
+ }
29
+ end
30
+ end
31
+
32
+ def type(definition)
33
+ if definition.properties['type']
34
+ definition.properties['type'].pattern.source.gsub(/(^\^|\$$)/, '')
35
+ end
36
+ end
37
+ end
38
+ end
@@ -95,7 +95,7 @@ module Dato
95
95
  end
96
96
 
97
97
  if options.fetch(:deserialize_response, true)
98
- JsonApiDeserializer.new.deserialize(response)
98
+ JsonApiDeserializer.new(link).deserialize(response)
99
99
  else
100
100
  response
101
101
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Dato
4
- VERSION = '0.6.12'
4
+ VERSION = '0.6.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.6.12
4
+ version: 0.6.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: 2018-12-07 00:00:00.000000000 Z
11
+ date: 2018-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -407,6 +407,7 @@ files:
407
407
  - lib/dato/dump/ssg_detector.rb
408
408
  - lib/dato/json_api_deserializer.rb
409
409
  - lib/dato/json_api_serializer.rb
410
+ - lib/dato/json_schema_relationships.rb
410
411
  - lib/dato/local/entities_repo.rb
411
412
  - lib/dato/local/field_type/boolean.rb
412
413
  - lib/dato/local/field_type/color.rb