0.01
No commit activity in last 3 years
No release in over 3 years
Sinatra NAmed Path support
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

Sinatra NAmed Paths (snap)¶ ↑

Enables named paths in Sinatra apps.

The SNAP extension is a solution for named paths in Sinatra. It also provides helper methods to build URLs and perform parameter substitution.

Install the gem:

sudo gem install sinatra-snap

Then require it in your application and use away!

require 'rubygems'
require 'sinatra'
require 'sinatra/snap'

paths :add => '/add/:addend/:augend',
      :sum => '/sum/:addend/:augend',
      :subtract => '/subtract/*/*',
      :difference => %r{/difference/(\d+)/(\d+)}

get :multiply => '/multiply/:one/:two' do |one, two|
  "#{one.to_i * two.to_i}"
end

get :add do
  redirect path_to(:sum).with(params[:addend], params[:augend])
end

get :sum do
  "#{params[:addend]} + #{params[:augend]} = #{params[:addend].to_i + params[:augend].to_i}"
end

get :subtract do
  redirect path_to(:difference).with(params[:splat][0], params[:splat][1])
end

get :difference do
  "#{params[:captures][0]} - #{params[:captures[1]]} = #{params[:captures][0]].to_i - params[:captures][1]].to_i}"
end

If your application subclasses Sinatra::Base, you have to register SNAP yourself:

class MyApplication < Sinatra::Base
  register Sinatra::Snap

  paths :add => '/add/:addend/:augend'

  get :add do
    redirect path_to(:sum).with(params[:addend], params[:augend])
  end
end

Copyright © 2009 Brandon Carlson (bcarlso). See LICENSE for details.