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 a code definition.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, &block) ⇒ Function

Create a new function definition.

Parameters:

  • name (String)

    The function name

  • type (Symbol)

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

  • block (Proc)

    The function code as a proc



30
31
32
33
34
35
36
# File 'lib/functions_framework/function.rb', line 30

def initialize name, type, &block
  @name = name
  @type = type
  @execution_context_class = Class.new do
    define_method :call, &block
  end
end

Instance Attribute Details

#nameString (readonly)

Returns The function name.

Returns:

  • (String)

    The function name



41
42
43
# File 'lib/functions_framework/function.rb', line 41

def name
  @name
end

#typeSymbol (readonly)

Returns The function type.

Returns:

  • (Symbol)

    The function type



46
47
48
# File 'lib/functions_framework/function.rb', line 46

def type
  @type
end

Instance Method Details

#call(argument) ⇒ Object

Call the function. You must pass an argument appropriate to the type of function.

  • A :http type function takes a Rack::Request argument, and returns a Rack response type. See Registry.add_http.
  • A :cloud_event type function takes a CloudEvents::Event argument, and does not return a value. See Registry.add_cloud_event.

Parameters:

Returns:

  • (Object)


61
62
63
64
65
66
67
68
69
# File 'lib/functions_framework/function.rb', line 61

def call argument
  execution_context = @execution_context_class.new
  case type
  when :event
    execution_context.call argument.data, argument
  else
    execution_context.call argument
  end
end