Class: WebhookSystem::Subscription

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/webhook_system/subscription.rb

Overview

This is the model encompassing the actual record of a webhook subscription

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.dispatch(event) ⇒ Object

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

Parameters:



26
27
28
29
30
# File 'lib/webhook_system/subscription.rb', line 26

def self.dispatch(event)
  interested_in_topic(event.event_name).each do |subscription|
    WebhookSystem::Job.perform_later subscription, event.as_json
  end
end

Instance Method Details

#topic_namesObject

Abstraction around the topics relation, returns an array of the subscribed topic names



38
39
40
# File 'lib/webhook_system/subscription.rb', line 38

def topic_names
  topics.map(&:name)
end

#topic_names=(new_topics) ⇒ Object

Abstraction around the topics relation, sets the topic names, requires save to take effect



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/webhook_system/subscription.rb', line 43

def topic_names=(new_topics)
  new_topics.reject!(&:blank?)
  add_topics = new_topics - topic_names

  new_topics_attributes = []

  topics.each do |topic|
    new_topics_attributes << {
      id: topic.id,
      name: topic.name,
      _destroy: !new_topics.include?(topic.name),
    }
  end

  new_topics_attributes += add_topics.map { |topic| { name: topic } }

  self.topics_attributes = new_topics_attributes
end

#url_domainObject

Just a helper to get a nice representation of the subscription



33
34
35
# File 'lib/webhook_system/subscription.rb', line 33

def url_domain
  URI.parse(url).host
end