drnic (owner)

Forks

Revisions

gist: 207929 Download_button fork
public
Public Clone URL: git://gist.github.com/207929.git
Embed All Files: show embed
probably_versioned.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Temporary middleware for your APIs to immediately support a /v1/some/path.json prefix to
# all route calls.
#
# When you decide to freeze a current API and provide a /v2/ route then
# you will cease using this middleware and implement /v1/ and /v2/ routing as appropriate
# to your app.
#
# This middleware provides a placeholder until then so users can be told to use /v1/some/path routes
# immediately.
#
# Example:
# /resource.json -> /resource.json
# /v1/resource.json -> /resource.json
#
# MIT-License - Dr Nic Williams, http://drnicwilliams.com
#
module Rack
  class ProbablyVersioned
    def initialize(app)
      @app = app
    end
    
    def call(env)
      req = Rack::Request.new(env)
      if req.path_info =~ /\/?v1(.+)/
        req.path_info = $1
      end
      @app.call(env)
    end
  end
end