Class: FunctionsFramework::Registry
- Inherits:
-
Object
- Object
- FunctionsFramework::Registry
- 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_http(name, &block) ⇒ self
Add an HTTP function to the registry.
-
#add_startup_task(&block) ⇒ self
Add a startup task.
-
#add_typed(name, request_class: nil, &block) ⇒ self
Add a Typed function to the registry.
-
#initialize ⇒ Registry
constructor
Create a new empty registry.
-
#names ⇒ Array<String>
Returns the list of defined names.
-
#startup_tasks ⇒ Array<FunctionsFramework::Function>
Return an array of startup tasks.
Constructor Details
#initialize ⇒ Registry
Create a new empty registry.
23 24 25 26 27 |
# File 'lib/functions_framework/registry.rb', line 23 def initialize @mutex = ::Mutex.new @functions = {} @start_tasks = [] end |
Instance Method Details
#[](name) ⇒ FunctionsFramework::Function?
Look up a function definition by name.
36 37 38 |
# File 'lib/functions_framework/registry.rb', line 36 def [] name @mutex.synchronize { @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.
119 120 121 122 123 124 125 126 |
# File 'lib/functions_framework/registry.rb', line 119 def add_cloud_event name, &block name = name.to_s @mutex.synchronize do raise ::ArgumentError, "Function already defined: #{name}" if @functions.key? name @functions[name] = Function.cloud_event name, &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.
75 76 77 78 79 80 81 82 |
# File 'lib/functions_framework/registry.rb', line 75 def add_http name, &block name = name.to_s @mutex.synchronize do raise ::ArgumentError, "Function already defined: #{name}" if @functions.key? name @functions[name] = Function.http name, &block end self end |
#add_startup_task(&block) ⇒ self
Add a startup task.
Startup tasks are generally run just before a server starts. They are passed the Function identifying the function to execute, and have no return value.
138 139 140 141 142 143 |
# File 'lib/functions_framework/registry.rb', line 138 def add_startup_task &block @mutex.synchronize do @start_tasks << Function.startup_task(&block) end self end |
#add_typed(name, request_class: nil, &block) ⇒ self
Add a Typed function to the registry.
You must provide a name for the function, and a block that implements the
function. The block should take a single Hash
argument which will be the
JSON decoded request payload. It should return a Hash
response which
will be JSON encoded and written to the response.
98 99 100 101 102 103 104 105 |
# File 'lib/functions_framework/registry.rb', line 98 def add_typed name, request_class: nil, &block name = name.to_s @mutex.synchronize do raise ::ArgumentError, "Function already defined: #{name}" if @functions.key? name @functions[name] = Function.typed name, request_class: request_class, &block end self end |
#names ⇒ Array<String>
Returns the list of defined names
45 46 47 |
# File 'lib/functions_framework/registry.rb', line 45 def names @mutex.synchronize { @functions.keys.sort } end |
#startup_tasks ⇒ Array<FunctionsFramework::Function>
Return an array of startup tasks.
54 55 56 |
# File 'lib/functions_framework/registry.rb', line 54 def startup_tasks @mutex.synchronize { @start_tasks.dup } end |