class WebhookSystem::Subscription

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

Constants

INLINE_JOB_REGEXP

Public Class Methods

dispatch(event) click to toggle source

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

# 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

Public Instance Methods

account_info() click to toggle source
# File lib/webhook_system/subscription.rb, line 71
def account_info
  if defined?(Account)
    "#{account_id}:#{account.try(:name)}"
  else
    account_id.to_s
  end
end
topic_names() click to toggle source

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

# File lib/webhook_system/subscription.rb, line 47
def topic_names
  topics.map(&:name)
end
topic_names=(new_topics) click to toggle source

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

# 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_domain() click to toggle source

Just a helper to get a nice representation of the subscription

# 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