Skip to content

Instantly share code, notes, and snippets.

@oisin
Forked from leereilly/gist:987094
Created May 23, 2011 18:34

Revisions

  1. oisin revised this gist Jul 2, 2012. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -23,6 +23,10 @@ def version_compatible?(nums)
    end
    end

    before !VERSION_REGEX do
    halt 404, "Resource not found"
    end

    # Reach this route using
    # http://localhost:4567/api/vX.Y/hello
    #
  2. oisin revised this gist May 23, 2011. 1 changed file with 3 additions and 7 deletions.
    10 changes: 3 additions & 7 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,7 @@
    #
    MAJOR_VERSION = 1
    MINOR_VERSION = 0
    VERSION_REGEX = %r{/api/v(\d)\.(\d)}

    helpers do
    def version_compatible?(nums)
    @@ -14,14 +15,9 @@ def version_compatible?(nums)
    # Enforce compatibility before the call. Rewrite the
    # URL in the request to remove the API versioning stuff
    #
    before %r{/api/v(\d)\.(\d)} do
    before VERSION_REGEX do
    if version_compatible?(params[:captures])
    elements = request.fullpath.split('/')
    new_path = ""
    (3..elements.size).each do |i|
    new_path << "/#{elements[i]}"
    end
    request.path_info = new_path
    request.path_info = request.path_info.match(VERSION_REGEX).post_match
    else
    halt 400, "Version not compatible with this server"
    end
  3. @leereilly leereilly revised this gist May 23, 2011. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -16,8 +16,12 @@ def version_compatible?(nums)
    #
    before %r{/api/v(\d)\.(\d)} do
    if version_compatible?(params[:captures])
    target = request.fullpath.split('/').last
    request.path_info = "/#{target}"
    elements = request.fullpath.split('/')
    new_path = ""
    (3..elements.size).each do |i|
    new_path << "/#{elements[i]}"
    end
    request.path_info = new_path
    else
    halt 400, "Version not compatible with this server"
    end
  4. oisin created this gist May 2, 2011.
    31 changes: 31 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    require 'sinatra'

    # Set the version of the API being run here
    #
    MAJOR_VERSION = 1
    MINOR_VERSION = 0

    helpers do
    def version_compatible?(nums)
    return MAJOR_VERSION == nums[0].to_i && MINOR_VERSION >= nums[1].to_i
    end
    end

    # Enforce compatibility before the call. Rewrite the
    # URL in the request to remove the API versioning stuff
    #
    before %r{/api/v(\d)\.(\d)} do
    if version_compatible?(params[:captures])
    target = request.fullpath.split('/').last
    request.path_info = "/#{target}"
    else
    halt 400, "Version not compatible with this server"
    end
    end

    # Reach this route using
    # http://localhost:4567/api/vX.Y/hello
    #
    get '/hello' do
    "Hello there, compatible client."
    end