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
# File 'lib/functions_framework/function.rb', line 30

def initialize name, type, &block
  @name = name
  @type = type
  @block = block
end

Instance Attribute Details

#blockProc (readonly)

Returns The function code as a proc.

Returns:

  • (Proc)

    The function code as a proc



49
50
51
# File 'lib/functions_framework/function.rb', line 49

def block
  @block
end

#nameString (readonly)

Returns The function name.

Returns:

  • (String)

    The function name



39
40
41
# File 'lib/functions_framework/function.rb', line 39

def name
  @name
end

#typeSymbol (readonly)

Returns The function type.

Returns:

  • (Symbol)

    The function type



44
45
46
# File 'lib/functions_framework/function.rb', line 44

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)


64
65
66
67
68
69
70
71
# File 'lib/functions_framework/function.rb', line 64

def call argument
  case type
  when :event
    block.call argument.data, argument
  else
    block.call argument
  end
end