Sha256: 67499858dcb0c7f12f79ab2ba4abafd8006b498811f372729ce047b5a5276445
Contents?: true
Size: 1.53 KB
Versions: 3
Compression:
Stored size: 1.53 KB
Contents
# frozen_string_literal: true module DuodealerApp class WebhooksManager class CreationFailed < StandardError; end def self.queue(account_domain, account_token, webhooks) DuodealerApp::WebhooksManagerJob.perform_later( account_domain: account_domain, account_token: account_token, webhooks: webhooks ) end attr_reader :required_webhooks def initialize(webhooks) @required_webhooks = webhooks end def recreate_webhooks! destroy_webhooks create_webhooks end def create_webhooks return if required_webhooks.blank? required_webhooks.each do |webhook| create_webhook(webhook) unless webhook_exists?(webhook[:topic]) end end def destroy_webhooks DuodealerAPI::Webhook.all.to_a.each do |webhook| DuodealerAPI::Webhook.delete(webhook.id) if is_required_webhook?(webhook) end @current_webhooks = nil end private def is_required_webhook?(webhook) required_webhooks.map { |w| w[:address] }.include? webhook.address end def create_webhook(attributes) attributes.reverse_merge!(format: "json") webhook = DuodealerAPI::Webhook.create(attributes) raise CreationFailed, webhook.errors.full_messages.to_sentence unless webhook.persisted? webhook end def webhook_exists?(topic) current_webhooks[topic] end def current_webhooks @current_webhooks ||= DuodealerAPI::Webhook.all.to_a.index_by(&:topic) end end end
Version data entries
3 entries across 3 versions & 1 rubygems