I can't seem to find much information on the web about the different approaches to building a REST API in Rails; so I kinda have two questions:
Use the standard controllers to return XML when a users adds .xml
to the end of the URL
Pros:
Cons:
.xml
will work in places it doesn't Use namespaced routing to create separate API controllers that only handle API functions, but still have access to the same models that the website uses
Pros:
Cons:
Use route forwarding and constraints to forward all API calls to a Rack application
Pros:
Cons:
REST stands for REpresentational State Transfer and describes resources (in our case URLs) on which we can perform actions. CRUD , which stands for Create, Read, Update, Delete, are the actions that we perform. Although, in Rails, REST and CRUD are bestest buddies, the two can work fine on their own.
I would propose that the API being in the same project as your website isn't a bad thing as long as the code is DRY*. Like you pointed out, having separate codebases is a challenge because you have to keep them in sync with every feature/bugfix you do. It's easier to maintain if they are in the same place. As long as you keep your code DRY, this method is the clear winner.
I would offer XML and JSON from the controllers with a subdomain handled by Rails's routing engine. When someone has picked up on the pattern of api.site.com/resource.xml and tries to access a resource that isn't there, it's really not a big deal. As long as your API is documented clearly and you fail/error gracefully when they try to access a resource not in your API, it should be just fine. I would try to return a message saying that resource isn't available and a url to your api documentation. This shouldn't be a runtime problem for any API consumers, as this should be part of discovering your API.
Just my $0.02.
*DRY = Don't Repeat Yourself. DRY code means you don't copy-paste or rewrite the same thing for your site and your api; you extract and call from multiple places.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With