https://github.com/yusukebe/plainrouter
Fast and simple routing engine for Ruby
https://github.com/yusukebe/plainrouter
Last synced: 11 months ago
JSON representation
Fast and simple routing engine for Ruby
- Host: GitHub
- URL: https://github.com/yusukebe/plainrouter
- Owner: yusukebe
- License: mit
- Created: 2016-04-22T03:02:22.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2016-04-22T16:51:48.000Z (about 10 years ago)
- Last Synced: 2025-07-28T08:43:38.112Z (11 months ago)
- Language: Ruby
- Homepage:
- Size: 21.5 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# PlainRouter
[](https://travis-ci.org/yusukebe/plainrouter)
[](https://badge.fury.io/rb/plainrouter)
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](https://metacpan.org/pod/Router::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**
```ruby
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
```ruby
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](http://opensource.org/licenses/MIT).
## See Also
* [Rooter::Boom](https://metacpan.org/pod/Router::Boom)
* [rack-router](https://github.com/pjb3/rack-router)
## Author
Yusuke Wada