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

Changing the main dispatch interface to call of a relation, enabling filtering of the subscriptions being considered #10

Merged
merged 3 commits into from
Jul 20, 2016
Merged
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
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,46 @@ more suitable for the actual notification payload.
The general API for this is via:

```ruby
WebhookSystem.dispatch(event_object)
WebhookSystem::Subscription.dispatch(event_object)
```

This is meant to be fairly fire and forget. Internally this will create an ActiveJob for each subscription
interested in the event.

### Dispatching to Selected Subscriptions

There may be scenarios where you extended the Subscription model, and may need to only dispatch to a subset of subs.
For example, if you attached a relation to say Account. The `dispatch` method is actually defined to work with any
subscription relation. eg:

```ruby
account = Account.find(1) # assume we have some model called Account
subs = account.webhook_subscriptions # and we added a column to webhook_subscriptions to accomodate this extra relation
subs.dispatch(some_event) # you can dispatch to just those subscriptions (it will filter for the specific ones)
# that are actually interested in the event
```

### Checking if any sub is interested

There may scenarios, where you really don't want to do some additional work unless you really have an event to dispatch.
You can check pretty quickly if there is any topics interested liks so:

```ruby
if WebhookSystem::Subscription.interested_in_topic('some_topic').present?
# do some stuff
end
```

This also works with selected subscriptions like in the example above:

```ruby
account = Account.find(1) # assume we have some model called Account
subs = account.webhook_subscriptions # and we added a column to webhook_subscriptions to accomodate this extra relation
if subs.interested_in_topic('some_topic').present?
subs.dispatch(SomeEvent.build(some_expensive_function()))
end
```

# Payload Format

Payloads can either be plain json or encrypted. On top of that, they're also signed. The format for the signature
Expand Down
4 changes: 0 additions & 4 deletions lib/webhook_system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,4 @@ module WebhookSystem
# Error raised when there is an issue with decoding the payload
class DecodingError < RuntimeError
end

class << self
delegate :dispatch, to: :'WebhookSystem::Dispatcher'
end
end
9 changes: 7 additions & 2 deletions lib/webhook_system/base_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ def initialize(*args, &block)

def event_name
mesg = "class #{self.class.name} must implement abstract method `#{self.class.name}#event_name()'."
raise RuntimeError.new(mesg).tap { |err| err.backtrace = caller }
raise with_caller_backtrace(RuntimeError.new(mesg), 2)
end

def payload_attributes
mesg = "class #{self.class.name} must implement abstract method `#{self.class.name}#payload_attributes()'."
raise RuntimeError.new(mesg).tap { |err| err.backtrace = caller }
raise with_caller_backtrace(RuntimeError.new(mesg), 2)
end

def as_json
Expand All @@ -39,6 +39,11 @@ def self.key_is_reserved?(key)

private

def with_caller_backtrace(exception, backtrack=2)
exception.set_backtrace(caller[backtrack..-1])
exception
end

def validate_attribute_name(key)
if self.class.key_is_reserved?(key)
message = "#{self.class.name} should not be defining an attribute named #{key} since its reserved"
Expand Down
16 changes: 0 additions & 16 deletions lib/webhook_system/dispatcher.rb

This file was deleted.

10 changes: 10 additions & 0 deletions lib/webhook_system/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ class Subscription < ActiveRecord::Base

scope :interested_in_topic, -> (topic) { active.for_topic(topic) }

# Main invocation point for dispatching events, can either be called on the class
# or on a relation (ie a scoped down list of subs), will find applicable subs and dispatch to them
#
# @param [WebhookSystem::BaseEvent] event The Event Object
def self.dispatch(event)
interested_in_topic(event.event_name).each do |subscription|
WebhookSystem::Job.perform_later subscription, event.as_json
end
end

# Just a helper to get a nice representation of the subscription
def url_domain
URI.parse(url).host
Expand Down
2 changes: 1 addition & 1 deletion lib/webhook_system/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module WebhookSystem
VERSION = '2.0.0'
VERSION = '2.1.0'
end
8 changes: 4 additions & 4 deletions spec/dispatch_spec.rb → spec/dispatching_events_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'spec_helper'

describe WebhookSystem, aggregate_failures: true, db: true do
describe 'dispatching events', aggregate_failures: true, db: true do
describe 'dispatching' do
let!(:subscription1) do
create(:webhook_subscription, :active, :encrypted, :with_topics, url: 'http://lvh.me/hook1', topics: ['other_event'])
Expand Down Expand Up @@ -45,7 +45,7 @@ def payload_attributes

expect {
perform_enqueued_jobs do
described_class.dispatch event
WebhookSystem::Subscription.dispatch event
end
}.to change { subscription1.event_logs.count }.by(1)

Expand All @@ -68,7 +68,7 @@ def payload_attributes
expect {
expect {
perform_enqueued_jobs do
described_class.dispatch event
WebhookSystem::Subscription.dispatch event
end
}.to change { subscription1.event_logs.count }.by(1)
}.to raise_exception(WebhookSystem::Job::RequestFailed, 'request failed with code: 400')
Expand All @@ -90,7 +90,7 @@ def payload_attributes
expect {
expect {
perform_enqueued_jobs do
described_class.dispatch event
WebhookSystem::Subscription.dispatch event
end
}.to change { subscription1.event_logs.count }.by(1)
}.to raise_exception(WebhookSystem::Job::RequestFailed, 'request failed with code: 0')
Expand Down
2 changes: 1 addition & 1 deletion spec/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def handle_webhook(to:)
end

perform_enqueued_jobs do
WebhookSystem.dispatch event
WebhookSystem::Subscription.dispatch event
end

expect(hooks_called).to match_array([:hook1, :hook2])
Expand Down