Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rkh/sinatra-advanced-routes
Make Sinatra routes first class objects (extracted from BigBand).
https://github.com/rkh/sinatra-advanced-routes
Last synced: 19 days ago
JSON representation
Make Sinatra routes first class objects (extracted from BigBand).
- Host: GitHub
- URL: https://github.com/rkh/sinatra-advanced-routes
- Owner: rkh
- License: other
- Created: 2010-02-12T15:06:18.000Z (almost 15 years ago)
- Default Branch: master
- Last Pushed: 2018-01-13T15:57:35.000Z (almost 7 years ago)
- Last Synced: 2024-05-14T09:22:17.979Z (6 months ago)
- Language: Ruby
- Homepage:
- Size: 53.7 KB
- Stars: 38
- Watchers: 3
- Forks: 5
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# sinatra-advanced-routes
> Makes routes first class objects in [Sinatra](http://sinatrarb.com).
[![Build Status](https://travis-ci.org/rkh/sinatra-advanced-routes.png)](https://travis-ci.org/rkh/sinatra-advanced-routes)
[![Gem Version](https://badge.fury.io/rb/sinatra-advanced-routes.png)](https://rubygems.org/gems/sinatra-advanced-routes)
[![Code Climate](https://codeclimate.com/github/rkh/sinatra-advanced-routes.png)](https://codeclimate.com/github/rkh/sinatra-advanced-routes)Check out [sinatra-contrib](https://github.com/sinatra/sinatra-contrib) you are looking for other fancy Sinatra extensions.
## Installation
```ruby
# Gemfile
gem 'sinatra-advanced-routes', :require => 'sinatra/advanced_routes'
```If you are serving a [modular application](https://github.com/sinatra/sinatra#modular-vs-classic-style),
register the extension manually :```ruby
require 'sinatra/base'
require 'sinatra/advanced_routes'class Foo < Sinatra::Base
register Sinatra::AdvancedRoutes
end
```## Examples
### Route manipulation
```ruby
require 'sinatra'
require 'sinatra/advanced_routes'admin_route = get '/admin' do
administrate_stuff
endbefore do
# Let's deactivate the route if we have no password file.
if File.exists? 'admin_password' then admin_route.activate
else admin_route.deactivate
end
endfirst_route = get '/:name' do
# stuff
endother_route = get '/foo_:name' do
# other stuff
end# Unfortunatly first_route will catch all the requests other_route would
# have gotten, since it has been defined first. But wait, we can fix this!
other_route.promote
```### Route inspection
```ruby
require 'some_sinatra_app'SomeSinatraApp.each_route do |route|
puts '-' * 20
puts route.app.name # "SomeSinatraApp"
puts route.path # that's the path given as argument to get and akin
puts route.verb # get / head / post / put / delete
puts route.file # "some_sinatra_app.rb" or something
puts route.line # the line number of the get/post/... statement
puts route.pattern # that's the pattern internally used by sinatra
puts route.keys # keys given when route was defined
puts route.conditions # conditions given when route was defined
puts route.block # the route's closure
end
```Some of that fields (like conditions or pattern) can be changed, which will take immediate effect on the routing.