Sha256: 0c4d1359ccde24fdb3fcd269054296e4d03d41e32b90a5541b16e721f279c4f7

Contents?: true

Size: 1.83 KB

Versions: 12

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

require "rails/generators/base"

module ShopifyApp
  module Generators
    class AddWebhookGenerator < Rails::Generators::Base
      source_root File.expand_path("../templates", __FILE__)
      class_option :topic, type: :string, aliases: "-t", required: true
      class_option :path, type: :string, aliases: "-p", required: true

      hook_for :test_framework, as: :job, in: :rails do |instance, generator|
        instance.invoke(generator, [instance.send(:job_file_name)])
      end

      def init_webhook_config
        initializer = load_initializer
        return if initializer.include?("config.webhooks")

        inject_into_file(
          "config/initializers/shopify_app.rb",
          "  config.webhooks = [\n  ]\n",
          after: /ShopifyApp\.configure.*\n/,
        )
      end

      def inject_webhook_to_shopify_app_initializer
        inject_into_file(
          "config/initializers/shopify_app.rb",
          webhook_config,
          after: "config.webhooks = [",
        )

        initializer = load_initializer

        unless initializer.include?(webhook_config)
          shell.say("Error adding webhook to config. Add this line manually: #{webhook_config}", :red)
        end
      end

      def add_webhook_job
        @job_file_name = job_file_name + "_job"
        @job_class_name = @job_file_name.classify
        template("webhook_job.rb", "app/jobs/#{@job_file_name}.rb")
      end

      private

      def job_file_name
        path.split("/").last
      end

      def load_initializer
        File.read(File.join(destination_root, "config/initializers/shopify_app.rb"))
      end

      def webhook_config
        "\n    { topic: \"#{topic}\", path: \"#{path}\" },"
      end

      def topic
        options["topic"]
      end

      def path
        options["path"]
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
shopify_app-21.8.1 lib/generators/shopify_app/add_webhook/add_webhook_generator.rb
shopify_app-21.8.0 lib/generators/shopify_app/add_webhook/add_webhook_generator.rb
shopify_app-21.7.0 lib/generators/shopify_app/add_webhook/add_webhook_generator.rb
shopify_app-21.6.0 lib/generators/shopify_app/add_webhook/add_webhook_generator.rb
shopify_app-21.5.0 lib/generators/shopify_app/add_webhook/add_webhook_generator.rb
shopify_app-21.4.1 lib/generators/shopify_app/add_webhook/add_webhook_generator.rb
shopify_app-21.4.0 lib/generators/shopify_app/add_webhook/add_webhook_generator.rb
shopify_app-21.3.1 lib/generators/shopify_app/add_webhook/add_webhook_generator.rb
shopify_app-21.3.0 lib/generators/shopify_app/add_webhook/add_webhook_generator.rb
shopify_app-21.2.0 lib/generators/shopify_app/add_webhook/add_webhook_generator.rb
shopify_app-21.1.1 lib/generators/shopify_app/add_webhook/add_webhook_generator.rb
shopify_app-21.1.0 lib/generators/shopify_app/add_webhook/add_webhook_generator.rb