Sha256: 1874fa76f7ce317752a749a9070a0c24d4e659541f38cdea6bf1e28ef75f08bf

Contents?: true

Size: 1.83 KB

Versions: 6

Compression:

Stored size: 1.83 KB

Contents

require 'rails/generators/base'

module SpiffyStoresApp
  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 :address, type: :string, aliases: "-a", 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/spiffy_stores_app.rb',
          "  config.webhooks = [\n  ]\n",
          before: 'end'
        )
      end

      def inject_webhook_to_spiffy_stores_app_initializer
        inject_into_file(
          'config/initializers/spiffy_stores_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
        address.split('/').last
      end

      def load_initializer
        File.read(File.join(destination_root, 'config/initializers/spiffy_stores_app.rb'))
      end

      def webhook_config
        "\n    {topic: '#{topic}', address: '#{address}', format: 'json'},"
      end

      def topic
        options['topic']
      end

      def address
        options['address']
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
spiffy_stores_app-8.2.11 lib/generators/spiffy_stores_app/add_webhook/add_webhook_generator.rb
spiffy_stores_app-8.2.10 lib/generators/spiffy_stores_app/add_webhook/add_webhook_generator.rb
spiffy_stores_app-8.2.9 lib/generators/spiffy_stores_app/add_webhook/add_webhook_generator.rb
spiffy_stores_app-8.2.8 lib/generators/spiffy_stores_app/add_webhook/add_webhook_generator.rb
spiffy_stores_app-8.2.7 lib/generators/spiffy_stores_app/add_webhook/add_webhook_generator.rb
spiffy_stores_app-8.2.6 lib/generators/spiffy_stores_app/add_webhook/add_webhook_generator.rb