Class: FunctionsFramework::Registry
- Inherits:
-
Object
- Object
- FunctionsFramework::Registry
- Includes:
- MonitorMixin
- Defined in:
- lib/functions_framework/registry.rb
Overview
Registry providing lookup of functions by name.
Instance Method Summary collapse
-
#[](name) ⇒ FunctionsFramework::Function?
Look up a function definition by name.
-
#add_cloud_event(name, &block) ⇒ self
Add a CloudEvent function to the registry.
-
#add_event(name, &block) ⇒ Object
deprecated
Deprecated.
Use #add_cloud_event instead.
-
#add_http(name, &block) ⇒ self
Add an HTTP function to the registry.
-
#initialize ⇒ Registry
constructor
Create a new empty registry.
-
#names ⇒ Array<String>
Returns the list of defined names.
Constructor Details
#initialize ⇒ Registry
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.
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.
104 105 106 107 108 109 110 111 |
# File 'lib/functions_framework/registry.rb', line 104 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) ⇒ Object
Use #add_cloud_event instead.
This is an obsolete interface that defines an event function taking two arguments (data and context) rather than one.
84 85 86 87 88 89 90 91 |
# File 'lib/functions_framework/registry.rb', line 84 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.
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 |
#names ⇒ Array<String>
Returns the list of defined names
48 49 50 |
# File 'lib/functions_framework/registry.rb', line 48 def names @functions.keys.sort end |