Skip to content

Instantly share code, notes, and snippets.

@ismasan
Created September 10, 2009 11:01
Show Gist options
  • Save ismasan/184475 to your computer and use it in GitHub Desktop.
Save ismasan/184475 to your computer and use it in GitHub Desktop.
# This module allows you to prepend a version 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