Class: ActiveFunction::Base

Inherits:
SuperBase show all
Defined in:
lib/active_function/base.rb

Overview

The main base class for defining functions using the ActiveFunction framework. Public methods of this class are considered as actions and be proceeded on Base.process call.

Examples:

class MyFunction < ActiveFunction::Base
   def index
      if user = User.find(@request.dig(:data, :user, :id))
          @response.body = user.to_h
      else
          @response.status = 404
      end
   end
end

Constant Summary collapse

Error =
Class.new(StandardError)

Instance Attribute Summary

Attributes inherited from SuperBase

#action_name, #request, #response

Class Method Summary collapse

Methods inherited from SuperBase

#dispatch, #initialize, #process

Constructor Details

This class inherits a constructor from ActiveFunction::SuperBase

Class Method Details

.process(action_name, request = {}, response = Response.new) ⇒ Object

Processes specified action and returns Hash’ed Functions::Response::Response object.

Examples:

MyFunction.process :index, { data: { user: { id: 1 } } } # => { statusCode: 200, body: { id: 1, name: "Pupa" }, headers: {} }

Parameters:

  • action_name (String, Symbol)
    • name of method to call

  • request (Hash) (defaults to: {})
    • request parameters.

  • response (Response) (defaults to: Response.new)
    • Functions::Response response object.

Raises:

  • (ArgumentError)


52
53
54
55
56
# File 'lib/active_function/base.rb', line 52

def self.process(action_name, request = {}, response = Response.new)
  raise ArgumentError, "Action method #{action_name} is not defined" unless method_defined?(action_name)

  new(action_name, request, response).dispatch
end