class ShopifyGraphql::WebhooksManager

Attributes

required_webhooks[R]

Public Class Methods

new(webhooks) click to toggle source
# File lib/shopify_graphql/managers/webhooks_manager.rb, line 28
def initialize(webhooks)
  @required_webhooks = webhooks
end
queue_create(shop_domain, shop_token) click to toggle source
# File lib/shopify_graphql/managers/webhooks_manager.rb, line 4
def queue_create(shop_domain, shop_token)
  ShopifyGraphql::CreateWebhooksJob.perform_later(
    shop_domain: shop_domain,
    shop_token: shop_token,
  )
end
queue_destroy(shop_domain, shop_token) click to toggle source
# File lib/shopify_graphql/managers/webhooks_manager.rb, line 11
def queue_destroy(shop_domain, shop_token)
  ShopifyGraphql::DestroyWebhooksJob.perform_later(
    shop_domain: shop_domain,
    shop_token: shop_token,
  )
end
queue_update(shop_domain, shop_token) click to toggle source
# File lib/shopify_graphql/managers/webhooks_manager.rb, line 18
def queue_update(shop_domain, shop_token)
  ShopifyGraphql::UpdateWebhooksJob.perform_later(
    shop_domain: shop_domain,
    shop_token: shop_token,
  )
end

Public Instance Methods

create_webhooks() click to toggle source
# File lib/shopify_graphql/managers/webhooks_manager.rb, line 37
def create_webhooks
  return unless webhooks_enabled?
  return unless required_webhooks.present?

  required_webhooks.each do |webhook|
    create_webhook(webhook) unless webhook_exists?(webhook[:topic])
  end
end
destroy_webhooks() click to toggle source
# File lib/shopify_graphql/managers/webhooks_manager.rb, line 46
def destroy_webhooks
  return unless webhooks_enabled?

  current_webhooks.each do |webhook|
    ShopifyGraphql::Webhook.delete(webhook.id)
  end

  @current_webhooks = nil
end
recreate_webhooks!() click to toggle source
# File lib/shopify_graphql/managers/webhooks_manager.rb, line 32
def recreate_webhooks!
  destroy_webhooks
  create_webhooks
end

Private Instance Methods

create_webhook(attributes) click to toggle source
# File lib/shopify_graphql/managers/webhooks_manager.rb, line 71
def create_webhook(attributes)
  ShopifyGraphql::Webhook.create(
    topic: attributes[:topic],
    address: attributes[:address],
    include_fields: attributes[:include_fields],
  )
end
current_webhooks() click to toggle source
# File lib/shopify_graphql/managers/webhooks_manager.rb, line 85
def current_webhooks
  @current_webhooks ||= ShopifyGraphql::Webhook.all
end
webhook_environments() click to toggle source
# File lib/shopify_graphql/managers/webhooks_manager.rb, line 58
def webhook_environments
  ShopifyGraphql.configuration.webhook_enabled_environments
end
webhook_exists?(topic) click to toggle source
# File lib/shopify_graphql/managers/webhooks_manager.rb, line 79
def webhook_exists?(topic)
  current_webhooks.any? do |webhook|
    webhook.topic == topic
  end
end
webhooks_enabled?() click to toggle source
# File lib/shopify_graphql/managers/webhooks_manager.rb, line 62
def webhooks_enabled?
  if webhook_environments.include?(Rails.env) || ActiveModel::Type::Boolean.new.cast(ENV["WEBHOOKS_ENABLED"])
    true
  else
    Rails.logger.info("[ShopifyGraphql] Webhooks disabled in #{Rails.env} environment. Check you config.")
    false
  end
end