Class: Lasagna
- Inherits:
-
Object
- Object
- Lasagna
- Defined in:
- lib/lasagna.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
{ include_ids: true, include_types: true, transform_keys: nil }
Class Method Summary collapse
-
.parse(json, options = {}) ⇒ Object
Parses jsonapi into a flat readable ruby hash object.
Class Method Details
.parse(json, options = {}) ⇒ Object
Parses jsonapi into a flat readable ruby hash object
Arguments:
json: (Hash) parsed jsonapi string
options: (Hash) (optional) parsing params
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/lasagna.rb', line 13 def parse(json, = {}) if json.nil? || json['data'].nil? return {} end = DEFAULT_OPTIONS.merge inclusion = parse_inclusion(json, ) parsed = [] json['data'].each do |item| row = {} if [:include_ids] row['id'] = item['id'] end if [:include_types] row['type'] = item['type'] end row = row.merge item['attributes'] unless item['relationships'].nil? item['relationships'].each do |key, r| next if r['data'].nil? type = r['data']['type'] id = r['data']['id'] row[key] = inclusion[type] && inclusion[type][id] end end parsed.push row end parsed end |