Class: Rails::Commands::Generate::Webhook

Inherits:
ShopifyCli::SubCommand show all
Defined in:
lib/project_types/rails/commands/generate/webhook.rb

Instance Attribute Summary

Attributes inherited from ShopifyCli::Command

#ctx, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ShopifyCli::SubCommand

call

Methods inherited from ShopifyCli::Command

call, call_help, #initialize, options, prerequisite_task, run_prerequisites, subcommand, subcommand_registry

Methods included from ShopifyCli::Feature::Set

#hidden?, #hidden_feature

Constructor Details

This class inherits a constructor from ShopifyCli::Command

Class Method Details

.helpObject



26
27
28
# File 'lib/project_types/rails/commands/generate/webhook.rb', line 26

def self.help
  ShopifyCli::Context.message('rails.generate.webhook.help', ShopifyCli::TOOL_NAME)
end

Instance Method Details

#call(args, _name) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/project_types/rails/commands/generate/webhook.rb', line 7

def call(args, _name)
  selected_type = args.first
  schema = ShopifyCli::AdminAPI::Schema.get(@ctx)
  webhooks = schema.get_names_from_type('WebhookSubscriptionTopic')
  unless selected_type && webhooks.include?(selected_type)
    selected_type = CLI::UI::Prompt.ask(@ctx.message('rails.generate.webhook.select')) do |handler|
      webhooks.each do |type|
        handler.option(type) { type }
      end
    end
  end
  spin_group = CLI::UI::SpinGroup.new
  spin_group.add(@ctx.message('rails.generate.webhook.selected', selected_type)) do |spinner|
    Rails::Commands::Generate.run_generate(generate_command(selected_type), selected_type, @ctx)
    spinner.update_title("{{green:#{selected_type}}} config/initializers/shopify_app.rb")
  end
  spin_group.wait
end

#generate_command(selected_type) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/project_types/rails/commands/generate/webhook.rb', line 30

def generate_command(selected_type)
  parts = selected_type.downcase.split("_")
  host = ShopifyCli::Project.current.env.host
  selected_type = parts[0..-2].join("_") + "/" + parts[-1]
  command = @ctx.windows? ? "ruby bin\\rails" : "bin/rails"
  "#{command} g shopify_app:add_webhook -t #{selected_type} -a #{host}/webhooks/#{selected_type.downcase}"
end