lasagna 0.1.2 → 0.2.0
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 +4 -4
- data/lib/lasagna.rb +58 -24
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d2b9af461576f4ea457b121777d424bf8744d24f
|
4
|
+
data.tar.gz: 980d405a0b0a152e2933cebb9e17a7ee45d0332e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d296dc36761d649a2734b068fcdd394837c068207cdfa4fd3bf8cbf390183407f0f33d88e6e433ea678f2dcbe278cb599aa1e15db1dc8d822b644e2bfdbd737e
|
7
|
+
data.tar.gz: 7fb57963b0bbbfdf52a4587f3e025ded18f240b92bf423fb3da94287e1c3ad61ba5d7ffad30898f86d72b3280ee44173bbe8375784df993cf8b88cde00e8fef3
|
data/lib/lasagna.rb
CHANGED
@@ -1,34 +1,68 @@
|
|
1
1
|
class Lasagna
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
2
|
+
DEFAULT_OPTIONS = { include_ids: true,
|
3
|
+
include_types: true,
|
4
|
+
transform_keys: nil } # transform_keys is not supported yet
|
5
|
+
|
6
|
+
class << self
|
7
|
+
# Parses jsonapi into a flat readable ruby hash object
|
8
|
+
#
|
9
|
+
# Arguments:
|
10
|
+
# json: (Hash) parsed jsonapi string
|
11
|
+
# options: (Hash) (optional) parsing params
|
12
|
+
#
|
13
|
+
def parse(json, options = {})
|
14
|
+
if json.nil? || json['data'].nil?
|
15
|
+
return {}
|
16
|
+
end
|
17
|
+
|
18
|
+
options = DEFAULT_OPTIONS.merge options
|
19
|
+
inclusion = parse_inclusion(json, options)
|
20
|
+
parsed = []
|
21
|
+
|
22
|
+
json['data'].each do |item|
|
23
|
+
row = {}
|
24
|
+
if options[:include_ids]
|
25
|
+
row['id'] = item['id']
|
26
|
+
end
|
6
27
|
|
7
|
-
|
8
|
-
|
28
|
+
if options[:include_types]
|
29
|
+
row['type'] = item['type']
|
30
|
+
end
|
9
31
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
32
|
+
row = row.merge item['attributes']
|
33
|
+
unless item['relationships'].nil?
|
34
|
+
item['relationships'].each do |key, r|
|
35
|
+
type = r['data']['type']
|
36
|
+
id = r['data']['id']
|
37
|
+
row[key] = inclusion[type] && inclusion[type][id]
|
38
|
+
end
|
39
|
+
end
|
40
|
+
parsed.push row
|
16
41
|
end
|
17
|
-
parsed
|
42
|
+
parsed
|
18
43
|
end
|
19
|
-
parsed
|
20
|
-
end
|
21
44
|
|
22
|
-
|
45
|
+
private
|
46
|
+
|
47
|
+
def parse_inclusion(json, options)
|
48
|
+
inclusion = {}
|
49
|
+
included = json['included'] || []
|
50
|
+
included.each do |include|
|
51
|
+
key = include['type']
|
52
|
+
inclusion[key] = {} if inclusion[key].nil?
|
53
|
+
inc = {}
|
54
|
+
|
55
|
+
if options[:include_ids]
|
56
|
+
inc['id'] = include['id']
|
57
|
+
end
|
58
|
+
if options[:include_types]
|
59
|
+
inc['type'] = include['type']
|
60
|
+
end
|
23
61
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
included.each do |include|
|
28
|
-
key = include['type'].underscore
|
29
|
-
inclusion[key] = {} if inclusion[key].nil?
|
30
|
-
inclusion[key][include['id']] = include['attributes'].transform_keys { |key| key.to_s.underscore }
|
62
|
+
inclusion[key][include['id']] = inc.merge include['attributes']
|
63
|
+
end
|
64
|
+
inclusion
|
31
65
|
end
|
32
|
-
inclusion
|
33
66
|
end
|
67
|
+
|
34
68
|
end
|