Project

mandrake

0.0
No commit activity in last 3 years
No release in over 3 years
Mandrake loads middlewares conditionally, and it provides two options and DSL for setting conditions
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.6
>= 0
>= 0

Runtime

>= 0
 Project Readme

Mandrake

Build Status Gem Version Code Climate

Mandrake loads middlewares conditionally, and it provides two options and DSL for setting conditions. If you use Mandrake, you can avoid executing unnecessary middlewares by setting conditions.

Installation

Add this line to your application's Gemfile:

gem 'mandrake'

And then execute:

$ bundle

Or install it yourself as:

$ gem install mandrake

Builder

Mandrake provides :if and :unless options which are available on #use method. These options can be used to set the condition to enable the middleware at runtime.

Mandrake::Builder.new do
  use Rack::Deflate, if:     request.path_info.start_with?("/deflater")
  use Rack::ETag,    unless: request.path_info.end_with?("etag")
  run ->(env){ [200, {'Content-Type' => 'text/plain'}, ["Hello World"]] }
end

Mandrake provides env and request methods for building the conditional expression. They can be used in the block.

Mandrake::Builder.new do
  request.path_info.start_with?("/public") #=> 'request.path_info.start_with?("/public")'
  env["PATH_INFO"] == "/public" #=> 'env["PATH_INFO"] == "/public"'
end

If you want to use these without the block executed on initialization, you must pass the proc to the conditional options.

builder = Mandrake::Builder.new
builder.use Rack::Deflater, if: proc{ request.path_info.start_with?("/public") }

The arguments of these options allow to pass the lambda, but its behavior is different from proc. lambda is defined as the validation method by define_method. Therefore the validation method will be slow.

Mandrake::Builder.new do
  use Rack::Deflate, if: proc{ request.path_info.start_with?("/deflater") }
  run ->(env){ [200, {'Content-Type' => 'text/plain'}, ["Hello World"]] }
end

In the end, Mandrake::Builder inherits Rack::Builder, so you can use this like Rack::Builder basically. Of course, it does not break the compatibility.

Middleware

This class is for incorporating mandrake into your application easily. You can use as well as the Rack Middleware.

use Mandrake::Middleware do
  use Rack::Deflate, if:     request.path_info.start_with?("/deflater")
  use Rack::ETag,    unless: request.path_info.end_with?("etag")
end

Contributing

  1. Fork it ( https://github.com/namusyaka/mandrake/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request