rack-rewrite

Rack middleware for request rewriting.

Example

use Rack::Rewrite do

  on :method => 'get' do

    # When it sees /api or /test, it calls whatever is in the act block
    #
    on :path_info => %r{/(api|test)} do
      act { puts "hey way to go!" }
      pass
    end

    # When it sees /google, it makes a redirect to google with the value of the query string as the search
    #
    on :path_info => %r{/google.*} do
      redirect { "http://google.com/search?q=#{CGI.escape(query_string)}" }
    end

    # If a PATH_INFO starts with /valid_place, it strips it off and keeps on truckin'
    #
    on :path_info => %r{/valid_place/.*} do
      set(:path_info) { path_info[%r{/valid_place(/.*)}, 1] }
      pass
    end

    # If a PATH_INFO starts with /hello_kitty, add a kitty header equal to your query string
    #
    on :path_info => %r{/hello_kitty/.*} do
      act { header['kitty'] = query_string }
      pass
    end

    # If the request has a has a param of kitten=cute or kitten=happy, lets log it and pass it on!
    #
    on :params => {:kitten => /cute|happy/}  do
      act { log('what a nice cat') }
      pass
    end

    fail
  end

end