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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/lasagna.rb +58 -24
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7b36469f0ed53c726b0bb62ce30f92bb56da147b
4
- data.tar.gz: 89a2cbed2489c66f7a7ba283e4135f218697f806
3
+ metadata.gz: d2b9af461576f4ea457b121777d424bf8744d24f
4
+ data.tar.gz: 980d405a0b0a152e2933cebb9e17a7ee45d0332e
5
5
  SHA512:
6
- metadata.gz: bde55854f41105b595ab44f3f35f45afb8b8eebbb02df898f93b845110b7c858d80447c4366272cdfb7221c14eac1b445833e32ae9853241f74538799e5425d3
7
- data.tar.gz: 7c87a472bf45cb9cc61fe62f38b434771f92f9b04f3e5ef2a09eb056d9ba1f52aa7c5260b0707400f413f7192da25f035433bc0b63835a72fa2840c09f839e21
6
+ metadata.gz: d296dc36761d649a2734b068fcdd394837c068207cdfa4fd3bf8cbf390183407f0f33d88e6e433ea678f2dcbe278cb599aa1e15db1dc8d822b644e2bfdbd737e
7
+ data.tar.gz: 7fb57963b0bbbfdf52a4587f3e025ded18f240b92bf423fb3da94287e1c3ad61ba5d7ffad30898f86d72b3280ee44173bbe8375784df993cf8b88cde00e8fef3
data/lib/lasagna.rb CHANGED
@@ -1,34 +1,68 @@
1
1
  class Lasagna
2
- def self.parse(json)
3
- if json.nil? || json['data'].nil?
4
- return {}
5
- end
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
- inclusion = parse_inclusion json
8
- parsed = []
28
+ if options[:include_types]
29
+ row['type'] = item['type']
30
+ end
9
31
 
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]
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.push row
42
+ parsed
18
43
  end
19
- parsed
20
- end
21
44
 
22
- private
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
- 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 }
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lasagna
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergey Vdovareize