Rutter
HTTP router for Rack.
Status
Installation
Add this line to your application's Gemfile:
gem "rutter"
And then execute:
$ bundle
Or install it yourself as:
$ gem install rutter
Usage
Basic usage
require "rutter"
router = Rutter.new do
get "/", to: ->(env) { [200, {}, ["Hello World"]] }
end
run router.freeze
HTTP verbs
The router supports most of the verbs available.
require "rutter"
Rutter.new do
get "/", to: ->(env) {}
post "/", to: ->(env) {}
put "/", to: ->(env) {}
patch "/", to: ->(env) {}
delete "/", to: ->(env) {}
"/", to: ->(env) {}
head "/", to: ->(env) {}
trace "/", to: ->(env) {}
end
Named parameters
In the example :title
is a named parameter. The values are accessible via env["rutter.params"]
and it contains a Hash<String => String>
.
require "rutter"
Rutter.new do
get "/books/:title", to: ->(env) {}
end
Named parameters only match a single path segment:
/books/eloquent-ruby match
/books/confident-ruby match
/books/confident-ruby.rb no match
/books/eloquent-ruby/reviews no match
/books/ no match
Catch-All parameters
catch-all parameters have the form *title
. Like the name suggests, they match everything, event new /
segments. Therefore they must always be at the end of the pattern.
require "rutter"
Rutter.new do
get "/books/*title", to: ->(env) {}
end
/books/eloquent-ruby match
/books/confident-ruby match
/books/confident-ruby.rb match
/books/eloquent-ruby/reviews match
/books/ no match
Optional segments
Support for optional segments have the form (i-am-optional)
.
require "rutter"
Rutter.new do
get "/books(/:title)", to: ->(env) {}
end
/books/eloquent-ruby match
/books/confident-ruby match
/books/confident-ruby.rb no match
/books/eloquent-ruby/reviews no match
/books match
Redirects
Make legacy paths point to a new destination.
require "rutter"
Rutter.new do
get "/legacy-path", to: redirect("/new_path")
end
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/sandelius/skeletor. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.