Module: Grape::Formatter::JSONAPIResources

Defined in:
lib/grape-jsonapi-resources/formatter.rb

Class Method Summary collapse

Class Method Details

.build_options_from_endpoint(endpoint) ⇒ Object



68
69
70
71
72
# File 'lib/grape-jsonapi-resources/formatter.rb', line 68

def build_options_from_endpoint(endpoint)
  options = {}
  options[:base_url] = endpoint.namespace_inheritable(:jsonapi_base_url) if endpoint.namespace_inheritable(:jsonapi_base_url)
  options
end

.call(resource, env) ⇒ Object



5
6
7
# File 'lib/grape-jsonapi-resources/formatter.rb', line 5

def call(resource, env)
  serialize_resource(resource, env) || Grape::Formatter::Json.call(resource, env)
end

.get_resource_for(klass) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/grape-jsonapi-resources/formatter.rb', line 88

def get_resource_for(klass)
  resources_cache.fetch_or_store(klass) do
    resource_class_name = "#{klass.name}Resource"
    resource_class = resource_class_name.safe_constantize

    if resource_class
      resource_class
    elsif klass.superclass
      get_resource_for(klass.superclass)
    end
  end
end

.resource_class_for(resource) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/grape-jsonapi-resources/formatter.rb', line 74

def resource_class_for(resource)
  if resource.class.respond_to?(:jsonapi_resource_class)
    resource.class.jsonapi_resource_class
  elsif resource.respond_to?(:to_ary)
    resource_class_for(resource.to_ary.first)
  else
    get_resource_for(resource.class)
  end
end

.resources_cacheObject



84
85
86
# File 'lib/grape-jsonapi-resources/formatter.rb', line 84

def resources_cache
  @resources_cache ||= ThreadSafe::Cache.new
end

.serialize_resource(resource, env) ⇒ Object



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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/grape-jsonapi-resources/formatter.rb', line 9

def serialize_resource(resource, env)
  endpoint = env['api.endpoint']

  jsonapi_options = build_options_from_endpoint(endpoint)
  jsonapi_options.merge!(env['jsonapi_options'] || {})

  context = {}
  context.merge!(current_user: endpoint.current_user) if endpoint.respond_to?(:current_user)
  context.merge!(jsonapi_options.delete(:context)) if jsonapi_options[:context]

  resource_class = resource_class_for(resource)

  if resource_class.nil?
    if resource.blank?
      # Return blank object
      blank_return = {}
      if resource.respond_to?(:to_ary)
        blank_return[:data] = []
      else
        blank_return[:data] = {}
      end
      blank_return[:meta] = jsonapi_options[:meta] if jsonapi_options[:meta]
      return blank_return.to_json
    else
      return nil
    end
  end

  resource_instances = nil
  sorted_primary_ids = nil
  if resource.respond_to?(:to_ary)
    resource_instances = resource.to_ary.compact.collect do |each_resource|
      each_resource_class = resource_class_for(each_resource)
      each_resource_class.new(each_resource, context)
    end
    sorted_primary_ids = resource_instances.collect { |resource_instance| resource_instance.try(:id) }
  else
    resource_instances = resource_class.new(resource, context)
  end

  resource_serialzed = JSONAPI::ResourceSerializer.new(resource_class, jsonapi_options).serialize_to_hash(resource_instances)
  json_output = if jsonapi_options[:meta]
    # Add option to merge top level meta tag as jsonapi-resources does not appear to support this
    resource_serialzed.as_json.merge(meta: jsonapi_options[:meta])
  else
    resource_serialzed
  end

  # Ensure sort order is maintained, serialize_to_hash can reorder objects if
  # objects of the array are of different types (polymorphic cases)
  json_output = json_output.stringify_keys
  if sorted_primary_ids && (data = json_output["data"]).present?
    sorted_primary_ids = sorted_primary_ids.map(&:to_s)
    json_output["data"] = data.sort_by { |d| sorted_primary_ids.index(d["id"]) }
  end

  json_output.to_json
end