Eiger is a DSL for easy route creation for Rack applications in Ruby with minimal effort. It is inspired by Sinatra and Rails routing engines.
It supports routes with parameters, namespaces and resource routes (set of CRUD routes):
# config.ru
require 'eiger'
require './app.rb'
run Eiger::Base
# app.rb
# simple route
get '/' do
'Hello world!'
end
# route with named parameter
get '/:name' do
"Hello #{params[:name]}!"
end
# route with wildcard parameter
get '/foo/*' do
"This is wildcard route with #{params[:splat].inspect}"
end
# namespaced route
namespace :foo do
get '/bar' do
"Foo Bar"
end
end
# resource route
route 'resource_path', 'resource_controller_name'
Install the gem:
# Gemfile
gem 'eiger', github: "garek/eiger"
Note that Rack, which is Eiger's direct dependency will be automatically fetched and added by Bundler.
And run with:
rackup
View at: http://localhost:9292
If you want to run your Rack application with Eiger, using Bundler is the recommended way.
In your Gemfile
add Eiger and Rack gem:
# Gemfile
gem 'rack'
gem 'eiger', :github => "garek/eiger"
Now you can run your app like this:
bundle install
rackup app.rb
In Sinatra, a route is an HTTP method paired with a URL-matching pattern. Each route is associated with a block:
get '/foo' do
.. show something ..
end
post '/foo' do
.. create something ..
end
put '/foo' do
.. replace something ..
end
patch '/foo' do
.. modify something ..
end
delete '/foo' do
.. destroy something ..
end
Routes are matched in the order they are defined. The first route that matches the request is invoked.
Route patterns may include named parameters, accessible via the
params
hash:
get '/hello/:name' do
# matches "GET /hello/foo" and "GET /hello/bar"
# params['name'] is 'foo' or 'bar'
"Hello #{params['name']}!"
end
Route patterns may also include splat (or wildcard) parameters, accessible
via the params['splat']
array:
get '/say/*/to/*' do
# matches /say/hello/to/world
params['splat'] # => ["hello", "world"]
end
Routes may also utilize query parameters:
get '/posts' do
# matches "GET /posts?title=foo&author=bar"
title = params['title']
author = params['author']
# uses title and author variables; query is optional to the /posts route
end
Routes may also be namespaced:
namespace :foo do
get '/bar' do
# matches "GET /foo/bar"
"Foo Bar"
end
end
A set of resource routes may be specified shortly:
route 'post', 'post_controller'
# will generate a set of paths and match them with controller actions:
# GET /post -> PostController#index
# GET /post/:id -> PostController#show
# POST /post -> PostController#create
# PUT /post/:id -> PostController#update
# DELETE /post/:id -> PostController#destroy
In Eiger::Controller subclass you can specify index
, show
, create
, update
and destroy
actions which will be automaticaly matched with resource routes.
class PostController < Eiger::Controller
# match GET /post
def index
"... index ..."
end
# match GET /post/:id
def show
"... show post ..."
end
# match POST /post
def create
"... create post ..."
end
# match PUT /post/:id
def update
"... update post ..."
end
# match DELETE /post/:id
def destroy
"... destroy post ..."
end
end
Example application using Eiger can be downloaded from here