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

Constant Summary collapse

INLINE_JOB_REGEXP =
/^inline:(.*)/.freeze

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:



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

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

#account_infoObject



71
72
73
74
75
76
77
# File 'lib/webhook_system/subscription.rb', line 71

def 
  if defined?(Account)
    "#{}:#{.try(:name)}"
  else
    .to_s
  end
end

#topic_namesObject

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



47
48
49
# File 'lib/webhook_system/subscription.rb', line 47

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/webhook_system/subscription.rb', line 52

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.exclude?(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



38
39
40
41
42
43
44
# File 'lib/webhook_system/subscription.rb', line 38

def url_domain
  if data = url.match(INLINE_JOB_REGEXP)
    data[1]
  else
    URI.parse(url).host
  end
end