Sinatra Simple Example

I’ve been playing around with the Sinarta Ruby web development framework recently and building a larger than usual Hello World Example. It’s describes itself as a DSL for quickly creating web-applications in Ruby with minimal effort (what is it about Ruby people and their obsession with calling everything a DSL?). In reality it’s a great little web framework. It deals with a minimal set of the things you really need to do as part of any application - URL handling and routing, HTTP request and response handling, etc. It reminds me of web.py in it’s minimalist approach which is definitely a good thing.

The following example is the hello world given on the site

pre. require ‘rubygems’ require ‘sinatra’ get ‘/’ do ‘Hello world!’ end

Which isn’t a million miles away from a web.py example:

pre. import web urls = ( ‘/’, ‘hello’ ) app = web.application(urls, globals()) class hello: def GET (self, name): return ‘Hello World’ if name == “main“: app.run()

The only real difference is the separate mapping of URLs to views in web.py, which is closer to how Rails or Django do things.

Their is quite a bit of documentation already for Sinatra, including the start of a book. The code (as with all good code these days) is on github for your forking pleasure.

As for what I’ve been up to I have a more advanced Hello World example up on GitHub. I’m wanting to get a running application that demonstrates all the basic features (except HAML and SASS support). So far I’ve got a simple bit of Rack middleware, several views demonstrating different url handling techniques, basic erb templates, before methods, configuration settings, error handling, decent unit test coverage, a rake file with a tasks for documentation, code coverage, etc. I’ve also got configuration files for running the application with Thin and using God. I’m going to add some simple database connectivity in at some point (either DataMapper or Sequel, I haven’t decided yet) and play around with writing spec tests and a capistrano recipe file. All in it’s a nice way of learning something new at the same time as producing something that might be useful. Once I have the rest of the bits and pieces I might even right a full tutorial.

I think the sweet spot for these sorts of mini frameworks are small services or little applications that just sit their are run. Both Integrity and IRCLogger both use Sinatra for instance and I think it’s used for the GitHub WebHooks as well. It’s exactly the sort of thing Google AppEngine is useful for in fact and Sinatra would likely be a closer fit that Rails if Google ever feel like adding Ruby support. Although it does depress me a little that the top four items in the public issue tracker are I want my own language.