Class: Whatsapp::Webhook::Installer

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby/whatsapp/webhook/installer.rb

Overview

Copies a personalizable webhook controller into a host Rails app. Plain Ruby with no Rails dependency of its own, so it can be unit tested against a plain directory — lib/tasks/whatsapp.rake is the thin Rails-facing wrapper around this.

Constant Summary collapse

TEMPLATE_PATH =
File.expand_path("templates/webhooks_controller.rb.tt", __dir__)
DESTINATION =
"app/controllers/whatsapp/webhooks_controller.rb"
NEXT_STEPS =
<<~TEXT
  Next steps:

  1. Add these routes to config/routes.rb:

       get  "/whatsapp/webhooks", to: "whatsapp/webhooks#verify"
       post "/whatsapp/webhooks", to: "whatsapp/webhooks#receive"

  2. Configure your verify token and app secret, e.g. in config/initializers/whatsapp.rb:

       Whatsapp.configure do |config|
         config.verify_token = Rails.application.credentials.whatsapp_verify_token
         config.app_secret    = Rails.application.credentials.whatsapp_app_secret
       end

  3. Personalize app/controllers/whatsapp/webhooks_controller.rb — it's your file now.
TEXT

Class Method Summary collapse

Class Method Details

.call(root: Dir.pwd, output: $stdout) ⇒ Symbol

Returns :created or :skipped (an existing controller is never overwritten).

Parameters:

  • root (String) (defaults to: Dir.pwd)

    The host app's root directory.

  • output (IO) (defaults to: $stdout)

    Where to print progress and next steps.

Returns:

  • (Symbol)

    :created or :skipped (an existing controller is never overwritten).



35
36
37
38
39
40
41
42
43
# File 'lib/ruby/whatsapp/webhook/installer.rb', line 35

def call(root: Dir.pwd, output: $stdout)
  destination = File.join(root, DESTINATION)
  result = write_controller(destination, output)

  output.puts
  output.puts NEXT_STEPS

  result
end