Module: Apes::Concerns::Response

Included in:
Apes::Controller
Defined in:
lib/apes/concerns/response.rb

Overview

JSON API response handling module.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (Object) included

Returns the value of attribute included



10
11
12
# File 'lib/apes/concerns/response.rb', line 10

def included
  @included
end

Instance Method Details

- (HashWithIndifferentAccess|Object|Nil) response_data(default = nil)

Returns the data for the current response.

Parameters:

  • default (Object) (defaults to: nil)

    Fallback data if nothing is found.

Returns:

  • (HashWithIndifferentAccess|Object|Nil)

    The data for the current response.



34
35
36
# File 'lib/apes/concerns/response.rb', line 34

def response_data(default = nil)
  @data || default || HashWithIndifferentAccess.new
end

- (HashWithIndifferentAccess) response_include(object, template = nil)

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

Parameters:

  • object (Object)

    The object to include.

  • template (String) (defaults to: nil)

    The template to use for rendering.

Returns:

  • (HashWithIndifferentAccess)

    A hash of objects to include. Keys are a template:id formatted strings, values are [object, template] pairs.



59
60
61
62
63
# 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

- (HashWithIndifferentAccess|Object|Nil) response_included(default = nil)

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

Parameters:

  • default (Object) (defaults to: nil)

    Fallback data if nothing is found.

Returns:

  • (HashWithIndifferentAccess|Object|Nil)

    The included objects for the current response.



50
51
52
# File 'lib/apes/concerns/response.rb', line 50

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

Returns the linked objects for the current response.

Parameters:

  • default (Object) (defaults to: nil)

    Fallback data if nothing is found.

Returns:

  • (HashWithIndifferentAccess|Object|Nil)

    The linked objects for the current response.



42
43
44
# File 'lib/apes/concerns/response.rb', line 42

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

- (HashWithIndifferentAccess|Object|Nil) response_meta(default = nil)

Returns the metadata for the current response.

Parameters:

  • default (Object) (defaults to: nil)

    Fallback data if nothing is found.

Returns:

  • (HashWithIndifferentAccess|Object|Nil)

    The metadata for the current response.



26
27
28
# File 'lib/apes/concerns/response.rb', line 26

def response_meta(default = nil)
  @meta || default || HashWithIndifferentAccess.new
end

- (String) response_template_for(object)

Returns the template to use to render a object.

Parameters:

  • object (Object)

    The object to render.

Returns:

  • (String)

    The template to use.



16
17
18
19
20
# 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

- (String) response_timestamp(timestamp)

Serializes a timestamp.

Parameters:

  • timestamp (DateTime)

    The timestamp to serialize.

Returns:

  • (String)

    The serialized timestamp.



69
70
71
# File 'lib/apes/concerns/response.rb', line 69

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