Skip to content

Instantly share code, notes, and snippets.

@dctanner
Forked from ismasan/Sinatra route versioning.rb
Created September 10, 2009 11:21
Show Gist options
  • Save dctanner/184480 to your computer and use it in GitHub Desktop.
Save dctanner/184480 to your computer and use it in GitHub Desktop.
# This module allows you to prepend a verion prefix to your Sinatra URLs
# Example:
#
# require 'rubygems'
# require 'sinatra/base'
#
# class App < Sinatra::Base
# register Versioned
#
# set :version, 'v1'
#
# # => GET /v1/hello
# get '/hello' do
# 'This is a GET for version' + options.version.inspect
# end
#
# # => POST /v1/hello
# post '/hello' do
# 'This is a POST for version ' + options.version.inspect
# end
#
# end
module Versioned
def post(*args, &block)
super *versioned_route(args), &block
end
def get(*args, &block)
super *versioned_route(args), &block
end
def put(*args, &block)
super *versioned_route(args), &block
end
def delete(*args, &block)
super *versioned_route(args), &block
end
protected
def versioned_route(args)
args[0] = "/#{version}#{args[0]}"
args
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment