module Apes::Concerns::Response

JSON API response handling module.

Attributes

included[RW]

Public Instance Methods

response_data(default = nil) click to toggle source

Returns the data for the current response.

@param default [Object] Fallback data if nothing is found. @return [HashWithIndifferentAccess|Object|Nil] The data for the current response.

# File lib/apes/concerns/response.rb, line 34
def response_data(default = nil)
  @data || default || HashWithIndifferentAccess.new
end
response_include(object, template = nil) click to toggle source

Adds an object to the included (side-load) set.

@param object [Object] The object to include. @param template [String] The template to use for rendering. @return [HashWithIndifferentAccess] A hash of objects to include. Keys are a template:id formatted strings, values are `[object, template]` pairs.

# File lib/apes/concerns/response.rb, line 59
def response_include(object, template = nil)
  controller.included ||= HashWithIndifferentAccess.new
  controller.included[sprintf("%s:%s", response_template_for(object), object.to_param)] = [object, template]
  controller.included
end
response_included(default = nil) click to toggle source

Returns the included (side-loaded) objects for the current response.

@param default [Object] Fallback data if nothing is found. @return [HashWithIndifferentAccess|Object|Nil] The included objects for the current response.

# File lib/apes/concerns/response.rb, line 50
def response_included(default = nil)
  controller.included || default || HashWithIndifferentAccess.new
end
response_meta(default = nil) click to toggle source

Returns the metadata for the current response.

@param default [Object] Fallback data if nothing is found. @return [HashWithIndifferentAccess|Object|Nil] The metadata for the current response.

# File lib/apes/concerns/response.rb, line 26
def response_meta(default = nil)
  @meta || default || HashWithIndifferentAccess.new
end
response_template_for(object) click to toggle source

Returns the template to use to render a object.

@param object [Object] The object to render. @return [String] The template to use.

# File lib/apes/concerns/response.rb, line 16
def response_template_for(object)
  return @object_template if @object_template
  object = object.first if object.respond_to?(:first)
  object.class.name.underscore.gsub("/", "_")
end
response_timestamp(timestamp) click to toggle source

Serializes a timestamp.

@param timestamp [DateTime] The timestamp to serialize. @return [String] The serialized timestamp.

# File lib/apes/concerns/response.rb, line 69
def response_timestamp(timestamp)
  timestamp.safe_send(:strftime, "%FT%T.%L%z")
end