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.

See also #add_event which creates a function that takes data and context as separate arguments.

Parameters:

  • name (String)

    The function name

  • block (Proc)

    The function code as a proc

Returns:

  • (self)


128
129
130
131
132
133
134
135
# File 'lib/functions_framework/registry.rb', line 128

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_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 two arguments: the event data and the event context. Any return value is ignored.

The event data argument will be one of the following types:

  • A String (with encoding ASCII-8BIT) if the data is in the form of binary data. You may choose to perform additional interpretation of the binary data using information in the content type provided by the context argument.
  • Any data type that can be represented in JSON (i.e. String, Integer, Array, Hash, true, false, or nil) if the event came with a JSON payload. The content type may also be set in the context if the data is a String.

The context argument will be of type CloudEvents::Event, and will contain CloudEvents context attributes such as id and type.

See also #add_cloud_event which creates a function that takes a single argument of type CloudEvents::Event.

Parameters:

  • name (String)

    The function name

  • block (Proc)

    The function code as a proc

Returns:

  • (self)


105
106
107
108
109
110
111
112
# File 'lib/functions_framework/registry.rb', line 105

def add_event name, &block
  name = name.to_s
  synchronize do
    raise ::ArgumentError, "Function already defined: #{name}" if @functions.key? name
    @functions[name] = Function.new name, :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