Class: FunctionsFramework::Function::Callable

Inherits:
Object
  • Object
show all
Defined in:
lib/functions_framework/function.rb

Overview

A base class for a callable object that provides calling context.

An object of this class is self while a function block is running.

Instance Method Summary collapse

Constructor Details

#initialize(globals: nil, logger: nil) ⇒ Callable

Create a callable object with the given context.

Parameters:

  • globals (Hash) (defaults to: nil)

    A set of globals available to the call.

  • logger (Logger) (defaults to: nil)

    A logger for use by the function call.



179
180
181
182
# File 'lib/functions_framework/function.rb', line 179

def initialize globals: nil, logger: nil
  @__globals = globals || {}
  @__logger = logger || FunctionsFramework.logger
end

Instance Method Details

#global(key) ⇒ Object

Get the given named global.

For most function calls, the following globals will be defined:

  • :function_name (String) The name of the running function.
  • :function_type (Symbol) The type of the running function, either :http or :cloud_event.

You can also set additional globals from a startup task.

Parameters:

  • key (Symbol, String)

    The name of the global to get.

Returns:

  • (Object)


198
199
200
# File 'lib/functions_framework/function.rb', line 198

def global key
  @__globals[key]
end

#loggerLogger

A logger for use by this call.

Returns:

  • (Logger)


219
220
221
# File 'lib/functions_framework/function.rb', line 219

def logger
  @__logger
end

#set_global(key, value) ⇒ Object

Set a global. This can be called from startup tasks, but the globals are frozen when the server starts, so this call will raise an exception if called from a normal function.

Parameters:

  • key (Symbol, String)
  • value (Object)


210
211
212
# File 'lib/functions_framework/function.rb', line 210

def set_global key, value
  @__globals[key] = value
end