I am currently developing versions APIs for a new website. I understand how to use spaces in routes, but I stick to the best way to implement versions in a model.
The following are code examples that use a rail structure, but the principle of this issue should be consistent across most web frameworks.
Currently, routes look something like this:
MyApp::Application.routes.draw do namespace :api do namespace :v1 do resources :products, :only => [:index, :show] end end end
And the controller:
class Api::V1::ProductsController < V1Controller respond_to :json, :xml def index respond_with @products = Product.scoped end def show respond_with @product = Product.find(params[:id]) end end
So itβs obvious that weβre just exposing the attributes available here, this solution works fine if you only have one version of the API. What happens when you want to release V2 and V2, you need to redefine the way the product name is displayed (while maintaining backward compatibility with V1 - at least in the short term)?
As far as I can see, you have several options ...
- Remove V1 support immediately and handle the fallout (worst possible solution)
- You start overriding the to_ [format] methods (I'm sure you do this with as_ [format], but this is next to the dot) to include a new attribute ...
name_2 - it seems just as dumb - Implement some kind of proxy class that is responsible for exposing only those methods that we after
- Let the views handle the creation of some hash that is controlled by the controller versions and calls
to[format] on ...
Three and four are the only things that I really can think of, it makes some sense ... Three will look something like this:
What have other people done in the past?
source share