Skip to content

Commit 97622f4

Browse files
committed
refactor: migrate handlers to plugins architecture
1 parent 652907a commit 97622f4

File tree

18 files changed

+63
-62
lines changed

18 files changed

+63
-62
lines changed

docs/design.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Note: The `hooks` gem name is already taken on RubyGems, so this project is name
3232

3333
2. **Plugin Architecture**
3434

35-
* **Team Handlers**: `class MyHandler < Hooks::Handlers::Base`
35+
* **Team Handlers**: `class MyHandler < Hooks::Plugins::Handlers::Base`
3636
* Must implement `#call(payload:, headers:, config:)` method
3737
* `payload`: parsed request body (JSON Hash or raw String)
3838
* `headers`: HTTP headers as Hash with string keys
@@ -142,7 +142,7 @@ lib/hooks/
142142
│ ├── logger_factory.rb # Structured JSON logger + context enrichment
143143
144144
├── handlers/
145-
│ └── base.rb # `Hooks::Handlers::Base` interface: defines #call
145+
│ └── base.rb # `Hooks::Plugins::Handlers::Base` interface: defines #call
146146
147147
├── plugins/
148148
│ ├── lifecycle.rb # `Hooks::Plugins::Lifecycle` hooks (on_request, response, error)
@@ -520,12 +520,12 @@ The health endpoint provides comprehensive status information for load balancers
520520

521521
### Core Classes
522522

523-
#### `Hooks::Handlers::Base`
523+
#### `Hooks::Plugins::Handlers::Base`
524524

525525
Base class for all webhook handlers.
526526

527527
```ruby
528-
class MyHandler < Hooks::Handlers::Base
528+
class MyHandler < Hooks::Plugins::Handlers::Base
529529
# @param payload [Hash, String] Parsed request body or raw string
530530
# @param headers [Hash<String, String>] HTTP headers
531531
# @param config [Hash] Merged endpoint configuration

lib/hooks.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
require_relative "hooks/version"
44
require_relative "hooks/core/builder"
5-
require_relative "hooks/handlers/base"
65

7-
# Load all plugins (request validators, lifecycle hooks, etc.)
6+
# Load all plugins (auth plugins, handler plugins, lifecycle hooks, etc.)
87
Dir[File.join(__dir__, "hooks/plugins/**/*.rb")].sort.each do |file|
98
require file
109
end

lib/hooks/app/api.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
require "securerandom"
66
require_relative "helpers"
77
require_relative "auth/auth"
8-
require_relative "../handlers/base"
9-
require_relative "../handlers/default"
8+
require_relative "../plugins/handlers/base"
9+
require_relative "../plugins/handlers/default"
1010
require_relative "../core/logger_factory"
1111
require_relative "../core/log"
1212

lib/hooks/app/endpoints/catchall.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
require "grape"
4-
require_relative "../../handlers/default"
4+
require_relative "../../plugins/handlers/default"
55
require_relative "../helpers"
66

77
module Hooks

lib/hooks/app/helpers.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ def load_handler(handler_class_name, handler_dir)
9292
handler_class = Object.const_get(handler_class_name)
9393

9494
# Security: Ensure the loaded class inherits from the expected base class
95-
unless handler_class < Hooks::Handlers::Base
96-
error!("handler class must inherit from Hooks::Handlers::Base", 400)
95+
unless handler_class < Hooks::Plugins::Handlers::Base
96+
error!("handler class must inherit from Hooks::Plugins::Handlers::Base", 400)
9797
end
9898

9999
handler_class.new

lib/hooks/handlers/base.rb

Lines changed: 0 additions & 33 deletions
This file was deleted.

lib/hooks/plugins/handlers/base.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# frozen_string_literal: true
2+
3+
module Hooks
4+
module Plugins
5+
module Handlers
6+
# Base class for all webhook handlers
7+
#
8+
# All custom handlers must inherit from this class and implement the #call method
9+
class Base
10+
# Process a webhook request
11+
#
12+
# @param payload [Hash, String] Parsed request body (JSON Hash) or raw string
13+
# @param headers [Hash<String, String>] HTTP headers
14+
# @param config [Hash] Merged endpoint configuration including opts section
15+
# @return [Hash, String, nil] Response body (will be auto-converted to JSON)
16+
# @raise [NotImplementedError] if not implemented by subclass
17+
def call(payload:, headers:, config:)
18+
raise NotImplementedError, "Handler must implement #call method"
19+
end
20+
21+
# Short logger accessor for all subclasses
22+
# @return [Hooks::Log] Logger instance
23+
#
24+
# Provides a convenient way for handlers to log messages without needing
25+
# to reference the full Hooks::Log namespace.
26+
#
27+
# @example Logging an error in an inherited class
28+
# log.error("oh no an error occured")
29+
def log
30+
Hooks::Log.instance
31+
end
32+
end
33+
end
34+
end
35+
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Default handler when no custom handler is found
44
# This handler simply acknowledges receipt of the webhook and shows a few of the built-in features
5-
class DefaultHandler < Hooks::Handlers::Base
5+
class DefaultHandler < Hooks::Plugins::Handlers::Base
66
def call(payload:, headers:, config:)
77

88
log.info("🔔 Default handler invoked for webhook 🔔")

spec/acceptance/plugins/handlers/github_handler.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
# Example handler for GitHub webhooks
4-
class GithubHandler < Hooks::Handlers::Base
4+
class GithubHandler < Hooks::Plugins::Handlers::Base
55
# Process GitHub webhook
66
#
77
# @param payload [Hash, String] GitHub webhook payload

spec/acceptance/plugins/handlers/okta_handler.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
class OktaHandler < Hooks::Handlers::Base
3+
class OktaHandler < Hooks::Plugins::Handlers::Base
44
def call(payload:, headers:, config:)
55
return {
66
status: "success"

0 commit comments

Comments
 (0)