Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
A simple router for Rack apps
Ruby
branch: master
Failed to load latest commit information.
lib Made the request method part of the route
test Made the request method part of the route
.gitignore Added support for an optional format param based on the extension of …
.rvmrc Added support for named routes
Gemfile Basic gem skeleton
LICENSE Basic gem skeleton
README.md Improved the documentation by explaining how the parameter from the r…
Rakefile
config.ru Added usage directly in the README
rack-router.gemspec Made the request method part of the route

README.md

Rack::Router

A simple router for rack apps. Requires Ruby 1.9+.

Installation

Add this line to your application's Gemfile:

gem 'rack-router'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rack-router

Usage

Here's an example showing a simple rack app that prints the value of a route parameter:

require 'rack/router'
require 'rack/lobster'

hello = ->(env) do
  [
    200,
    { "Content-Type" => "text/html" },
    ["<h1>Hello, #{env['rack.route_params'][:name]}</h1>"]
  ]
end

router = Rack::Router.new do
  get "/hello/:name" => hello
  get "/lobster" => Rack::Lobster.new, :as => "lobster"
end

run router

In this example, hello is just a rack app defined inline, in order to give us something to route to. The route to our hello app includes a parameter :name. The hello rack app is able to access that parameter via the rack env.

This is a valid Rackup file, so if you put this in a file named config.ru and run rackup, you will be app to hit the application like this:

$ curl http://localhost:9292/hello/paul
<h1>Hello, paul</h1>

Don't forget to try the lobster!

$ open http://localhost:9292/lobster

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request
Something went wrong with that request. Please try again.