Class: Otto::RouteHandlers::LambdaHandler

Inherits:
BaseHandler
  • Object
show all
Defined in:
lib/otto/route_handlers/lambda.rb

Overview

Handler for pre-registered lambda/proc route targets (issue #41).

Route syntax GET /ping &health_check parses to kind :lambda with klass_name = “health_check” (the registry KEY, not a Ruby constant) and method_name = nil. The proc is looked up O(1) by name from the Otto instance’s pre-registered lambda_handlers — no eval, no dynamic constants.

Reuses BaseHandler#call: implements #invoke_target and guards the base’s constant-resolution steps (#target_class / #handler_name).

Instance Attribute Summary

Attributes inherited from BaseHandler

#otto_instance, #route_definition

Instance Method Summary collapse

Methods inherited from BaseHandler

#call, #finalize_response, #handle_execution_error, #handle_local_error, #handle_response, #initialize, #setup_request_response

Constructor Details

This class inherits a constructor from Otto::RouteHandlers::BaseHandler

Instance Method Details

#handler_nameObject (protected)

Derive the log/handler name from the route, not target_class.name (which would be nil.name -> NoMethodError inside handle_execution_error).



31
32
33
# File 'lib/otto/route_handlers/lambda.rb', line 31

def handler_name
  "Lambda[#{route_definition.klass_name}]"
end

#invoke_target(req, res) ⇒ Array (protected)

Look up the pre-registered proc and invoke it with (req, res, extra_params).

Returns:

  • (Array)

    [result, context] consumed by BaseHandler#handle_response



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/otto/route_handlers/lambda.rb', line 37

def invoke_target(req, res)
  handler_key = route_definition.klass_name
  lambda_proc = lambda_registry[handler_key]

  unless lambda_proc.respond_to?(:call)
    raise ArgumentError,
          "Lambda handler '#{handler_key}' is not registered or not callable"
  end

  result = lambda_proc.call(req, res, @extra_params || {})
  [result, { request: req }]
end

#target_classObject (protected)

No Ruby constant backs a lambda route. Returning nil (a) prevents ConstantResolver.safe_const_get from raising on a registry key and (b) makes BaseHandler#setup_request_response skip its target_class extension block.



25
26
27
# File 'lib/otto/route_handlers/lambda.rb', line 25

def target_class
  nil
end