Class: Hanami::Lambda::Dispatcher Private

Inherits:
Object
  • Object
show all
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

Since:

  • 0.1.0

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.

Since:

  • 0.2.0

->(to) { to }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.

Since:

  • 0.2.0



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

#defaultObject (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.

Since:

  • 0.1.0



13
14
15
# File 'lib/hanami/lambda/dispatcher.rb', line 13

def default
  @default
end

#handlersObject (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.

Since:

  • 0.1.0



13
14
15
# File 'lib/hanami/lambda/dispatcher.rb', line 13

def handlers
  @handlers
end

#rack_appObject (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.

Since:

  • 0.1.0



13
14
15
# File 'lib/hanami/lambda/dispatcher.rb', line 13

def rack_app
  @rack_app
end

#resolverObject (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.

Since:

  • 0.1.0



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

Since:

  • 0.1.0



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

.definitionsObject

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

Since:

  • 0.1.0



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

Parameters:

  • name (String)

    the name of the handler

  • args (Array)

    the arguments to pass to the handler

  • kwargs (Hash)

    the keyword arguments to pass to the handler

  • block (Proc)

    the block to pass to the handler

Since:

  • 0.1.0



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

Parameters:

  • event (Hash)

    the event

  • context (Hash)

    the context

Since:

  • 0.2.0



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

Parameters:

  • event (Hash)

    the event

  • context (Hash)

    the context

Returns:

  • (Handler)

    the handler

Since:

  • 0.1.0



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

Parameters:

  • name (String)

    the name of the handler

  • args (Array)

    the arguments to pass to the handler

  • kwargs (Hash)

    the keyword arguments to pass to the handler

  • block (Proc)

    the block to pass to the handler

Since:

  • 0.2.0



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