TL DR: Creating an API. We need different fields for different versions. Teach me wise.
I'm currently trying to find a better way to create a versioned API. That is, I want to have a URL /api/v1/projects.json that displays a list of projects with a bunch of fields and api/v2/projects.json to display a list of projects with separate fields.
I thought about this problem for about 15 minutes, which probably means that all this is wrong. At the moment, I have this in my app/models/project.rb file:
def self.api_fields { :v1 => ["name"], :v2 => ["name", "tickets_count"] } end
Then I can use this in my API controllers ( api/v1/projects_controller.rb ) as follows:
def index respond_with(Project.all(:select => Project.api_fields[:v1])) end
This is great and works the way I would like, but probably the best way. This is your task! Share with me your mountains of wisdom API development.
Bonus points if you come up with a solution that also allows me to use methods for instances of the model object, such as the tickets_count method for the Project method.
source share