Skip to content

Instantly share code, notes, and snippets.

@swalberg
Last active August 29, 2015 14:06
Show Gist options
  • Save swalberg/9bc2e2680afb6762cc06 to your computer and use it in GitHub Desktop.
Save swalberg/9bc2e2680afb6762cc06 to your computer and use it in GitHub Desktop.
# Inside the gem
class ActionController::Base
def self.check_webhook_signature(key, *args)
before_filter lambda { |controller| controller.check_webhook_signature(key, controller)}, *args
end
def check_webhook_signature(key, controller)
# Do the work
end
end
# Gem was included through Gemfile
class WebhookController < ApplicationController
# Gem provides a Class method to ActionController::Base that inserts a before_action
check_webhook_signature "MyKey"
def index
# I can do my thing here
end
end
@swalberg
Copy link
Author

Pros:

  • Still one line
  • Puts the behaviour in the same place as the rest of the code
  • Does not require that every request be regex'ed to see if the middleware needs to do its thing

Cons:

  • Rails specific

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment