Class: Her::Middleware::JsonApiParser

Inherits:
ParseJSON
  • Object
show all
Defined in:
lib/her/middleware/json_api_parser.rb

Overview

This middleware requires the resource/collection data to be contained in the ‘data` key of the JSON object

Instance Method Summary collapse

Methods inherited from ParseJSON

#assert_response_ok

Instance Method Details

#build_relationships(resource, included) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/her/middleware/json_api_parser.rb', line 30

def build_relationships(resource, included)
  relationships = resource.fetch(:relationships, {})
  {}.tap do |built|
    relationships.each do |rel_name, linkage|
      if linkage_data = linkage.fetch(:data, {})
        built_relationship = if linkage_data.is_a? Array
          linkage_data.map { |l| included.detect { |i| i && i.values_at(:id, :type) == l.values_at(:id, :type) } }
        else
          included.detect { |i| i && i.values_at(:id, :type) == linkage_data.values_at(:id, :type) }
        end

        built[rel_name] = built_relationship if built_relationship
      end
    end
  end
end