Code Monkey home page Code Monkey logo

meddler's Introduction

Meddler

Hey, someone meddled with my middleware!

This lets you have on_request and on_response wrappers around a builder, and optionally skip them in either case. It also gives you before and after even hooks, to do something else awesome before you proceed.

Why?

There’s a few reasons you might want to do this. One is that you may have an expensive sub stack that only needs to run on specifiec urls, or you may want to make something available only to part of the userbase. Here’s an example:

require 'meddler'
use Meddler::Builder do
  on_request {|req| req.will_be_expensive? }
  use ExpensiveMiddleware
  use AnotherExpensiveMiddleware
end

Or perhaps you want to give all of your developers Rack::Bug even in production.

require 'meddler'
use Meddler::Builder do
  on_request do |req|
    req.env['warden'].authenticate
    user = req.env['warden'].user
    user && user.staff_developer?
  end
  use Rack::Bug
end

Usage

In your rackup file, do this:

require 'meddler'
use Meddler::Builder do
  on_request {|req| req.post?}
  before {|req| puts "yup, we passed, and now we're onto passing in the request"}
  use MungePostMiddleware
end

Or, on response

require 'meddler'
use Meddler::Builder do
  on_response {|resp| resp.status == 200}
  after {|req| puts "yup, we passed, just about to pass the response back to the middleware stack in here"}
  use MiddlewareThatLikesOkThings
end

You can have as many on_request and on_response as you’d like, and processing will stop on the first one that returns false. before and after hooks will get called in order and be called with an instance of Rack::Request and Rack::Response respectively.

meddler's People

Contributors

joshbuddy avatar

Stargazers

Angus H. avatar Daniel Vartanov avatar Kristan Krispy Uccello avatar Chris Herring avatar Ilya Grigorik avatar  avatar Mathias Meyer avatar John Barton avatar Paolo Negri avatar David Dai avatar Cyril Mougel avatar Eric Lindvall avatar Cristi Balan avatar Geoffrey Grosenbach avatar

Watchers

 avatar Kristan Krispy Uccello avatar James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.