webhook_system 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d120e25bddd3b90b7f79a51f5ee17aaf9e161336
4
- data.tar.gz: e872d6ca6dad0879a5c71b9ca0ace3c129bc33c1
3
+ metadata.gz: 90f58d18463b764007411f0afe4d73c5bb57355b
4
+ data.tar.gz: cceb480b7c982ec3cf245583be982d6f774bd25c
5
5
  SHA512:
6
- metadata.gz: 793b9a205d0244f79aad3fad61ffd2aadbde2f65b16545acebb7c9192859a85d1f6aec764371905baf373eeaa8950e1cd31c8906f9a47a4a27370b550e672733
7
- data.tar.gz: fb7a2493ef2d3a666162c9eb3b4d1ce1b9cc8b1b58261869ec11a672514eb83b06458a874701dd602bdedf696df3cecdb87e5e2f510038884b578faddffaff3b
6
+ metadata.gz: f923b7323b1b3eef8a4d9ed1db3315c66e1ee96daaf6397a37d52fe171aef8696a73224a3f9edc2e6215223a5cea1b78156a39cc59eab379003e41c29a71702f
7
+ data.tar.gz: 97de701df22a41e2d5d3ec80204be6422c2a9a07c04f6a0c61f0bfe3237266c4de9310e754c43459e60336618b1347ed33a208ee22c28b183051666b966d9938
data/README.md CHANGED
@@ -203,12 +203,46 @@ more suitable for the actual notification payload.
203
203
  The general API for this is via:
204
204
 
205
205
  ```ruby
206
- WebhookSystem.dispatch(event_object)
206
+ WebhookSystem::Subscription.dispatch(event_object)
207
207
  ```
208
208
 
209
209
  This is meant to be fairly fire and forget. Internally this will create an ActiveJob for each subscription
210
210
  interested in the event.
211
211
 
212
+ ### Dispatching to Selected Subscriptions
213
+
214
+ There may be scenarios where you extended the Subscription model, and may need to only dispatch to a subset of subs.
215
+ For example, if you attached a relation to say Account. The `dispatch` method is actually defined to work with any
216
+ subscription relation. eg:
217
+
218
+ ```ruby
219
+ account = Account.find(1) # assume we have some model called Account
220
+ subs = account.webhook_subscriptions # and we added a column to webhook_subscriptions to accomodate this extra relation
221
+ subs.dispatch(some_event) # you can dispatch to just those subscriptions (it will filter for the specific ones)
222
+ # that are actually interested in the event
223
+ ```
224
+
225
+ ### Checking if any sub is interested
226
+
227
+ There may scenarios, where you really don't want to do some additional work unless you really have an event to dispatch.
228
+ You can check pretty quickly if there is any topics interested liks so:
229
+
230
+ ```ruby
231
+ if WebhookSystem::Subscription.interested_in_topic('some_topic').present?
232
+ # do some stuff
233
+ end
234
+ ```
235
+
236
+ This also works with selected subscriptions like in the example above:
237
+
238
+ ```ruby
239
+ account = Account.find(1) # assume we have some model called Account
240
+ subs = account.webhook_subscriptions # and we added a column to webhook_subscriptions to accomodate this extra relation
241
+ if subs.interested_in_topic('some_topic').present?
242
+ subs.dispatch(SomeEvent.build(some_expensive_function()))
243
+ end
244
+ ```
245
+
212
246
  # Payload Format
213
247
 
214
248
  Payloads can either be plain json or encrypted. On top of that, they're also signed. The format for the signature
@@ -13,12 +13,12 @@ module WebhookSystem
13
13
 
14
14
  def event_name
15
15
  mesg = "class #{self.class.name} must implement abstract method `#{self.class.name}#event_name()'."
16
- raise RuntimeError.new(mesg).tap { |err| err.backtrace = caller }
16
+ raise with_caller_backtrace(RuntimeError.new(mesg), 2)
17
17
  end
18
18
 
19
19
  def payload_attributes
20
20
  mesg = "class #{self.class.name} must implement abstract method `#{self.class.name}#payload_attributes()'."
21
- raise RuntimeError.new(mesg).tap { |err| err.backtrace = caller }
21
+ raise with_caller_backtrace(RuntimeError.new(mesg), 2)
22
22
  end
23
23
 
24
24
  def as_json
@@ -39,6 +39,11 @@ module WebhookSystem
39
39
 
40
40
  private
41
41
 
42
+ def with_caller_backtrace(exception, backtrack=2)
43
+ exception.set_backtrace(caller[backtrack..-1])
44
+ exception
45
+ end
46
+
42
47
  def validate_attribute_name(key)
43
48
  if self.class.key_is_reserved?(key)
44
49
  message = "#{self.class.name} should not be defining an attribute named #{key} since its reserved"
@@ -19,6 +19,16 @@ module WebhookSystem
19
19
 
20
20
  scope :interested_in_topic, -> (topic) { active.for_topic(topic) }
21
21
 
22
+ # Main invocation point for dispatching events, can either be called on the class
23
+ # or on a relation (ie a scoped down list of subs), will find applicable subs and dispatch to them
24
+ #
25
+ # @param [WebhookSystem::BaseEvent] event The Event Object
26
+ def self.dispatch(event)
27
+ interested_in_topic(event.event_name).each do |subscription|
28
+ WebhookSystem::Job.perform_later subscription, event.as_json
29
+ end
30
+ end
31
+
22
32
  # Just a helper to get a nice representation of the subscription
23
33
  def url_domain
24
34
  URI.parse(url).host
@@ -1,3 +1,3 @@
1
1
  module WebhookSystem
2
- VERSION = '2.0.0'
2
+ VERSION = '2.1.0'
3
3
  end
@@ -20,8 +20,4 @@ module WebhookSystem
20
20
  # Error raised when there is an issue with decoding the payload
21
21
  class DecodingError < RuntimeError
22
22
  end
23
-
24
- class << self
25
- delegate :dispatch, to: :'WebhookSystem::Dispatcher'
26
- end
27
23
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webhook_system
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Banasik
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2016-07-19 00:00:00.000000000 Z
12
+ date: 2016-07-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -286,7 +286,6 @@ files:
286
286
  - Rakefile
287
287
  - lib/webhook_system.rb
288
288
  - lib/webhook_system/base_event.rb
289
- - lib/webhook_system/dispatcher.rb
290
289
  - lib/webhook_system/encoder.rb
291
290
  - lib/webhook_system/event_log.rb
292
291
  - lib/webhook_system/job.rb
@@ -1,16 +0,0 @@
1
- module WebhookSystem
2
-
3
- # Main code that handles dispatching of events out to subscribers
4
- class Dispatcher
5
- class << self
6
-
7
- # @param [WebhookSystem::BaseEvent] event The Event Object
8
- def dispatch(event)
9
- WebhookSystem::Subscription.interested_in_topic(event.event_name).each do |subscription|
10
- WebhookSystem::Job.perform_later subscription, event.as_json
11
- end
12
- end
13
-
14
- end
15
- end
16
- end