Class: FunctionsFramework::Function

Inherits:
Object
  • Object
show all
Defined in:
lib/functions_framework/function.rb

Overview

Representation of a function.

A function has a name, a type, and an implementation.

The implementation in general is an object that responds to the call method. For a function of type :http, the call method takes a single Rack::Request argument and returns one of various HTTP response types. See Registry.add_http. For a function of type :cloud_event, the call method takes a single CloudEvent argument, and does not return a value. See Registry.add_cloud_event.

If a callable object is provided directly, its call method is invoked for every function execution. Note that this means it may be called multiple times concurrently in separate threads.

Alternately, the implementation may be provided as a class that should be instantiated to produce a callable object. If a class is provided, it should either subclass CallBase or respond to the same constructor interface, i.e. accepting arbitrary keyword arguments. A separate callable object will be instantiated from this class for every function invocation, so each instance will be used for only one invocation.

Finally, an implementation can be provided as a block. If a block is provided, it will be recast as a call method in an anonymous subclass of CallBase. Thus, providing a block is really just syntactic sugar for providing a class. (This means, for example, that the return keyword will work within the block because it is treated as a method.)

Defined Under Namespace

Classes: CallBase

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, callable = nil, &block) ⇒ Function

Create a new function definition.

Parameters:

  • name (String)

    The function name

  • type (Symbol)

    The type of function. Valid types are :http and :cloud_event.

  • callable (Class, #call) (defaults to: nil)

    A callable object or class.

  • block (Proc)

    The function code as a block.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/functions_framework/function.rb', line 58

def initialize name, type, callable = nil, &block
  @name = name
  @type = type
  @callable = @callable_class = nil
  if callable.respond_to? :call
    @callable = callable
  elsif callable.is_a? ::Class
    @callable_class = callable
  elsif block_given?
    @callable_class = ::Class.new CallBase do
      define_method :call, &block
    end
  else
    raise ::ArgumentError, "No callable given for function"
  end
end

Instance Attribute Details

#nameString (readonly)

Returns The function name.

Returns:

  • (String)

    The function name



78
79
80
# File 'lib/functions_framework/function.rb', line 78

def name
  @name
end

#typeSymbol (readonly)

Returns The function type.

Returns:

  • (Symbol)

    The function type



83
84
85
# File 'lib/functions_framework/function.rb', line 83

def type
  @type
end

Instance Method Details

#new_call(logger: nil) ⇒ #call

Get a callable for performing a function invocation. This will either return the singleton callable object, or instantiate a new callable from the configured class.

Parameters:

  • logger (::Logger) (defaults to: nil)

    The logger for use by function executions. This may or may not be used by the callable.

Returns:

  • (#call)


94
95
96
97
98
# File 'lib/functions_framework/function.rb', line 94

def new_call logger: nil
  return @callable unless @callable.nil?
  logger ||= FunctionsFramework.logger
  @callable_class.new logger: logger, function_name: name, function_type: type
end