class FunctionsFramework::Server::EventApp

@private

Public Class Methods

new(function, globals, config) click to toggle source
# File lib/functions_framework/server.rb, line 417
def initialize function, globals, config
  super config
  @function = function
  @globals = globals
  @cloud_events = ::CloudEvents::HttpBinding.default
  @legacy_events = LegacyEventConverter.new
end

Public Instance Methods

call(env) click to toggle source
# File lib/functions_framework/server.rb, line 425
def call env
  return notfound_response if excluded_path? env
  logger = env["rack.logger"] ||= @config.logger
  event = decode_event env
  response =
    case event
    when ::CloudEvents::Event
      handle_cloud_event event, logger
    when ::Array
      ::CloudEvents::CloudEventsError.new "Batched CloudEvents are not supported"
    when ::CloudEvents::CloudEventsError
      event
    else
      raise "Unexpected event type: #{event.class}"
    end
  interpret_response response
end

Private Instance Methods

decode_event(env) click to toggle source
# File lib/functions_framework/server.rb, line 445
def decode_event env
  begin
    @cloud_events.decode_event env
  rescue ::CloudEvents::NotCloudEventError
    env["rack.input"].rewind rescue nil
    @legacy_events.decode_rack_env(env) ||
      raise(::CloudEvents::CloudEventsError, "Unrecognized event format")
  end
rescue ::CloudEvents::CloudEventsError => e
  e
end
handle_cloud_event(event, logger) click to toggle source
# File lib/functions_framework/server.rb, line 457
def handle_cloud_event event, logger
  logger.info "FunctionsFramework: Handling CloudEvent"
  @function.call event, globals: @globals, logger: logger
  "ok"
rescue ::StandardError => e
  e
end