Giter Club home page Giter Club logo

eiger's Introduction

Eiger

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

Table of Contents

Instalation

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

Routes

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.

Named parameters

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

Wildcard parameters

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

Query parameters

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

Namespaces

Routes may also be namespaced:

namespace :foo do
 get '/bar' do
  # matches "GET /foo/bar"
  "Foo Bar"
 end
end

Resource routes

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

Example application using Eiger can be downloaded from here

eiger's People

Contributors

garek-zz avatar garrek avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.