Class: JSONAPI::ResourceSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapi/resource_serializer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(primary_resource_klass, options = {}) ⇒ ResourceSerializer

Returns a new instance of ResourceSerializer.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/jsonapi/resource_serializer.rb', line 17

def initialize(primary_resource_klass, options = {})
  @primary_class_name = primary_resource_klass._type
  @fields             = options.fetch(:fields, {})
  @include            = options.fetch(:include, [])
  @include_directives = options[:include_directives]
  @key_formatter      = options.fetch(:key_formatter, JSONAPI.configuration.key_formatter)
  @url_generator      = generate_link_builder(primary_resource_klass, options)
  @always_include_to_one_linkage_data = options.fetch(:always_include_to_one_linkage_data,
                                                      JSONAPI.configuration.always_include_to_one_linkage_data)
  @always_include_to_many_linkage_data = options.fetch(:always_include_to_many_linkage_data,
                                                       JSONAPI.configuration.always_include_to_many_linkage_data)
end

Instance Attribute Details

#url_generatorObject (readonly)

Options can include include:

Purpose: determines which objects will be side loaded with the source objects in a linked section
Example: ['comments','author','comments.tags','author.posts']

fields:

Purpose: determines which fields are serialized for a resource type. This encompasses both attributes and
         relationship ids in the links section for a resource. Fields are global for a resource type.
Example: { people: [:id, :email, :comments], posts: [:id, :title, :author], comments: [:id, :body, :post]}

key_formatter: KeyFormatter class to override the default configuration base_url: a string to prepend to generated resource links



15
16
17
# File 'lib/jsonapi/resource_serializer.rb', line 15

def url_generator
  @url_generator
end

Instance Method Details



73
74
75
# File 'lib/jsonapi/resource_serializer.rb', line 73

def find_link(query_params)
  url_generator.query_link(query_params)
end

#serialize_to_hash(source) ⇒ Object

Converts a single resource, or an array of resources to a hash, conforming to the JSONAPI structure



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
# File 'lib/jsonapi/resource_serializer.rb', line 31

def serialize_to_hash(source)
  is_resource_collection = source.respond_to?(:to_ary)

  @included_objects = {}
  @include_directives ||= JSONAPI::IncludeDirectives.new(@include)

  process_primary(source, @include_directives.include_directives)

  included_objects = []
  primary_objects = []
  @included_objects.each_value do |objects|
    objects.each_value do |object|
      if object[:primary]
        primary_objects.push(object[:object_hash])
      else
        included_objects.push(object[:object_hash])
      end
    end
  end

  primary_hash = { data: is_resource_collection ? primary_objects : primary_objects[0] }

  primary_hash[:included] = included_objects if included_objects.size > 0
  primary_hash
end


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/jsonapi/resource_serializer.rb', line 57

def serialize_to_links_hash(source, requested_relationship)
  if requested_relationship.is_a?(JSONAPI::Relationship::ToOne)
    data = to_one_linkage(source, requested_relationship)
  else
    data = to_many_linkage(source, requested_relationship)
  end

  {
    links: {
      self: self_link(source, requested_relationship),
      related: related_link(source, requested_relationship)
    },
    data: data
  }
end