File: jsonapi.rb

package info (click to toggle)
ruby-active-model-serializers 0.10.12-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,752 kB
  • sloc: ruby: 13,138; sh: 53; makefile: 6
file content (51 lines) | stat: -rw-r--r-- 1,330 bytes parent folder | download | duplicates (2)
1
2
3
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# frozen_string_literal: true

module ActiveModelSerializers
  module Adapter
    class JsonApi < Base
      # {http://jsonapi.org/format/#document-jsonapi-object Jsonapi Object}

      # toplevel_jsonapi
      # definition:
      #   JSON Object
      #
      # properties:
      #   version : String
      #   meta
      #
      # description:
      #   An object describing the server's implementation
      # structure:
      #   {
      #     version: ActiveModelSerializers.config.jsonapi_version,
      #     meta: ActiveModelSerializers.config.jsonapi_toplevel_meta
      #   }.reject! { |_, v| v.blank? }
      # prs:
      #   https://github.com/rails-api/active_model_serializers/pull/1050
      module Jsonapi
        module_function

        def add!(hash)
          hash.merge!(object) if include_object?
        end

        def include_object?
          ActiveModelSerializers.config.jsonapi_include_toplevel_object
        end

        # TODO: see if we can cache this
        def object
          object = {
            jsonapi: {
              version: ActiveModelSerializers.config.jsonapi_version,
              meta: ActiveModelSerializers.config.jsonapi_toplevel_meta
            }
          }
          object[:jsonapi].reject! { |_, v| v.blank? }

          object
        end
      end
    end
  end
end