Class: Landable::ApiResponder

Inherits:
ActionController::Responder
  • Object
show all
Defined in:
app/responders/landable/api_responder.rb

Instance Method Summary collapse

Instance Method Details

#to_formatObject



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
# File 'app/responders/landable/api_responder.rb', line 3

def to_format
  controller.response.headers['X-Landable-Media-Type'] = api_media_type

  serializer = resource_serializer
  if serializer
    options[collection_resource? ? :each_serializer : :serializer] = serializer
    controller.response.headers['X-Landable-Serializer'] = serializer.name if leaky?
  end

  schema = json_schema
  if leaky? && format == :json && schema
    key  = collection_resource? ? resource_name.pluralize : resource_name
    link = "<#{schema}>; rel=\"describedby\"; anchor=\"#/#{key}\""
    link = "#{link}; collection=\"collection\"" if collection_resource?
    controller.response.headers['Link'] = link
  end

  # For updates, rails defaults to returning 204 No Content;
  # we would actually prefer that the updated record be returned,
  # in case an update to one key necessitates an automatic update to another.
  if patch? || put? || delete?
    display resource
  else
    super
  end
end