module Ballast::Concerns::JSONApi::ResponseHandling

A concern to handle JSON API responses.

Attributes

included[RW]

Public Instance Methods

response_data(default = nil) click to toggle source

Returns the data for the current response.

@param default [Object|NilClass] The default data to return if nothing is set. @return [HashWithIndifferentAccess] The data for the current response.

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

Adds a object to the set of included objects.

@param object [Object] The object to include. @param template [String|Nilclass] The template to use for rendering. If not set, it's guessed from object class. @return [HashWithIndifferentAccess] The new set of included objects.

# File lib/ballast/concerns/json_api/response_handling.rb, line 54
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 additionally included objects for the current response.

@param default [Object|NilClass] The default included objects to return if nothing is set. @return [HashWithIndifferentAccess] The additionally included objects for the current response.

# File lib/ballast/concerns/json_api/response_handling.rb, line 45
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|NilClass] The default metadata to return if nothing is set. @return [HashWithIndifferentAccess] The metadata for the current response.

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

Returns the template for a object. It can overriden by setting the `@object_template` variable.

@return [String] The template for a object.

# File lib/ballast/concerns/json_api/response_handling.rb, line 11
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

Formats a timestamp in ISO 8601 format.

@param timestamp [DateTime|Time|Date] The timestamp to format. @return [String] The timestamp in ISO 8601 format.

# File lib/ballast/concerns/json_api/response_handling.rb, line 64
def response_timestamp(timestamp)
  timestamp.safe_send(:strftime, "%FT%T.%L%z")
end