Sinatra serenades Rails 2
It turns out it’s really easy to mount a Sinatra application into your Rails 2 app (yes, there are still a few Rails 2 apps out there). Here’s how.
First, declare Sinatra as a gem dependency in config/environment.rb
:
Next, create a directory to put Sinatra apps in and add them to the Rails load path:
Then, define a Sinatra app in a file in app/sinatra
:
And finally, insert your Sinatra app as middleware:
Sinatra, being the classy framework that it is, is smart enough to act as a Rack middleware OR an endpoint (what’s the difference?). If you use it as a middleware, it will serve any routes that it knows about and pass on any that it doesn’t.
Why would you use Rails and Sinatra?
I’m working on large Rails application that consists mostly of an XML API. We’re working on a new version of the API with some significant changes. Since the new API will only have 7-10 endpoints, and only return JSON, we are thinking it will just be cleaner to build it as a Sinatra app. It will be easier to document and easier to maintain. And we are embedding it in the Rails app so it can make use of our existing models.