A couple of things to add:
Your redirect match isn't going to work for certain routes - the *api
param is greedy and will swallow up everything, e.g. /api/asdf/users/1
will redirect to /api/v2/1
. You'd be better off using a regular param like :api
. Admittedly it won't match cases like /api/asdf/asdf/users/1
but if you have nested resources in your api it's a better solution.
Ryan WHY U NO LIKE namespace
? :-), e.g:
current_api_routes = lambda do resources :usersendnamespace :api do scope :module => :v2, ¤t_api_routes namespace :v2, ¤t_api_routes namespace :v1, ¤t_api_routes match ":api/*path", :to => redirect("/api/v2/%{path}")end
Which has the added benefit of versioned and generic named routes. One additional note - the convention when using :module
is to use underscore notation, e.g: api/v1
not 'Api::V1'. At one point the latter didn't work but I believe it was fixed in Rails 3.1.
Also, when you release v3 of your API the routes would be updated like this:
current_api_routes = lambda do resources :usersendnamespace :api do scope :module => :v3, ¤t_api_routes namespace :v3, ¤t_api_routes namespace :v2, ¤t_api_routes namespace :v1, ¤t_api_routes match ":api/*path", :to => redirect("/api/v3/%{path}")end
Of course it's likely that your API has different routes between versions in which case you can do this:
current_api_routes = lambda do # Define latest APIendnamespace :api do scope :module => :v3, ¤t_api_routes namespace :v3, ¤t_api_routes namespace :v2 do # Define API v2 routes end namespace :v1 do # Define API v1 routes end match ":api/*path", :to => redirect("/api/v3/%{path}")end