Module: JetEmail::Webhooks
- Defined in:
- lib/jetemail/webhooks.rb
Class Method Summary collapse
-
.create(params) ⇒ Hash
Create a webhook.
-
.get(uuid) ⇒ Hash
Get a single webhook.
-
.list ⇒ Hash
List all webhooks.
-
.query(params = {}) ⇒ Hash
Query webhook events.
-
.remove(uuid) ⇒ Hash
Delete a webhook.
-
.replay(params) ⇒ Hash
Replay a webhook event.
-
.update(params) ⇒ Hash
Update a webhook.
-
.verify(payload:, signature:, timestamp:, secret:, tolerance: WEBHOOK_TOLERANCE_SECONDS) ⇒ Boolean
Verify a webhook signature.
Class Method Details
.create(params) ⇒ Hash
Create a webhook.
34 35 36 37 |
# File 'lib/jetemail/webhooks.rb', line 34 def create(params) path = "webhooks" JetEmail::Request.new(path, params, "post").perform end |
.get(uuid) ⇒ Hash
Get a single webhook.
22 23 24 25 |
# File 'lib/jetemail/webhooks.rb', line 22 def get(uuid) path = "webhooks/#{uuid}" JetEmail::Request.new(path, {}, "get").perform end |
.list ⇒ Hash
List all webhooks.
13 14 15 16 |
# File 'lib/jetemail/webhooks.rb', line 13 def list path = "webhooks" JetEmail::Request.new(path, {}, "get").perform end |
.query(params = {}) ⇒ Hash
Query webhook events.
62 63 64 65 |
# File 'lib/jetemail/webhooks.rb', line 62 def query(params = {}) path = "webhooks/query" JetEmail::Request.new(path, params, "post").perform end |
.remove(uuid) ⇒ Hash
Delete a webhook.
53 54 55 56 |
# File 'lib/jetemail/webhooks.rb', line 53 def remove(uuid) path = "webhooks/#{uuid}" JetEmail::Request.new(path, {}, "delete").perform end |
.replay(params) ⇒ Hash
Replay a webhook event.
73 74 75 76 |
# File 'lib/jetemail/webhooks.rb', line 73 def replay(params) path = "webhooks/replay" JetEmail::Request.new(path, params, "post").perform end |
.update(params) ⇒ Hash
Update a webhook.
44 45 46 47 |
# File 'lib/jetemail/webhooks.rb', line 44 def update(params) path = "webhooks" JetEmail::Request.new(path, params, "patch").perform end |
.verify(payload:, signature:, timestamp:, secret:, tolerance: WEBHOOK_TOLERANCE_SECONDS) ⇒ Boolean
Verify a webhook signature.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/jetemail/webhooks.rb', line 87 def verify(payload:, signature:, timestamp:, secret:, tolerance: WEBHOOK_TOLERANCE_SECONDS) raise JetEmail::Error.new("Payload cannot be empty") if payload.nil? || payload.empty? raise JetEmail::Error.new("Signature cannot be empty") if signature.nil? || signature.empty? raise JetEmail::Error.new("Timestamp cannot be empty") if .nil? || .to_s.empty? raise JetEmail::Error.new("Secret cannot be empty") if secret.nil? || secret.empty? ts = .to_i diff = (Time.now.to_i - ts).abs if tolerance > 0 && diff > tolerance raise JetEmail::Error.new("Webhook timestamp is outside tolerance (#{diff}s)") end expected = "sha256=" + OpenSSL::HMAC.hexdigest("SHA256", secret, payload) unless secure_compare(expected, signature) raise JetEmail::Error.new("Webhook signature verification failed") end true end |