easyhooks 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +201 -0
- data/README.md +374 -0
- data/lib/easyhooks/action.rb +37 -0
- data/lib/easyhooks/base.rb +22 -0
- data/lib/easyhooks/concerns/helpers.rb +35 -0
- data/lib/easyhooks/concerns/validators.rb +93 -0
- data/lib/easyhooks/hook.rb +8 -0
- data/lib/easyhooks/migration.rb +12 -0
- data/lib/easyhooks/post_processor.rb +101 -0
- data/lib/easyhooks/specification.rb +108 -0
- data/lib/easyhooks/store.rb +51 -0
- data/lib/easyhooks/store_values.rb +34 -0
- data/lib/easyhooks/trigger.rb +22 -0
- data/lib/easyhooks/version.rb +5 -0
- data/lib/easyhooks.rb +109 -0
- data/lib/generators/easyhooks/migration/migration_generator.rb +28 -0
- data/lib/generators/easyhooks/migration/templates/migration.rb +28 -0
- metadata +190 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 219561adbcf6276d6d1a582a09356af22440fd99b880951a927a408c009e4f89
|
4
|
+
data.tar.gz: b44708a1a93e6b22f82d7ec00e9ec2df7938af7d11257faed4103d2d9aeda58b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4f2c8992ac190e68c0c71b4f4f5364a6c2b12c48a407ad6a5fd785abf5855822f8145b03f118f70fc43e73f2dd4a53f88ee54279b8f2a12f18faa63b2fddfa34
|
7
|
+
data.tar.gz: 3a8e7fedb30c5b6ffe568c2c125e4e3e80930224ef98eaf6c3cd48ebbd2499dc131238ef6b17fabaadd11a01054dacd33920c2de683e4151a19c0a9b6fc9b547
|
data/LICENSE
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,374 @@
|
|
1
|
+
# Easyhooks - Webhooks made easy
|
2
|
+
|
3
|
+
Easyhooks is a ruby gem created to handle webhooks for Rails ActiveRecord instances. Simple, easy and fast.
|
4
|
+
You can use it to create webhooks for your models, and then use them to send data to your clients.
|
5
|
+
|
6
|
+
## Requirements
|
7
|
+
|
8
|
+
- Ruby 3.0 or newer
|
9
|
+
- Rails 6.1 or newer (including Rails 7.0)
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Include the gem in your Gemfile and run `bundle` to install it:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'easyhooks'
|
17
|
+
```
|
18
|
+
|
19
|
+
This step is not required if you don't want to store your hooks configuration in the database, but it's recommended.
|
20
|
+
|
21
|
+
```shell
|
22
|
+
rails g easyhooks:migration
|
23
|
+
rails db:migrate
|
24
|
+
```
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
### Defining the easiest hook
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
class User < ActiveRecord::Base
|
32
|
+
easyhooks do
|
33
|
+
trigger :approved do
|
34
|
+
action :my_first_action, endpoint: 'https://example.com'
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
The example above it's the simplest use. It will create a trigger called `approved` for the `User` model. Whenever a _user_ is **created**, **updated** or **deleted**,
|
41
|
+
the trigger `approved` dispatches an **ActiveJob** called `PostProcessor` to asynchronously send a **POST** request to the endpoint `https://example.com` with the following payload:
|
42
|
+
|
43
|
+
```json
|
44
|
+
{
|
45
|
+
"object": "User",
|
46
|
+
"action": "my_first_action",
|
47
|
+
"trigger": {
|
48
|
+
"name": "approved",
|
49
|
+
"event": "CREATE"
|
50
|
+
},
|
51
|
+
"data": {
|
52
|
+
"id": 1
|
53
|
+
}
|
54
|
+
}
|
55
|
+
```
|
56
|
+
|
57
|
+
Easy, no? Let's understand how everything works and see how to customize it even more.
|
58
|
+
|
59
|
+
### Trigger
|
60
|
+
|
61
|
+
A trigger is a way to define when a webhook should be dispatched. It can be defined by the following options:
|
62
|
+
* `:on` - Defines the events that will trigger the webhook. It can be `:create`, `:update` or `:destroy`. Defaults to `[:create, :update, :destroy]`.
|
63
|
+
* `:only` - Defines the attributes that will trigger the webhook. It can be a single attribute or an array of attributes. Defaults to `nil` (or any model changes). Example: `only: :name` or `only: [:name, :email]`.
|
64
|
+
|
65
|
+
**Note:** `:only` works only for `:update` events.
|
66
|
+
|
67
|
+
Example:
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
class User < ActiveRecord::Base
|
71
|
+
easyhooks do
|
72
|
+
trigger :approved, on: :update, only: :name do
|
73
|
+
action :my_first_action, endpoint: 'https://example.com'
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
```
|
78
|
+
|
79
|
+
Whenever in your codebase a `User` is updated and the `name` attribute is changed, the trigger `approved` will dispatch the action `my_first_action`.
|
80
|
+
|
81
|
+
### Action
|
82
|
+
|
83
|
+
An action is a way to define what should be done when a webhook is dispatched. It can be defined by the following options:
|
84
|
+
* `:endpoint` - Defines the endpoint that will receive the webhook data. It must be a valid URL.
|
85
|
+
* `:method` - Defines the HTTP method that will be used to send the webhook. It can be `:get`, `:post`, `:put`, `:patch` or `:delete`. Defaults to `:post`.
|
86
|
+
* `:headers` - Defines the headers that will be sent with the webhook. It must be a hash. Defaults to `{ 'Content-Type': 'application/json' }`.
|
87
|
+
* `:auth` - Defines the authentication that will be used to send with the webhook `Authorization` header. Is must be a string. Defaults to `nil`. Example: `Basic YWRtaW46cGFzc3dvcmQ=`.
|
88
|
+
|
89
|
+
Example:
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
class User < ActiveRecord::Base
|
93
|
+
easyhooks do
|
94
|
+
trigger :approved do
|
95
|
+
action :my_first_action, endpoint: 'https://example.com', method: :put, headers: { 'X-Easy': 'Easyhooks' }, auth: 'Basic YWRtaW46cGFzc3dvcmQ='
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
```
|
100
|
+
|
101
|
+
You can also define multiple actions for a single trigger:
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
class User < ActiveRecord::Base
|
105
|
+
easyhooks do
|
106
|
+
trigger :approved do
|
107
|
+
action :my_first_action, endpoint: 'https://example.com/first', method: put
|
108
|
+
action :my_second_action, endpoint: 'https://example.com/second', method: post
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
```
|
113
|
+
|
114
|
+
### Customizing the Payload
|
115
|
+
|
116
|
+
The payload is the data that will be sent to the endpoint. It can be defined by the following options in any easyhooks block like `trigger`, `action` or even `easyhooks`:
|
117
|
+
* `:payload` - Defines the payload that will be sent to the endpoint. It must be a symbol or a proc. Defaults to `{ id: model.id }`.
|
118
|
+
|
119
|
+
**Note:** If you define a payload in a `trigger` block, it will be used for all actions. If you define a payload in an `action` block, it will be used only for that action.
|
120
|
+
|
121
|
+
Example:
|
122
|
+
|
123
|
+
```ruby
|
124
|
+
class User < ActiveRecord::Base
|
125
|
+
easyhooks do
|
126
|
+
trigger :approved do
|
127
|
+
action :my_first_action, endpoint: 'https://example.com', payload: :my_payload
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
def my_payload
|
132
|
+
{ id: id, name: name }
|
133
|
+
end
|
134
|
+
end
|
135
|
+
```
|
136
|
+
JSON Payload:
|
137
|
+
```json
|
138
|
+
{
|
139
|
+
"object": "User",
|
140
|
+
"action": "my_first_action",
|
141
|
+
"trigger": {
|
142
|
+
"name": "approved",
|
143
|
+
"event": "CREATE"
|
144
|
+
},
|
145
|
+
"data": {
|
146
|
+
"id": 1,
|
147
|
+
"name": "John Doe"
|
148
|
+
}
|
149
|
+
}
|
150
|
+
```
|
151
|
+
|
152
|
+
### Adding conditions
|
153
|
+
|
154
|
+
You can add conditions to your triggers and actions. It can be defined by the following options:
|
155
|
+
* `:if` - Defines a condition that will be evaluated before dispatching the webhook. It must be a symbol or a proc. Defaults to `nil`.
|
156
|
+
|
157
|
+
**Note:** If you define a condition in a `trigger` block, it will be used for all actions. If you define a condition in an `action` block, it will be used only for that action.
|
158
|
+
|
159
|
+
Example:
|
160
|
+
|
161
|
+
```ruby
|
162
|
+
class User < ActiveRecord::Base
|
163
|
+
easyhooks do
|
164
|
+
trigger :approved, if: :my_condition do
|
165
|
+
action :my_first_action, endpoint: 'https://example.com'
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
def my_condition
|
170
|
+
name == 'John Doe'
|
171
|
+
end
|
172
|
+
end
|
173
|
+
```
|
174
|
+
|
175
|
+
### Accessing the webhook response data
|
176
|
+
|
177
|
+
You can access the webhook response data in your codebase. This will be useful if you want to do something with the response, like logging it.
|
178
|
+
|
179
|
+
**Note:** This callback will be called only if the webhook is successfully sent. Meaning that, if any error occurs while evaluating the webhook, this callback will not be called.
|
180
|
+
For failure callbacks, you can use the `:on_fail` option.
|
181
|
+
|
182
|
+
**Note 2:** The response object is an instance of [Net::HTTPResponse](https://ruby-doc.org/stdlib-3.0.0/libdoc/net/http/rdoc/Net/HTTPResponse.html).
|
183
|
+
|
184
|
+
Example:
|
185
|
+
|
186
|
+
```ruby
|
187
|
+
class User < ActiveRecord::Base
|
188
|
+
easyhooks do
|
189
|
+
trigger :approved do
|
190
|
+
action :my_first_action, endpoint: 'https://example.com' do |response|
|
191
|
+
puts response.code
|
192
|
+
puts response.body
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
196
|
+
end
|
197
|
+
```
|
198
|
+
|
199
|
+
### Handling webhook failures
|
200
|
+
|
201
|
+
You can handle webhook failures in your codebase. This will be useful let's say if the endpoint is down and you want to retry the webhook later.
|
202
|
+
You can define a `:on_fail` callback (symbol or proc) in any easyhooks block like `trigger` or `action`:
|
203
|
+
```ruby
|
204
|
+
class User < ActiveRecord::Base
|
205
|
+
easyhooks do
|
206
|
+
trigger :approved do
|
207
|
+
action :my_first_action, endpoint: 'https://example.com', on_fail: :my_callback
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
def my_callback
|
212
|
+
# Do something
|
213
|
+
end
|
214
|
+
end
|
215
|
+
```
|
216
|
+
|
217
|
+
### Global configuration
|
218
|
+
|
219
|
+
Defining endpoints, headers and auth for each action can be a little bit annoying. You can define a global configuration for all actions in your codebase.
|
220
|
+
There is three ways to do that:
|
221
|
+
* Using the `easyhooks` block
|
222
|
+
* Using an YAML file
|
223
|
+
* Using the database
|
224
|
+
|
225
|
+
#### Using the `easyhooks` block
|
226
|
+
|
227
|
+
You can define a global configuration for all actions in your codebase using the `easyhooks` block:
|
228
|
+
|
229
|
+
```ruby
|
230
|
+
class User < ActiveRecord::Base
|
231
|
+
easyhooks endpoint: 'https://example.com', auth: 'Bearer token' do
|
232
|
+
trigger :approved do
|
233
|
+
action :my_first_action, if: :my_condition
|
234
|
+
action :my_second_action, if: :my_second_condition
|
235
|
+
end
|
236
|
+
end
|
237
|
+
end
|
238
|
+
```
|
239
|
+
|
240
|
+
**Note:** Easyhooks prioritizes the configuration defined in the `action` block over the configuration defined in the `easyhooks` block:
|
241
|
+
* Order of priority: `action` > `trigger` > `easyhooks` > `yaml` > `database`.
|
242
|
+
|
243
|
+
Example:
|
244
|
+
```ruby
|
245
|
+
class User < ActiveRecord::Base
|
246
|
+
easyhooks endpoint: 'https://example.com' do
|
247
|
+
trigger :approved do
|
248
|
+
action :my_first_action, method: :put
|
249
|
+
action :my_second_action
|
250
|
+
end
|
251
|
+
end
|
252
|
+
end
|
253
|
+
```
|
254
|
+
|
255
|
+
In the example above, the `my_first_action` will be sent using the `PUT` method, while the `my_second_action` will be sent using the `POST` method.
|
256
|
+
You can combine any number of configurations in your codebase and Easyhooks handle.
|
257
|
+
|
258
|
+
#### Using an YAML file
|
259
|
+
|
260
|
+
You can define a global configuration for all actions in your codebase using an YAML file:
|
261
|
+
|
262
|
+
```yaml
|
263
|
+
# config/easyhooks.yml
|
264
|
+
development:
|
265
|
+
classes:
|
266
|
+
User:
|
267
|
+
endpoint: 'https://example.com'
|
268
|
+
method: :post
|
269
|
+
auth: 'Bearer token'
|
270
|
+
headers:
|
271
|
+
X-Easy: Easyhooks
|
272
|
+
triggers:
|
273
|
+
approved:
|
274
|
+
endpoint: 'https://example.com'
|
275
|
+
method: :patch
|
276
|
+
actions:
|
277
|
+
my_first_action:
|
278
|
+
endpoint: 'https://example.com'
|
279
|
+
method: :put
|
280
|
+
```
|
281
|
+
|
282
|
+
In the example above, we start configuring the hooks by environment.
|
283
|
+
An `action` should have a unique name and can be shared between classes. Same for `triggers`.
|
284
|
+
A `class` can have multiple trigger/actions and you can define a single configuration by class.
|
285
|
+
|
286
|
+
**Note:** The priority of the configurations defined in the YAML file is the same as mentioned before:
|
287
|
+
* Order of priority: `action` > `trigger` > `easyhooks` > `yaml` > `database`.
|
288
|
+
|
289
|
+
#### Using the database (Stored configuration)
|
290
|
+
|
291
|
+
You can define a global configuration for everything in your codebase using the database. For that you will need
|
292
|
+
to execute the migration generator and run the migration:
|
293
|
+
|
294
|
+
```shell
|
295
|
+
rails g easyhooks:migration
|
296
|
+
rails db:migrate
|
297
|
+
```
|
298
|
+
|
299
|
+
Define your models and hooks, but make sure to use the `:stored` option in the `easyhooks` block:
|
300
|
+
```ruby
|
301
|
+
class User < ActiveRecord::Base
|
302
|
+
easyhooks :stored do
|
303
|
+
trigger :approved do
|
304
|
+
action :my_first_action
|
305
|
+
end
|
306
|
+
end
|
307
|
+
end
|
308
|
+
```
|
309
|
+
Then, store the configuration in the database using the `Easyhooks::Store` model. Open the rails console and run:
|
310
|
+
```ruby
|
311
|
+
stored_action = Easyhooks::Store.create!(context: 'actions', name: 'my_first_action', endpoint: 'https://example.com', method: :put)
|
312
|
+
stored_action.add_headers({ 'X-Easy': 'Easyhooks' })
|
313
|
+
stored_action.add_auth('Bearer', 'token')
|
314
|
+
```
|
315
|
+
|
316
|
+
Using the database store will allow you to change the configuration without the need to restart your application, which
|
317
|
+
is pretty useful, let`s say, if you want to change the endpoint of a webhook that is broken, or the auth token expired.
|
318
|
+
|
319
|
+
The `context` attribute can be `actions`, `triggers` or `classes`.
|
320
|
+
The `name` attribute is the name of the action, trigger or class.
|
321
|
+
|
322
|
+
Here you can also override the configurations using the priority order mentioned before:
|
323
|
+
```ruby
|
324
|
+
class User < ActiveRecord::Base
|
325
|
+
easyhooks :stored do
|
326
|
+
trigger :approved do
|
327
|
+
action :my_first_action
|
328
|
+
action :my_second_action, method: :patch
|
329
|
+
end
|
330
|
+
end
|
331
|
+
end
|
332
|
+
```
|
333
|
+
|
334
|
+
You can also use the type `:stored` for blocks like `trigger` and `action`, and combine multiple rules:
|
335
|
+
```ruby
|
336
|
+
Easyhooks::Store.create!(context: 'triggers', name: 'approved', method: :patch, endpoint: 'https://example.com/users')
|
337
|
+
```
|
338
|
+
```ruby
|
339
|
+
class User < ActiveRecord::Base
|
340
|
+
easyhooks do
|
341
|
+
trigger :approved, type: :stored do
|
342
|
+
action :my_first_action, payload: :my_payload
|
343
|
+
action :another_action, method: :post
|
344
|
+
end
|
345
|
+
trigger :deleted, on: :destroy, payload: :my_other_payload, if: :condition do
|
346
|
+
action :my_second_action, endpoint: 'https://example.com/users/deleted'
|
347
|
+
end
|
348
|
+
end
|
349
|
+
end
|
350
|
+
```
|
351
|
+
|
352
|
+
## Conclusion
|
353
|
+
You can combine all the options mentioned above to create your own webhooks. Easyhooks is flexible and easy to use.
|
354
|
+
Be creative and have fun!
|
355
|
+
|
356
|
+
## Contributing
|
357
|
+
|
358
|
+
Bug reports and pull requests are welcome. This project is intended to be a safe, welcoming space for collaboration.
|
359
|
+
|
360
|
+
## Future improvements
|
361
|
+
|
362
|
+
- Add option to temporarily disable a trigger, action or class hook
|
363
|
+
- Add option to retry a webhook if it fails
|
364
|
+
- Add option to define a timeout for the webhook
|
365
|
+
- Rails generator to create database stored hooks
|
366
|
+
- Rails generator to create YAML stored hooks
|
367
|
+
|
368
|
+
|
369
|
+
## License
|
370
|
+
|
371
|
+
Apache License, Version 2.0.
|
372
|
+
See [LICENSE](https://apache.org/licenses/LICENSE-2.0.txt) for details.
|
373
|
+
|
374
|
+
Copyright (c) 2023-2023 [Thiago Bonfante](https://github.com/thiagobonfante)
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'easyhooks/store'
|
3
|
+
require 'easyhooks/base'
|
4
|
+
|
5
|
+
module Easyhooks
|
6
|
+
class Action < Easyhooks::Base
|
7
|
+
|
8
|
+
attr_accessor :trigger_name, :args, :on_fail_callable, :event_callable, :event
|
9
|
+
|
10
|
+
def initialize(name, trigger_name, hook, args = {}, &event)
|
11
|
+
super(name, args[:type], hook, args[:if], args[:payload], args[:on_fail])
|
12
|
+
@args = args
|
13
|
+
@trigger_name = trigger_name
|
14
|
+
@event_callable = "#{name}_event".to_sym
|
15
|
+
@on_fail_callable = "#{name}_on_fail".to_sym
|
16
|
+
@event = event
|
17
|
+
@hook = validate_hook(hook)
|
18
|
+
end
|
19
|
+
|
20
|
+
def load!(klass_name = nil)
|
21
|
+
return if self.type == :default
|
22
|
+
|
23
|
+
stored_action = Store.find_by(name: self.name, context: 'actions')
|
24
|
+
stored_action ||= Store.find_by(name: self.trigger_name, context: 'triggers')
|
25
|
+
stored_action ||= Store.find_by(name: klass_name, context: 'classes') if klass_name.present?
|
26
|
+
stored_action ||= Store.find_by(name: self.name, context: 'global')
|
27
|
+
|
28
|
+
raise "Action '#{self.name}' not found in database" unless stored_action.present?
|
29
|
+
|
30
|
+
# noinspection RubyArgCount
|
31
|
+
self.hook.method = validate_method(stored_action.method)
|
32
|
+
self.hook.endpoint = validate_endpoint(stored_action.endpoint)
|
33
|
+
self.hook.auth = validate_auth(stored_action.auth)
|
34
|
+
self.hook.headers = validate_headers(stored_action.headers)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'easyhooks/concerns/helpers'
|
4
|
+
require 'easyhooks/concerns/validators'
|
5
|
+
|
6
|
+
module Easyhooks
|
7
|
+
class Base
|
8
|
+
include Easyhooks::Helpers
|
9
|
+
include Easyhooks::Validators
|
10
|
+
|
11
|
+
attr_accessor :name, :type, :hook, :condition, :payload, :on_fail
|
12
|
+
|
13
|
+
def initialize(name, type, hook, condition, payload, on_fail)
|
14
|
+
@name = validate_name(name)
|
15
|
+
@type = validate_type(type)
|
16
|
+
@hook = hook
|
17
|
+
@condition = validate_callback(condition, 'if')
|
18
|
+
@payload = validate_callback(payload, 'payload')
|
19
|
+
@on_fail = validate_callback(on_fail, 'on_fail')
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/concern"
|
4
|
+
|
5
|
+
module Easyhooks
|
6
|
+
module Helpers
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
included do
|
10
|
+
def condition_applicable?(object)
|
11
|
+
if condition
|
12
|
+
if condition.is_a?(Symbol)
|
13
|
+
object.send(condition)
|
14
|
+
else
|
15
|
+
condition.call(object)
|
16
|
+
end
|
17
|
+
else
|
18
|
+
true
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def request_payload(object)
|
23
|
+
if payload
|
24
|
+
if payload.is_a?(Symbol)
|
25
|
+
object.send(payload)
|
26
|
+
else
|
27
|
+
payload.call(object)
|
28
|
+
end
|
29
|
+
else
|
30
|
+
{ id: object.id }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|