Module: ActiveFunction::Functions::Rendering

Defined in:
lib/active_function/functions/rendering.rb

Overview

Allows manipulations with SuperBase#response via #render instance method.

Examples:

require "active_function"

ActiveFunction.config do
  plugin :rendering
end

class PostsFunction < ActiveFunction::Base
  def index
    render json: {id: 1, name: "Pupa"}, status: 200, head: {"Some-Header" => "Some-Value"}
  end
end

PostFunction.process(:index) # => { :statusCode=>200, :headers=> {"Content-Type"=>"application/json", "Some-Header" => "Some-Value"}, :body=>"{\"id\":1,\"name\":\"Pupa\"}" }

Defined Under Namespace

Classes: DoubleRenderError

Constant Summary collapse

Error =
Class.new(StandardError)
DEFAULT_HEADER =
{"Content-Type" => "application/json"}.freeze

Instance Method Summary collapse

Instance Method Details

#render(status: 200, json: {}, head: {}) ⇒ Object

Render JSON response.

Parameters:

  • status (Integer) (defaults to: 200)

    HTTP status code (default is 200).

  • json (Hash) (defaults to: {})

    JSON data to be rendered (default is an empty hash).

  • head (Hash) (defaults to: {})

    Additional headers to be included in the response (default is an empty hash).

Raises:

  • (DoubleRenderError)

    Raised if #render is called multiple times in the same action.



47
48
49
50
51
52
53
54
55
# File 'lib/active_function/functions/rendering.rb', line 47

def render(status: 200, json: {}, head: {})
  raise DoubleRenderError, @action_name if performed?

  @response.status     = status
  @response.headers    = head.merge(Hash[DEFAULT_HEADER])
  @response.body       = JSON.generate(json)

  @response.commit!
end