lasagna 0.0.2 → 0.1.2
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 +31 -2
- 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: 7b36469f0ed53c726b0bb62ce30f92bb56da147b
|
4
|
+
data.tar.gz: 89a2cbed2489c66f7a7ba283e4135f218697f806
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bde55854f41105b595ab44f3f35f45afb8b8eebbb02df898f93b845110b7c858d80447c4366272cdfb7221c14eac1b445833e32ae9853241f74538799e5425d3
|
7
|
+
data.tar.gz: 7c87a472bf45cb9cc61fe62f38b434771f92f9b04f3e5ef2a09eb056d9ba1f52aa7c5260b0707400f413f7192da25f035433bc0b63835a72fa2840c09f839e21
|
data/lib/lasagna.rb
CHANGED
@@ -1,5 +1,34 @@
|
|
1
1
|
class Lasagna
|
2
|
-
def self.
|
3
|
-
|
2
|
+
def self.parse(json)
|
3
|
+
if json.nil? || json['data'].nil?
|
4
|
+
return {}
|
5
|
+
end
|
6
|
+
|
7
|
+
inclusion = parse_inclusion json
|
8
|
+
parsed = []
|
9
|
+
|
10
|
+
json['data'].each do |item|
|
11
|
+
row = item['attributes'].transform_keys { |key| key.to_s.underscore }
|
12
|
+
item['relationships'].each do |key, r|
|
13
|
+
type = r['data']['type'].underscore
|
14
|
+
id = r['data']['id']
|
15
|
+
row[key] = inclusion[type] && inclusion[type][id]
|
16
|
+
end
|
17
|
+
parsed.push row
|
18
|
+
end
|
19
|
+
parsed
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def self.parse_inclusion(json)
|
25
|
+
inclusion = {}
|
26
|
+
included = json['included'] || []
|
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 }
|
31
|
+
end
|
32
|
+
inclusion
|
4
33
|
end
|
5
34
|
end
|