Snowing Code

Personal notes on software development

Sinatra Eye for the .NET Guy

(Publish date: 02/06/2011)

So writing this blog using Sinatra has been a brilliant experience up until now. The main reason to start hacking around and porting my blog from wordpress to here was obviously education- but not just for education on itself. I think the main lesson was the knowledge I've acquired to make an educated decision in the future, when the opportunity will come up, to use a ruby rack-based framework to build a website. I can honestly say now that next time I'm being asked to build a web site whose major functionality would be to display information- static or without far too much complex presentation logic- Sinatra would be my first choice. The reason for making such a decision would be without a doubt the ruby ecosystem- the wonderful choice of ruby gems out there, the amount of documentation one can find on the web and the brilliant community that'll be always happy to help you out with the questions you may have.

Gems are probably the best example for the case of simplicity and code reduction. Two of the gems I've used when writing this site were nokogiri and gmail_sender. With gmail_sender it was simply the need to enable email sending via gmail; plenty of gems are out there to perform this same task and gmail_sender was the one that worked for me the best. All you need to do is call mail.send(:to => email_address, :subject => subject, :content => content) - that easy!

As for Nokogiri, I wanted to import all of my old wordpress posts. Nothing would be easier than doing that with nokogiri. Something like:
doc = Nokogiri::HTML(open(post_url))
body = doc.xpath("//body")
divs = body.xpath("//div")
post_text = divs > ".entrytext"
was enough to make me work it out. sweet.

Another life saver was the rss renderer baked into Sinatra. These sort of things just make you think how insane it would be using .NET for doing the same thing. I think Justin Etheredge made a good enough point about this.

The most important advice I could give someone who wishes to pick up Sinatra and start writing something would be rather obvious: RTFM. The Sinatra Readme document is a good place to start and the Sinatra-book is in my opinion the best place to get a proper insight into the framework. Thing is, only after reading what the framework can do, will you know what do you need to look for elsewhere, what sort of gems you may need to find and gain time afterwards. Obviously.
Knowing beforehand that you need to add use Rack::MethodOverride in order to have the delete and put methods could save you a lot of head scratchings.

A few pointers I did find useful myself:

  • When dealing with different pages of different parts/concerns of your application, in order to separate these into different classes, or if you'd like 'controllers', just move your 'actions' into a new class inheriting from Sinatra::Base, while adding to your 'primary' controller use MyWebSite::SecondController
  • Working with Haml has proved to be a truly agreable experience. Thinking about the parts of your page as classes really pays out when you are then sitting down to add the corresponding styles.
  • Last but not least- heroku. A lot has been written about it, so I'm not going to senselessly reiterate it. I'll just say that I found the ability to deploy, rollback, fix and redeploy directly to live and on top of that straight from the beginning, was exactly what I would like any work experience to look like. A man can dream...

blog comments powered by Disqus