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

Instance Method Summary collapse

Instance Method Details

#topic_names ⇒ Object

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



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

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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/webhook_system/subscription.rb', line 33

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_domain ⇒ Object

Just a helper to get a nice representation of the subscription



23
24
25
# File 'lib/webhook_system/subscription.rb', line 23

def url_domain
  URI.parse(url).host
end