Skip to content

Commit 2a4f904

Browse files
committed
update docs
1 parent 3344477 commit 2a4f904

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

docs/handler_plugins.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,66 @@ class Example < Hooks::Plugins::Handlers::Base
2727
end
2828
```
2929

30+
### `payload` Parameter
31+
32+
The `payload` parameter can be a Hash or a String. If the payload is a String, it will be parsed as JSON. If it is a Hash, it will be passed directly to the handler. The payload can contain any data that the webhook sender wants to send.
33+
34+
By default, the payload is parsed as JSON (if it can be) and then symbolized. This means that the keys in the payload will be converted to symbols. You can disable this auto-symbolization of the payload by setting the environment variable `HOOKS_SYMBOLIZE_PAYLOAD` to `false` or by setting the `symbolize_payload` option to `false` in the global configuration file.
35+
36+
**TL;DR**: The payload is almost always a Hash with symbolized keys, regardless of whether the original payload was a Hash or a JSON String.
37+
38+
For example, if the client sends the following JSON payload:
39+
40+
```json
41+
{
42+
"hello": "world",
43+
"foo": ["bar", "baz"],
44+
"truthy": true,
45+
"coffee": {"is": "good"}
46+
}
47+
```
48+
49+
It will be parsed and passed to the handler as:
50+
51+
```ruby
52+
{
53+
hello: "world",
54+
foo: ["bar", "baz"],
55+
truthy: true,
56+
coffee: {is: "good"}
57+
}
58+
```
59+
60+
### `headers` Parameter
61+
62+
The `headers` parameter is a Hash that contains the HTTP headers that were sent with the webhook request. It includes standard headers like `host`, `user-agent`, `accept`, and any custom headers that the webhook sender may have included.
63+
64+
Here is an example of what the `headers` parameter might look like:
65+
66+
```ruby
67+
# example headers as a Hash
68+
{
69+
"host" => "<HOSTNAME>", # e.g., "hooks.example.com"
70+
"user-agent" => "foo-client/1.0",
71+
"accept" => "application/json, text/plain, */*",
72+
"accept-encoding" => "gzip, compress, deflate, br",
73+
"client-name" => "foo",
74+
"x-forwarded-for" => "<IP_ADDRESS>",
75+
"x-forwarded-host" => "<HOSTNAME>", # e.g., "hooks.example.com"
76+
"x-forwarded-proto" => "https",
77+
"version" => "HTTP/1.1",
78+
"Authorization" => "Bearer <TOKEN>" # a careful reminder that headers *can* contain sensitive information!
79+
}
80+
```
81+
82+
It should be noted that the `headers` parameter is a Hash with **String keys** (not symbols). They are also normalized (lowercased and trimmed) to ensure consistency.
83+
84+
You can disable this normalization by either setting the environment variable `HOOKS_NORMALIZE_HEADERS` to `false` or by setting the `normalize_headers` option to `false` in the global configuration file.
85+
86+
### `config` Parameter
87+
88+
The `config` parameter is a Hash (symbolized) that contains the endpoint configuration. This can include any additional settings or parameters that you want to use in your handler. Most of the time, this won't be used, but sometimes endpoint configs add `opts` that can be useful for the handler.
89+
3090
## Built-in Features
3191

3292
This section goes into details on the built-in features that exist in all handler plugins that extend the `Hooks::Plugins::Handlers::Base` class.

lib/hooks/plugins/handlers/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Base
1616
#
1717
# @param payload [Hash, String] Parsed request body (JSON Hash) or raw string
1818
# @param headers [Hash<String, String>] HTTP headers
19-
# @param config [Hash] Merged endpoint configuration including opts section
19+
# @param config [Hash] Merged endpoint configuration including opts section (symbolized keys)
2020
# @return [Hash, String, nil] Response body (will be auto-converted to JSON)
2121
# @raise [NotImplementedError] if not implemented by subclass
2222
def call(payload:, headers:, config:)

0 commit comments

Comments
 (0)