strum 0.0.29 → 0.0.30

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: 28f83ff1c7afb2e86bc1987cedd18a3b59b619b710a5fed03482d2a15eaa9083
4
- data.tar.gz: 91b8f3f9552004629031c1d46e3cdfa93f60f730d3a162e136203c7bb8ee9219
3
+ metadata.gz: 3cc3410ea8c7896616a0c101f919aec2d30679aef1059f71e9b2b7b22c132fd1
4
+ data.tar.gz: e9d224772544d46180aaa1dd2fb382ee593d8a1af8103a51da5a498a72a43e7b
5
5
  SHA512:
6
- metadata.gz: aa3a04ed892026f25804aad7d6c76d3fc6e52f5162ababf6116ecb6582285824cd1bf3b2a4e095cd505d39de68d9633af373c35af5adf245bca4a52dbe6660bf
7
- data.tar.gz: e29bb7d1480e590b907b5e919d8bca27bea14dea722b39efb7bde464fa06a22d37ab4c76069d7d52e4fd066ae3bdd7aec1004702572ca897b361aefba4fb5cbb
6
+ metadata.gz: 84de9d475b3eaccf53b3568c01b0c869c70bbd02ccff42fe185b1fed5827d062563bd1d72695490b2aca70f8bb3ccce265343dbc90a8b0b0931ba2583dddb34a
7
+ data.tar.gz: 37863393861879b8c6daed953346fced5057cb30971bc5d6344b0776db7baa55887ed4b8942bc84552ee4475fd0e285567890efcdf0ea48edfbca9aa3385f4ee
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- strum (0.0.29)
4
+ strum (0.0.30)
5
5
  dry-inflector (~> 0.2.0)
6
6
  dry-matcher (~> 0.8.2)
7
7
  thor (~> 0.20)
@@ -17,28 +17,30 @@ GEM
17
17
  dry-inflector (0.2.0)
18
18
  dry-matcher (0.8.3)
19
19
  dry-core (>= 0.4.8)
20
- irb (1.1.0)
20
+ io-console (0.5.4)
21
+ irb (1.2.1)
21
22
  reline (>= 0.0.1)
22
23
  jaro_winkler (1.5.4)
23
- parallel (1.19.0)
24
- parser (2.6.5.0)
24
+ parallel (1.19.1)
25
+ parser (2.7.0.2)
25
26
  ast (~> 2.4.0)
26
27
  rainbow (3.0.0)
27
28
  rake (10.5.0)
28
- reline (0.0.7)
29
+ reline (0.1.2)
30
+ io-console (~> 0.5)
29
31
  rspec (3.9.0)
30
32
  rspec-core (~> 3.9.0)
31
33
  rspec-expectations (~> 3.9.0)
32
34
  rspec-mocks (~> 3.9.0)
33
- rspec-core (3.9.0)
34
- rspec-support (~> 3.9.0)
35
+ rspec-core (3.9.1)
36
+ rspec-support (~> 3.9.1)
35
37
  rspec-expectations (3.9.0)
36
38
  diff-lcs (>= 1.2.0, < 2.0)
37
39
  rspec-support (~> 3.9.0)
38
- rspec-mocks (3.9.0)
40
+ rspec-mocks (3.9.1)
39
41
  diff-lcs (>= 1.2.0, < 2.0)
40
42
  rspec-support (~> 3.9.0)
41
- rspec-support (3.9.0)
43
+ rspec-support (3.9.2)
42
44
  rubocop (0.76.0)
43
45
  jaro_winkler (~> 1.5.1)
44
46
  parallel (~> 1.10)
@@ -0,0 +1,131 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry-inflector"
4
+
5
+ module Strum
6
+ # Deserialize JSON-API to hash(s)
7
+ class JsonDeserializer
8
+ include Strum::Service
9
+
10
+ def call
11
+ output(root_data[:data] ? prepare_output : root_errors[:errors])
12
+ end
13
+
14
+ def audit
15
+ clear_output!
16
+ add_error(:root, :data_and_errors_must_not_coexist) unless !root_data[:data] ^ !root_errors[:errors]
17
+ end
18
+
19
+ def root_data
20
+ @root_data ||= deep_transform_keys(input)
21
+ end
22
+
23
+ def root_errors
24
+ @root_errors ||= deep_transform_keys(input)
25
+ end
26
+
27
+ def deep_transform_keys(object)
28
+ keys_to_sym = lambda do |object|
29
+ case object
30
+ when Hash
31
+ object.keys.each do |key|
32
+ value = object.delete(key)
33
+ object[(key.respond_to?(:to_sym) ? inflector.underscore(key).to_sym : key)] = keys_to_sym[value]
34
+ end
35
+ object
36
+ when Array
37
+ object.map! { |e| keys_to_sym[e] }
38
+ else
39
+ object
40
+ end
41
+ end
42
+ merge_relationships = lambda do |object|
43
+ handle_attributes(object)
44
+ end
45
+ result = keys_to_sym >> merge_relationships
46
+ result[object]
47
+ end
48
+
49
+ def merge_relations(hash)
50
+ if hash[:data].is_a?(Array)
51
+ parse_array_relationships(hash)
52
+ else
53
+ relationships = relations(hash)
54
+ included = includes(hash)
55
+ prepare_relationships = lambda { |hash|
56
+ hash.each do |key, value|
57
+ if value.is_a?(Hash)
58
+ hash[key.to_sym] = (value.values_at(:attributes)&.first || value.values_at(:data)&.first)
59
+ end
60
+ end
61
+ }
62
+ if relationships && hash[:data][:attributes]
63
+ hash[:data][:attributes].merge!(prepare_relationships[relationships])
64
+ end
65
+ hash[:data][:attributes].merge!(prepare_includes(included)) if included && hash[:data][:attributes]
66
+ end
67
+ hash
68
+ end
69
+
70
+ def parse_array_relationships(hash)
71
+ prepare_nested = lambda { |hash|
72
+ hash.each do |key, value|
73
+ hash[key.to_sym] = value.values_at(:data)&.first if value.is_a?(Hash)
74
+ end
75
+ }
76
+ hash[:data].each do |element|
77
+ relationships, included = element.values_at(:relationships, :included)
78
+ element[:attributes].merge!(prepare_nested[relationships]) if relationships
79
+ element[:attributes].merge!(prepare_nested[included]) if included
80
+ end
81
+ end
82
+
83
+ def relations(hash)
84
+ case hash[:data]
85
+ when Array
86
+ hash[:data].map { |i| i[:relationships] }
87
+ when Hash
88
+ hash[:data][:relationships]
89
+ else
90
+ hash[:data]
91
+ end
92
+ end
93
+
94
+ def includes(hash)
95
+ hash[:included]
96
+ end
97
+
98
+ def prepare_includes(includes)
99
+ includes.each_with_object({}) do |h, e|
100
+ e[h[:type].to_sym] ||= []
101
+ e[h[:type].to_sym] << h[:attributes]
102
+ end
103
+ end
104
+
105
+ def handle_attributes(object)
106
+ case object
107
+ when Array
108
+ object.map { |i| merge_relations(i) }
109
+ when Hash
110
+ merge_relations(object)
111
+ else
112
+ object
113
+ end
114
+ end
115
+
116
+ def prepare_output
117
+ case root_data
118
+ when Array
119
+ root_data.map { |i| i[:data][:attributes] }
120
+ when Hash
121
+ root_data[:data].is_a?(Array) ? root_data[:data].map { |i| i[:attributes] } : root_data[:data][:attributes]
122
+ else
123
+ root_data
124
+ end
125
+ end
126
+
127
+ def inflector
128
+ @inflector = Dry::Inflector.new
129
+ end
130
+ end
131
+ end
data/lib/strum/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Strum
4
- VERSION = "0.0.29"
4
+ VERSION = "0.0.30"
5
5
  end
data/lib/strum.rb CHANGED
@@ -3,6 +3,8 @@
3
3
  require "strum/version"
4
4
  require "strum/service"
5
5
  require "strum/chain"
6
+ require "strum/json_serializer"
7
+ require "strum/json_deserializer"
6
8
 
7
9
  module Strum
8
10
  class Error < StandardError; end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.29
4
+ version: 0.0.30
5
5
  platform: ruby
6
6
  authors:
7
7
  - Serhiy Nazarov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-14 00:00:00.000000000 Z
11
+ date: 2020-01-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-inflector
@@ -156,6 +156,7 @@ files:
156
156
  - lib/strum/commands/resource/scaffold.rb
157
157
  - lib/strum/commands/resource/serializer.rb
158
158
  - lib/strum/commands/resource/service.rb
159
+ - lib/strum/json_deserializer.rb
159
160
  - lib/strum/json_serializer.rb
160
161
  - lib/strum/serializer_name.rb
161
162
  - lib/strum/service.rb