class IntercomApp::WebhooksManager

Public Class Methods

new(params) click to toggle source
# File lib/intercom-app/webhooks_manager.rb, line 4
def initialize(params)
  @intercom_token = params[:intercom_token]
end

Public Instance Methods

create_webhooks_subscriptions() click to toggle source
# File lib/intercom-app/webhooks_manager.rb, line 8
def create_webhooks_subscriptions
  return unless required_webhooks.present?
  required_webhooks.each do |webhook|
    create_webhook_subscription(webhook) unless webhook_subscription_exists?(webhook[:topics])
  end
end
destroy_webhooks_subscriptions() click to toggle source
# File lib/intercom-app/webhooks_manager.rb, line 15
def destroy_webhooks_subscriptions
  intercom_client.subscriptions.all.each do |webhook|
    intercom_client.subscriptions.delete(webhook.id) if is_required_webhook?(webhook)
  end

  @current_webhooks_subscriptions = nil
end
recreate_webhooks_subscriptions!() click to toggle source
# File lib/intercom-app/webhooks_manager.rb, line 23
def recreate_webhooks_subscriptions!
  destroy_webhooks_subscriptions
  create_webhooks_subscriptions
end

Private Instance Methods

add_hub_secret_to_subscription(attributes) click to toggle source
# File lib/intercom-app/webhooks_manager.rb, line 55
def add_hub_secret_to_subscription(attributes)
  attributes[:hub_secret] = IntercomApp.configuration.hub_secret if IntercomApp.configuration.hub_secret.present?
end
create_webhook_subscription(attributes) click to toggle source
# File lib/intercom-app/webhooks_manager.rb, line 42
def create_webhook_subscription(attributes)
  add_hub_secret_to_subscription(attributes)
  webhook = intercom_client.subscriptions.create(attributes)
end
current_webhooks_subscriptions() click to toggle source
# File lib/intercom-app/webhooks_manager.rb, line 51
def current_webhooks_subscriptions
  @current_webhooks_subscriptions ||= intercom_client.subscriptions.all.index_by(&:topics)
end
intercom_client() click to toggle source
# File lib/intercom-app/webhooks_manager.rb, line 30
def intercom_client
  @intercom_client ||= Intercom::Client.new(app_id: @intercom_token, api_key: '')
end
is_required_webhook?(webhook) click to toggle source
# File lib/intercom-app/webhooks_manager.rb, line 38
def is_required_webhook?(webhook)
  required_webhooks.map{ |w| w[:url] }.include? webhook.url
end
required_webhooks() click to toggle source
# File lib/intercom-app/webhooks_manager.rb, line 34
def required_webhooks
  IntercomApp.configuration.webhooks
end
webhook_subscription_exists?(topics) click to toggle source
# File lib/intercom-app/webhooks_manager.rb, line 47
def webhook_subscription_exists?(topics)
  current_webhooks_subscriptions[topics]
end