Class: JSONAPI::ResourceSerializer
- Inherits:
-
Object
- Object
- JSONAPI::ResourceSerializer
- Defined in:
- lib/jsonapi/resource_serializer.rb
Instance Method Summary collapse
- #find_link(query_params) ⇒ Object
-
#initialize(primary_resource_klass, options = {}) ⇒ ResourceSerializer
constructor
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.
-
#serialize_to_hash(source) ⇒ Object
Converts a single resource, or an array of resources to a hash, conforming to the JSONAPI structure.
- #serialize_to_links_hash(source, requested_association) ⇒ Object
Constructor Details
#initialize(primary_resource_klass, options = {}) ⇒ ResourceSerializer
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
association 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 18 19 20 21 22 23 24 25 |
# File 'lib/jsonapi/resource_serializer.rb', line 15 def initialize(primary_resource_klass, = {}) @primary_resource_klass = primary_resource_klass @primary_class_name = @primary_resource_klass._type @fields = .fetch(:fields, {}) @include = .fetch(:include, []) @include_directives = [:include_directives] @key_formatter = .fetch(:key_formatter, JSONAPI.configuration.key_formatter) @route_formatter = .fetch(:route_formatter, JSONAPI.configuration.route_formatter) @base_url = .fetch(:base_url, '') end |
Instance Method Details
#find_link(query_params) ⇒ Object
70 71 72 |
# File 'lib/jsonapi/resource_serializer.rb', line 70 def find_link(query_params) "#{@base_url}/#{formatted_module_path_from_klass(@primary_resource_klass)}#{@route_formatter.format(@primary_resource_klass._type.to_s)}?#{query_params.to_query}" end |
#serialize_to_hash(source) ⇒ Object
Converts a single resource, or an array of resources to a hash, conforming to the JSONAPI structure
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/jsonapi/resource_serializer.rb', line 28 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 |
#serialize_to_links_hash(source, requested_association) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/jsonapi/resource_serializer.rb', line 54 def serialize_to_links_hash(source, requested_association) if requested_association.is_a?(JSONAPI::Association::HasOne) data = has_one_linkage(source, requested_association) else data = has_many_linkage(source, requested_association) end { links: { self: self_link(source, requested_association), related: (source, requested_association) }, data: data } end |