Class: Yaks::Reader::JsonAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/yaks/reader/json_api.rb

Instance Method Summary collapse

Instance Method Details

#call(parsed_json, _env = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/yaks/reader/json_api.rb', line 4

def call(parsed_json, _env = {})
  included = parsed_json['included'].nil? ? {} : parsed_json['included'].dup

  if parsed_json['data'].is_a?(Array)
    CollectionResource.new(
      attributes: parsed_json['meta'].nil? ? nil : {meta: parsed_json['meta']},
      members: parsed_json['data'].map { |data| call('data' => data, 'included' => included) }
    )
  else
    attributes = parsed_json['data'].dup
    links = attributes.delete('links') || {}
    relationships = attributes.delete('relationships') || {}
    type = attributes.delete('type')
    attributes.merge!(attributes.delete('attributes') || {})

    embedded   = convert_embedded(Hash[relationships], included)
    links      = convert_links(Hash[links])

    Resource.new(
      type: Util.singularize(type),
      attributes: Util.symbolize_keys(attributes),
      subresources: embedded,
      links: links
    )
  end
end

#convert_embedded(relationships, included) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/yaks/reader/json_api.rb', line 31

def convert_embedded(relationships, included)
  relationships.flat_map do |rel, relationship|
    # A Link doesn't have to contain a `data` member.
    # It can contain URLs instead, or as well, but we are only worried about *embedded* links here.
    data = relationship['data']
    # Resource data MUST be represented as one of the following:
    #
    # * `null` for empty to-one relationships.
    # * a "resource identifier object" for non-empty to-one relationships.
    # * an empty array ([]) for empty to-many relationships.
    # * an array of resource identifier objects for non-empty to-many relationships.
    if data.nil?
      NullResource.new(rels: [rel])
    elsif data.is_a? Array
      if data.empty?
        NullResource.new(collection: true, rels: [rel])
      else
        CollectionResource.new(
          members: data.map { |link|
            data = included.find{ |item| (item['id'] == link['id']) && (item['type'] == link['type']) }
            call('data' => data, 'included' => included)
          },
          rels: [rel]
        )
      end
    else
      data = included.find{ |item| (item['id'] == data['id']) && (item['type'] == data['type']) }
      call('data' => data, 'included' => included).with(rels: [rel])
    end
  end.compact
end


63
64
65
66
67
# File 'lib/yaks/reader/json_api.rb', line 63

def convert_links(links)
  links.map do |rel, link|
    Resource::Link.new(rel: rel.to_sym, uri: link)
  end
end