Class: BoltRb::Handlers::Base
- Inherits:
-
Object
- Object
- BoltRb::Handlers::Base
- Defined in:
- lib/bolt_rb/handlers/base.rb
Overview
Base class for all Slack event handlers.
This provides the common interface and middleware execution that all handler types (Event, Command, Action, Shortcut) inherit from.
Subclasses should:
- Override .matches?(payload) to define matching logic
- Override #handle to implement the handler behavior
- Optionally use .use(middleware) to add handler-specific middleware
Direct Known Subclasses
ActionHandler, AssistantHandler, CommandHandler, EventHandler, ShortcutHandler, ViewClosedHandler, ViewSubmissionHandler
Class Attribute Summary collapse
-
.matcher_config ⇒ Object?
readonly
Configuration for matching this handler to payloads Subclasses should set this to define their matching criteria.
Instance Attribute Summary collapse
-
#context ⇒ Context
readonly
The context object for this handler invocation.
Class Method Summary collapse
-
.inherited(subclass) ⇒ void
Hook called when a class inherits from Base.
-
.matches?(_payload) ⇒ Boolean
Determines if this handler matches the given payload.
-
.middleware_stack ⇒ Array<Class>
Returns the middleware stack for this handler class.
-
.use(middleware_class) ⇒ void
Adds middleware to this handler's stack.
Instance Method Summary collapse
-
#ack(response = nil) ⇒ void
Acknowledges the event.
-
#call ⇒ void
Executes this handler.
-
#channel ⇒ String?
Returns the channel ID from the context.
-
#client ⇒ Slack::Web::Client
Returns the Slack Web API client.
-
#handle ⇒ Object
The main handler logic.
-
#initialize(context) ⇒ Base
constructor
Creates a new handler instance.
-
#payload ⇒ Hash
Returns the raw payload from the context.
-
#respond(message) ⇒ Net::HTTPResponse?
Responds using the response_url.
-
#say(message) ⇒ Hash
Posts a message to the channel.
-
#user ⇒ String?
Returns the user ID from the context.
Constructor Details
#initialize(context) ⇒ Base
Creates a new handler instance
91 92 93 |
# File 'lib/bolt_rb/handlers/base.rb', line 91 def initialize(context) @context = context end |
Class Attribute Details
.matcher_config ⇒ Object? (readonly)
Configuration for matching this handler to payloads Subclasses should set this to define their matching criteria
33 34 35 |
# File 'lib/bolt_rb/handlers/base.rb', line 33 def matcher_config @matcher_config end |
Instance Attribute Details
#context ⇒ Context (readonly)
Returns The context object for this handler invocation.
86 87 88 |
# File 'lib/bolt_rb/handlers/base.rb', line 86 def context @context end |
Class Method Details
.inherited(subclass) ⇒ void
This method returns an undefined value.
Hook called when a class inherits from Base
Ensures each subclass gets its own independent middleware stack and registers the handler with the global router.
77 78 79 80 81 82 |
# File 'lib/bolt_rb/handlers/base.rb', line 77 def inherited(subclass) super subclass.instance_variable_set(:@middleware_stack, []) # Auto-register with the global router BoltRb.router.register(subclass) end |
.matches?(_payload) ⇒ Boolean
Determines if this handler matches the given payload
Base implementation always returns false. Subclasses should override this to implement their matching logic.
66 67 68 |
# File 'lib/bolt_rb/handlers/base.rb', line 66 def matches?(_payload) false end |
.middleware_stack ⇒ Array<Class>
Returns the middleware stack for this handler class
38 39 40 |
# File 'lib/bolt_rb/handlers/base.rb', line 38 def middleware_stack @middleware_stack ||= [] end |
.use(middleware_class) ⇒ void
This method returns an undefined value.
Adds middleware to this handler's stack
Middleware is executed in the order added, wrapping the #handle method. Each middleware should call yield to continue the chain.
55 56 57 |
# File 'lib/bolt_rb/handlers/base.rb', line 55 def use(middleware_class) middleware_stack << middleware_class end |
Instance Method Details
#ack(response = nil) ⇒ void
This method returns an undefined value.
Acknowledges the event
Delegates to Context#ack
139 140 141 |
# File 'lib/bolt_rb/handlers/base.rb', line 139 def ack(response = nil) context.ack(response) end |
#call ⇒ void
This method returns an undefined value.
Executes this handler
Runs the middleware stack, then calls #handle at the end of the chain.
158 159 160 |
# File 'lib/bolt_rb/handlers/base.rb', line 158 def call run_middleware { handle } end |
#channel ⇒ String?
Returns the channel ID from the context
119 120 121 |
# File 'lib/bolt_rb/handlers/base.rb', line 119 def channel context.channel end |
#client ⇒ Slack::Web::Client
Returns the Slack Web API client
105 106 107 |
# File 'lib/bolt_rb/handlers/base.rb', line 105 def client context.client end |
#handle ⇒ Object
The main handler logic
Subclasses must override this method to implement their behavior.
167 168 169 |
# File 'lib/bolt_rb/handlers/base.rb', line 167 def handle raise NotImplementedError, 'Subclasses must implement #handle' end |
#payload ⇒ Hash
Returns the raw payload from the context
98 99 100 |
# File 'lib/bolt_rb/handlers/base.rb', line 98 def payload context.payload end |
#respond(message) ⇒ Net::HTTPResponse?
Responds using the response_url
Delegates to Context#respond
149 150 151 |
# File 'lib/bolt_rb/handlers/base.rb', line 149 def respond() context.respond() end |
#say(message) ⇒ Hash
Posts a message to the channel
Delegates to Context#say
129 130 131 |
# File 'lib/bolt_rb/handlers/base.rb', line 129 def say() context.say() end |
#user ⇒ String?
Returns the user ID from the context
112 113 114 |
# File 'lib/bolt_rb/handlers/base.rb', line 112 def user context.user end |