Module: Ballast::Concerns::JSONApi::ResponseHandling

Defined in:
lib/ballast/concerns/json_api/response_handling.rb

Overview

A concern to handle JSON API responses.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#includedObject

Returns the value of attribute included.



6
7
8
# File 'lib/ballast/concerns/json_api/response_handling.rb', line 6

def included
  @included
end

Instance Method Details

#response_data(default = nil) ⇒ HashWithIndifferentAccess

Returns the data for the current response.

Parameters:

  • default (Object|NilClass) (defaults to: nil)

    The default data to return if nothing is set.

Returns:

  • (HashWithIndifferentAccess)

    The data for the current response.



29
30
31
# 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) ⇒ HashWithIndifferentAccess

Adds a object to the set of included objects.

Parameters:

  • object (Object)

    The object to include.

  • template (String|Nilclass) (defaults to: nil)

    The template to use for rendering. If not set, it's guessed from object class.

Returns:

  • (HashWithIndifferentAccess)

    The new set of included objects.



54
55
56
57
58
# 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) ⇒ HashWithIndifferentAccess

Returns the additionally included objects for the current response.

Parameters:

  • default (Object|NilClass) (defaults to: nil)

    The default included objects to return if nothing is set.

Returns:

  • (HashWithIndifferentAccess)

    The additionally included objects for the current response.



45
46
47
# File 'lib/ballast/concerns/json_api/response_handling.rb', line 45

def response_included(default = nil)
  controller.included || default || HashWithIndifferentAccess.new
end

Returns the links for the current response.

Parameters:

  • default (Object|NilClass) (defaults to: nil)

    The default links to return if nothing is set.

Returns:

  • (HashWithIndifferentAccess)

    The links for the current response.



37
38
39
# File 'lib/ballast/concerns/json_api/response_handling.rb', line 37

def response_links(default = nil)
  @links || default || HashWithIndifferentAccess.new
end

#response_meta(default = nil) ⇒ HashWithIndifferentAccess

Returns the metadata for the current response.

Parameters:

  • default (Object|NilClass) (defaults to: nil)

    The default metadata to return if nothing is set.

Returns:

  • (HashWithIndifferentAccess)

    The metadata for the current response.



21
22
23
# 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) ⇒ String

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

Returns:

  • (String)

    The template for a object.



11
12
13
14
15
# 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) ⇒ String

Formats a timestamp in ISO 8601 format.

Parameters:

  • timestamp (DateTime|Time|Date)

    The timestamp to format.

Returns:

  • (String)

    The timestamp in ISO 8601 format.



64
65
66
# File 'lib/ballast/concerns/json_api/response_handling.rb', line 64

def response_timestamp(timestamp)
  timestamp.safe_send(:strftime, "%FT%T.%L%z")
end