Sha256: a17953d3e61c3bdc40d9f7a981800b31f7c8705a0a091231ca7d278a7ffebec9

Contents?: true

Size: 1015 Bytes

Versions: 3

Compression:

Stored size: 1015 Bytes

Contents

module NoCheckout
  class WebhooksController < ActionController::Base
    skip_before_action :verify_authenticity_token

    # Raised when a method is not implemented on the webhook
    # that's needed to handle the hook.
    UnhandledWebhookError = Class.new(RuntimeError)

    rescue_from UnhandledWebhookError, with: :unhandled_webhook

    def create
      dispatch_webhook
      head :no_content
    end

    private
      def dispatch_webhook
        logger.info "Webhook dispatching #{method_name.inspect}"
        if webhook_method_exists?
          public_send method_name
        else
          raise UnhandledWebhookError, "Webhook method #{method_name.inspect} does not exist"
        end
      end

      def method_name
        raise NotImplementedError
      end

      def webhook_methods
        public_methods(false)
      end

      def webhook_method_exists?
        webhook_methods.include? method_name.to_sym
      end

      def unhandled_webhook
        head :bad_request
      end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nocheckout-0.1.4 app/controllers/nocheckout/webhooks_controller.rb
nocheckout-0.1.3 app/controllers/nocheckout/webhooks_controller.rb
nocheckout-0.1.0 app/controllers/nocheckout/webhooks_controller.rb