I have a web service that returns some XML data using a RESTful API. I am going to implement a new database with completely different controller code, that will be in effect after some date. But any WS call for the old data should still use the old DB.
Right now, part of the URL contains the date. I am parsing this at the beginning of the controller and then going off to an old or new function.
def web_service_action
#shared code between old and new methods
@some_instance_variables = ...
# check the date and fork appropriately
if use_new?
web_service_action_new
else
web_service_action_old
end
def use_new?
date_match = params[:id_string].match(/^.*([0-9]{8})$/)
matched_date = date_match[1]
date = Time.local(date_match[0,4],date_match[4,2],date_match[6,2])
if date >= DATE_CONSTANT
return true
else
return false
end
end
end
I’m wondering if there is a clever way to do this with a before_filter or a conditional in the routes file somehow?
Additional information: I am using an old version of Rails (2.3.x) and the new method is a total rewrite, not an incremental change.
To start, I recommend watching Ryan Bates’ railscast on REST API Versioning. He explains very well how to manage API changes in your application while maintaining BC.