Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds webhook delivery method #238

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Currently, we support these notification delivery methods out of the box:
* Vonage / Nexmo (SMS)
* iOS Apple Push Notifications
* Firebase Cloud Messaging (Android and more)
* Webhook

And you can easily add new notification types for any other delivery methods.

Expand Down Expand Up @@ -252,6 +253,7 @@ For example, emails will require a subject, body, and email address while an SMS
* [Twilio](docs/delivery_methods/twilio.md)
* [Vonage](docs/delivery_methods/vonage.md)
* [Firebase Cloud Messaging](docs/delivery_methods/fcm.md)
* [Webhook](docs/delivery_methods/webhook.md)

### Fallback Notifications

Expand Down
17 changes: 17 additions & 0 deletions docs/delivery_methods/webhook.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
### Webhook Delivery Method

Sends a notification via webhook.

`deliver_by :webhook, url: "https://webhook.site/0090u9-989238u-23898u-1823"`

##### Options

* `format: :format_for_webhook` - *Optional*

Use a custom method to define the payload sent to webhook URL. Method should return a Hash.

* `url: :url_for_webhook` - **Required**

Use a custom method to retrieve the Webhook URL. Method should return a String.


1 change: 1 addition & 0 deletions lib/noticed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module DeliveryMethods
autoload :Test, "noticed/delivery_methods/test"
autoload :Twilio, "noticed/delivery_methods/twilio"
autoload :Vonage, "noticed/delivery_methods/vonage"
autoload :Webhook, "noticed/delivery_methods/webhook"
end

mattr_accessor :parent_class
Expand Down
27 changes: 27 additions & 0 deletions lib/noticed/delivery_methods/webhook.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Noticed
module DeliveryMethods
class Webhook < Base
option :url

def deliver
post(url, json: format)
end

private

def format
if (method = options[:format])
notification.send(method)
else
notification.params
end
end

def url
if (method = options[:url])
notification.send(method)
end
end
end
end
end
47 changes: 47 additions & 0 deletions test/delivery_methods/webhook_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require "test_helper"

class WebhookTest < ActiveSupport::TestCase
class WebhookExampleWithoutWebhookUrl < Noticed::Base
deliver_by :webhook, debug: true
end

class WebhookExample < Noticed::Base
deliver_by :webhook, debug: true, url: :webhook_url

def webhook_url
"https://webhook.site/8c6ed375-d871-41b9-9536-e01b47d6b20b"
end
end

test "sends a POST to Webhook link" do
stub_delivery_method_request(delivery_method: :webhook, matcher: /webhook.site/)
WebhookExample.new.deliver(user)
end

test "raises an error when http request fails" do
stub_delivery_method_request(delivery_method: :webhook, matcher: /webhook.site/, type: :failure)
e = assert_raises(::Noticed::ResponseUnsuccessful) {
WebhookExample.new.deliver(user)
}
assert_equal HTTP::Response, e.response.class
end

test "deliver returns an http response" do
stub_delivery_method_request(delivery_method: :webhook, matcher: /webhook.site/)

args = {
notification_class: "::WebhookTest::WebhookExample",
recipient: user,
options: {url: :webhook_url}
}
response = Noticed::DeliveryMethods::Webhook.new.perform(args)

assert_kind_of HTTP::Response, response
end

test "validates webhook url is specified for webhook delivery method" do
assert_raises Noticed::ValidationError do
WebhookExampleWithoutWebhookUrl.new.deliver(user)
end
end
end
10 changes: 10 additions & 0 deletions test/fixtures/files/webhook/failure.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
HTTP/1.1 403
date: Mon, 09 Nov 2020 12:14:30 GMT
server: Apache
strict-transport-security: max-age=31536000; includeSubDomains; preload
access-control-allow-origin: *
x-frame-options: SAMEORIGIN
referrer-policy: no-referrer
vary: Accept-Encoding
content-type: text/html
x-via: haproxy-www-2n6w,haproxy-edge-fra-9k3b
12 changes: 12 additions & 0 deletions test/fixtures/files/webhook/success.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
HTTP/1.1 200
date: Mon, 09 Nov 2020 12:14:30 GMT
server: Apache
strict-transport-security: max-age=31536000; includeSubDomains; preload
access-control-allow-origin: *
x-frame-options: SAMEORIGIN
referrer-policy: no-referrer
vary: Accept-Encoding
content-type: text/html
x-via: haproxy-www-2n6w,haproxy-edge-fra-9k3b

ok