Class: FunctionsFramework::Registry

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/functions_framework/registry.rb

Overview

Registry providing lookup of functions by name.

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Create a new empty registry.



27
28
29
30
# File 'lib/functions_framework/registry.rb', line 27

def initialize
  super()
  @functions = {}
end

Instance Method Details

#[](name) ⇒ FunctionsFramework::Function?

Look up a function definition by name.

Parameters:

  • name (String)

    The function name

Returns:



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

def [] name
  @functions[name.to_s]
end

#add_cloud_event(name, &block) ⇒ self

Add a CloudEvent function to the registry.

You must provide a name for the function, and a block that implemets the function. The block should take one argument: the event object of type CloudEvents::Event. Any return value is ignored.

Parameters:

  • name (String)

    The function name

  • block (Proc)

    The function code as a proc

Returns:

  • (self)


90
91
92
93
94
95
96
97
# File 'lib/functions_framework/registry.rb', line 90

def add_cloud_event name, &block
  name = name.to_s
  synchronize do
    raise ::ArgumentError, "Function already defined: #{name}" if @functions.key? name
    @functions[name] = Function.new name, :cloud_event, &block
  end
  self
end

#add_http(name, &block) ⇒ self

Add an HTTP function to the registry.

You must provide a name for the function, and a block that implemets the function. The block should take a single Rack::Request argument. It should return one of the following:

  • A standard 3-element Rack response array. See https://github.com/rack/rack/blob/master/SPEC
  • A Rack::Response object.
  • A simple String that will be sent as the response body.
  • A Hash object that will be encoded as JSON and sent as the response body.

Parameters:

  • name (String)

    The function name

  • block (Proc)

    The function code as a proc

Returns:

  • (self)


69
70
71
72
73
74
75
76
# File 'lib/functions_framework/registry.rb', line 69

def add_http name, &block
  name = name.to_s
  synchronize do
    raise ::ArgumentError, "Function already defined: #{name}" if @functions.key? name
    @functions[name] = Function.new name, :http, &block
  end
  self
end

#namesArray<String>

Returns the list of defined names

Returns:

  • (Array<String>)


48
49
50
# File 'lib/functions_framework/registry.rb', line 48

def names
  @functions.keys.sort
end