-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to dispatch by function name
- Loading branch information
Showing
5 changed files
with
129 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
module Hanami | ||
module Lambda | ||
# Dispatch Event to the Handler | ||
# | ||
# @api private | ||
class Dispatcher | ||
attr_reader :app, :handlers | ||
|
||
def initialize(app) | ||
@app = app | ||
@handlers = {} | ||
end | ||
|
||
# Call the handler | ||
# | ||
# @param event [Hash] the event | ||
# @param context [Hash] the context | ||
# | ||
# @since 0.2.0 | ||
def call(event:, context:) | ||
handler = lookup(event: event, context: context) | ||
handler.call(event: event, context: context) | ||
end | ||
|
||
# Lookup the handler | ||
# | ||
# @param event [Hash] the event | ||
# @param context [Hash] the context | ||
# | ||
# @return [Handler] the handler | ||
def lookup(event:, context:) | ||
function_name = context.function_name | ||
handlers[function_name] | ||
end | ||
|
||
# Register a handler | ||
# | ||
# @param name [String] the name of the handler | ||
# @param args [Array] the arguments to pass to the handler | ||
# @param kwargs [Hash] the keyword arguments to pass to the handler | ||
# @param block [Proc] the block to pass to the handler | ||
# | ||
# @since 0.2.0 | ||
def register(name, ...) | ||
handlers[name] = Rack.new(app) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters