Class: BoltRb::Handlers::Base

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

Examples:

Creating a custom handler

class MyHandler < BoltRb::Handlers::Base
  use MyCustomMiddleware

  def self.matches?(payload)
    payload.dig('event', 'type') == 'message'
  end

  def handle
    say("Received your message!")
  end
end

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Base

Creates a new handler instance

Parameters:

  • context (Context)

    The context containing payload, client, and ack



91
92
93
# File 'lib/bolt_rb/handlers/base.rb', line 91

def initialize(context)
  @context = context
end

Class Attribute Details

.matcher_configObject? (readonly)

Configuration for matching this handler to payloads Subclasses should set this to define their matching criteria

Returns:

  • (Object, nil)

    The matcher configuration



33
34
35
# File 'lib/bolt_rb/handlers/base.rb', line 33

def matcher_config
  @matcher_config
end

Instance Attribute Details

#contextContext (readonly)

Returns The context object for this handler invocation.

Returns:

  • (Context)

    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.

Parameters:

  • subclass (Class)

    The inheriting class



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.

Parameters:

  • _payload (Hash)

    The incoming Slack payload

Returns:

  • (Boolean)

    true if this handler should process the payload



66
67
68
# File 'lib/bolt_rb/handlers/base.rb', line 66

def matches?(_payload)
  false
end

.middleware_stackArray<Class>

Returns the middleware stack for this handler class

Returns:

  • (Array<Class>)

    Array of middleware classes



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.

Examples:

Adding middleware

class MyHandler < Base
  use LoggingMiddleware
  use AuthenticationMiddleware
end

Parameters:

  • middleware_class (Class)

    The middleware class to add



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

Parameters:

  • response (String, Hash, nil) (defaults to: nil)

    Optional response to include



139
140
141
# File 'lib/bolt_rb/handlers/base.rb', line 139

def ack(response = nil)
  context.ack(response)
end

#callvoid

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

#channelString?

Returns the channel ID from the context

Returns:

  • (String, nil)

    The channel ID



119
120
121
# File 'lib/bolt_rb/handlers/base.rb', line 119

def channel
  context.channel
end

#clientSlack::Web::Client

Returns the Slack Web API client

Returns:

  • (Slack::Web::Client)

    The API client



105
106
107
# File 'lib/bolt_rb/handlers/base.rb', line 105

def client
  context.client
end

#handleObject

The main handler logic

Subclasses must override this method to implement their behavior.

Raises:

  • (NotImplementedError)

    Always raises in the base class



167
168
169
# File 'lib/bolt_rb/handlers/base.rb', line 167

def handle
  raise NotImplementedError, 'Subclasses must implement #handle'
end

#payloadHash

Returns the raw payload from the context

Returns:

  • (Hash)

    The Slack event payload



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

Parameters:

  • message (String, Hash)

    The message to send

Returns:

  • (Net::HTTPResponse, nil)

    The HTTP response



149
150
151
# File 'lib/bolt_rb/handlers/base.rb', line 149

def respond(message)
  context.respond(message)
end

#say(message) ⇒ Hash

Posts a message to the channel

Delegates to Context#say

Parameters:

  • message (String, Hash)

    The message to post

Returns:

  • (Hash)

    The Slack API response



129
130
131
# File 'lib/bolt_rb/handlers/base.rb', line 129

def say(message)
  context.say(message)
end

#userString?

Returns the user ID from the context

Returns:

  • (String, nil)

    The user ID



112
113
114
# File 'lib/bolt_rb/handlers/base.rb', line 112

def user
  context.user
end