Class: Hanami::Lambda::Dispatcher Private
- Inherits:
-
Object
- Object
- Hanami::Lambda::Dispatcher
- Defined in:
- lib/hanami/lambda/dispatcher.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Dispatch Event to the Handler
Constant Summary collapse
- DEFAULT_RESOLVER =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
->(to) { to }
Instance Attribute Summary collapse
- #default ⇒ Object readonly private
- #handlers ⇒ Object readonly private
- #rack_app ⇒ Object readonly private
- #resolver ⇒ Object readonly private
Class Method Summary collapse
-
.build(rack_app:, resolver:) ⇒ Object
private
Build Dispatcher.
-
.definitions ⇒ Object
private
Definitions of handlers.
-
.delegate(name, *args, **kwargs, &block) ⇒ Object
private
Define function delegate action.
Instance Method Summary collapse
-
#call(event:, context:) ⇒ Object
private
Call the handler.
-
#initialize(rack_app:, resolver: DEFAULT_RESOLVER) ⇒ Dispatcher
constructor
private
A new instance of Dispatcher.
-
#lookup(event:, context:) ⇒ Handler
private
Lookup the handler.
-
#register(name, *_args, to: nil) ⇒ Object
private
Register a handler.
Constructor Details
#initialize(rack_app:, resolver: DEFAULT_RESOLVER) ⇒ Dispatcher
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Dispatcher.
16 17 18 19 20 |
# File 'lib/hanami/lambda/dispatcher.rb', line 16 def initialize(rack_app:, resolver: DEFAULT_RESOLVER) @handlers = {} @resolver = resolver @default = Rack.new(rack_app) end |
Instance Attribute Details
#default ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
13 14 15 |
# File 'lib/hanami/lambda/dispatcher.rb', line 13 def default @default end |
#handlers ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
13 14 15 |
# File 'lib/hanami/lambda/dispatcher.rb', line 13 def handlers @handlers end |
#rack_app ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
13 14 15 |
# File 'lib/hanami/lambda/dispatcher.rb', line 13 def rack_app @rack_app end |
#resolver ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
13 14 15 |
# File 'lib/hanami/lambda/dispatcher.rb', line 13 def resolver @resolver end |
Class Method Details
.build(rack_app:, resolver:) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Build Dispatcher
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/hanami/lambda/dispatcher.rb', line 87 def build(rack_app:, resolver:) new(rack_app: rack_app, resolver: resolver).tap do |dispatcher| definitions.each do |(name, args, kwargs, block)| if block dispatcher.register(name, *args, **kwargs, &block) else dispatcher.register(name, *args, **kwargs) end end end end |
.definitions ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Definitions of handlers
70 71 72 |
# File 'lib/hanami/lambda/dispatcher.rb', line 70 def definitions @definitions ||= [] end |
.delegate(name, *args, **kwargs, &block) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Define function delegate action
80 81 82 |
# File 'lib/hanami/lambda/dispatcher.rb', line 80 def delegate(name, *args, **kwargs, &block) definitions << [name, args, kwargs, block] end |
Instance Method Details
#call(event:, context:) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Call the handler
28 29 30 31 32 33 |
# File 'lib/hanami/lambda/dispatcher.rb', line 28 def call(event:, context:) handler = lookup(event: event, context: context) return default.call(event: event, context: context) unless handler handler.call(event: event, context: context) end |
#lookup(event:, context:) ⇒ Handler
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Lookup the handler
41 42 43 44 45 46 47 |
# File 'lib/hanami/lambda/dispatcher.rb', line 41 def lookup(event:, context:) # rubocop:disable Lint/UnusedMethodArgument function_name = ENV.fetch("AWS_LAMBDA_FUNCTION_NAME", context.function_name) handlers .select { |name, _| function_name.include?(name) } .max_by { |name, _| name.length } &.last end |
#register(name, *_args, to: nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Register a handler
57 58 59 60 61 62 63 64 |
# File 'lib/hanami/lambda/dispatcher.rb', line 57 def register(name, *_args, to: nil) handlers[name] = if to.nil? @default else resolver.call(to) end end |