Class: Fopost::Rails::WebhooksController

Inherits:
ActionController::API
  • Object
show all
Defined in:
app/controllers/fopost/rails/webhooks_controller.rb

Overview

Receives FoPost webhooks and republishes them as ActiveSupport::Notifications events, so subscribing costs no route, controller, or queue of your own.

ActiveSupport::Notifications.subscribe('fopost.post.published') do |*, payload|
Rails.logger.info(payload[:data])
end

Two events fire per delivery: fopost.<event> (fopost.post.published, fopost.delivery.failed, …) and fopost.webhook for a catch-all.

Instance Method Summary collapse

Instance Method Details

#createObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/controllers/fopost/rails/webhooks_controller.rb', line 20

def create
  secret = Fopost::Rails.config.webhook_secret
  return head :service_unavailable if secret.nil? || secret.to_s.empty?

  payload = request.raw_post
  signature = request.headers[WebhookSignature::SIGNATURE_HEADER]
  return head :unauthorized unless WebhookSignature.valid?(payload, signature, secret)

  body = parse(payload)
  event = body && body['event']
  return head :bad_request unless event.is_a?(String) && !event.empty?

  publish(event, body)
  head :ok
end