GithubHelp home page GithubHelp logo

bmorelax / sinatra-advanced-routes Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rkh/sinatra-advanced-routes

0.0 0.0 0.0 55 KB

Make Sinatra routes first class objects (extracted from BigBand).

License: Other

Ruby 100.00%

sinatra-advanced-routes's Introduction

sinatra-advanced-routes

Makes routes first class objects in Sinatra.

Build Status Gem Version Code Climate

Check out sinatra-contrib you are looking for other fancy Sinatra extensions.

Installation

# Gemfile
gem 'sinatra-advanced-routes', :require => 'sinatra/advanced_routes'

If you are serving a modular application, register the extension manually :

require 'sinatra/base'
require 'sinatra/advanced_routes'

class Foo < Sinatra::Base
  register Sinatra::AdvancedRoutes
end

Examples

Route manipulation

require 'sinatra'
require 'sinatra/advanced_routes'

admin_route = get '/admin' do
  administrate_stuff
end

before 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
end

first_route = get '/:name' do
  # stuff
end

other_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

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.

sinatra-advanced-routes's People

Contributors

j15e avatar rkh avatar rye 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.