0.0
No commit activity in last 3 years
No release in over 3 years
PlainRouter is a fast and simple routing engine for Ruby. Using PlainRouter::Method, you can quickly make web application framework like Sinatra. PlainRouter is a porting project of Route::Boom.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 1.11
~> 10.0
~> 3.0
 Project Readme

PlainRouter

Build Status Gem Version

PlainRouter is a fast and simple routing engine for Ruby. Using PlainRouter::Method, you can quickly make web application framework like Sinatra. PlainRouter is a porting project of Route::Boom.

Install

Add this line to your application's Gemfile:

gem 'plainrouter'

And then execute:

$ bundle

Or install it yourself as:

$ gem install plainrouter

Usage

Here is synopsis of using PlainRouter

router = PlainRouter.new
router.add('/', 'dispatch_root')
router.add('/entries', 'dispatch_entries')
router.add('/entries/:id', 'dispatch_permalink')
router.add('/users/:user/{year}', 'dispatch_year')
router.add('/users/:user/{year}/{month:\d+}', 'dispatch_month')

dest, captured = router.match(env['PATH_INFO'])

PlainRouter::Method supports HTTP methods. Sinatra-like Web Framework and Application are below

class SinatraLikeFramework
  def initialize
    @router = PlainRouter::Method.new
    self.routes
  end
  def routes
  end
  def get(path, &block)
    @router.add('GET', path, block)
  end
  def call(env)
    block, params = @router.match(env['REQUEST_METHOD'], env['PATH_INFO'])
    unless block.instance_of?(Proc)
      return not_found
    end
    response = block.call(params)
    if response.instance_of?(Array)
      return response
    elsif response.instance_of?(String)
      return [200, {"Content-Type" => "text/plain"}, [response]]
    end
    not_found
  end
  def not_found
    [404, {"Content-Type" => "text/plain"}, ['Not Found']]    
  end
end

class SinatraLikeApplication < SinatraLikeFramework
  def routes
    get '/' do
      'Hello World!'
    end
    get '/user/:name' do |params|
      "Hello #{params['name']}!"
    end
  end
end

run SinatraLikeApplication.new

License

The gem is available as open source under the terms of the MIT License.

See Also

Author

Yusuke Wada https://github.com/yusukebe