Creating a self containing mvc application with Sinatra

July 29, 2009 by Michael

I recently presented, a Sinatra based application written in Ruby.

Its main purpose for me is to keep track of my milage in 2009.

Although the application is completely self containing it has some nice features:

Simple setup

The application is simple and i didn’t want to use a “big” database like PostgreSQL or MySQL, therefore i choose SQLite. Together with DataMapper i can use the following and have a running SQLite database in no time:

configure :development do
  DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/")
# A milage is stored for a bike at the beginning of the month.
# Its the milage of the bike at this point of time.
class Milage
  include DataMapper::Resource
  property :id,          Integer, :serial => true
  property :when,        Date, :nullable => false
  property :value,       BigDecimal, :nullable => false, :precision => 8, :scale => 2
  property :created_at,  DateTime
  belongs_to :bike
  validates_is_unique :when, :scope => :bike
  is :list, :scope => [:bike_id]

Creating charts with the google_chart gem

Google has a nice api for creating charts, the Google Charts API. The charts are created through URL parameters and as we know, most browsers limits the length of a query string. And for that, the parameters must be encoded.

Nobody wants to do it themselves so there is gchartrb that i use:

  # Create the google chart
  # All will include all float values
  all =
  @gc ='800x375', nil, :vertical, false) do |bc|
    @bikes.each do |b|
      # Help is an array of floats for all periods
      hlp = b.milage_report.collect{|mr| mr[:milage]}
      # and gets added to the chart for that bike, hlp, b.color
      all += hlp
    hlp =
    # Get all periods. It sure can be done via a group by but 
    # the app should be database agnostiv
    periods = Milage.all.collect{|m| m.when.strftime '%m.%Y'}.uniq
    # Get all other trips in this period and also add them to the chart
    periods.each {|period| hlp <<= (AssortedTrip.sum(:distance, :conditions => ['strftime("%m.%Y", "when") = ?', period]) || 0.0)}
    all += hlp "Assorted Trips", hlp[0,hlp.length-1], "003366"
    # Define the labels
    bc.axis :x, :labels => periods, :font_size => 16, :alignment => :center
    # and the range from 0 to the highest value
    bc.axis :y, :range => [0,all.max], :font_size => 16, :alignment => :center
    bc.show_legend = true
    bc.grid :x_step => 0, :y_step => (100.0/all.max)*25.0, :length_segment => 1, :length_blank => 0 if all.size > 0
  end if @bikes.size > 0

The gc object will be used as simple as

%img{:src => @gc.to_url, :alt => 'gc'}

Using libxml for parsing RSS feeds

It seems that i’m not going to write an application without including my daily faces project in some way.

So i decided not only to present numbers but also my biking pictures. The images are available through a Media RSS feed. The feed itself contains pointers to other pages of that very same feed.

One of the fastest ways to parse XML is LibXML and luckily, it’s available for ruby through LibXML Ruby.

Together with memcached it can be used to efficiently handle feeds like so:

def BikingPicture.random_url
  # Check if pictures are cached...
  biking_pictures = @@cache['biking_pictures']
  unless biking_pictures then
    # Start retrieving the feed
    url = @@uri.parse("")
    next_page = false do |http|
      # Its a media rss feed that defines previous and next feeds
      while url
        req ="#{url.path}?#{url.query}")
        xml = http.request(req).body
        # Parse the data
        doc =
        # Unless this url hasn't been retrieved...          
        unless @@cache[url.to_s]            
          doc.find('/rss/channel/item').each do |item|
            # Get all pictures and store them if not already grapped
            biking_picture = BikingPicture.first :url => item['url']
            unless biking_picture
              biking_picture ={:url => item.find_first('media:thumbnail')['url'], :link => item.find_first('link').content })
          # Mark this url as seen
          @@cache[url.to_s] = next_page
          next_page = true                          
        # Check if there are more feeds...
        next_url = doc.find_first("/rss/channel/atom:link[@rel = 'next']")
        url = if next_url
          @@uri.parse next_url['href']
    # Get the data...
    biking_pictures = BikingPicture.all()
    # ...and store it
    @@cache.set 'biking_pictures', biking_pictures, 3600

Sinatra, Passenger and Memcached

Speaking of being efficient, the application certainly runs through Phusion Passenger aka modrails.

Running a Sinatra app under passenger is as simple as this: Create a directory for a new vhost, setup a structure like

\- public
 - tmp
 - biking.rb

and let contain the following:

root_dir = File.dirname(__FILE__)
require 'biking.rb'
set :environment, ENV['RACK_ENV'].to_sym
set :root,        root_dir
set :app_file,    File.join(root_dir, 'biking.rb')
disable :run
run Sinatra::Application

The apache vhost is configured like so:

<VirtualHost *>
        DocumentRoot "/path/to/the/applications/public/folder"
        RackBaseURI /

I have turned off the Rack and Rails Autodetect features (RailsAutoDetect off, RackAutoDetect off) so i need to explicitly turn them on for a vhost.

After that, the application is running and thats all there is.

But wait. I’ve written before about problems with memcache-client and Passenger and this problems needs to be addressed in a Rails as well as Rack application.

I handle them in Sinatra as follows:

configure do
  # Create a global memcache client instance...
  @@cache ={
    :c_threshold => 10000,
    :compression => true,
    :debug => false,
    :namespace => 'some_ns',
    :readonly => false,
    :urlencode => false
  @@cache.servers = 'some_server:11211'
  if defined?(PhusionPassenger)
    PhusionPassenger.on_event(:starting_worker_process) do |forked|
      @@cache.reset if forked

If PhusionPassenger is available, install an event handler that resets the freshly forked memcached connection to avoid corruption.


Google Maps is great but one thing that’s often forgotten is reverse geocoding. Google doesn’t offer such api (as far as i know) but GeoNames does.

I’ve written a mobile J2ME client around JSR 179 api that can push my current location to this server when i’m biking so my girlfriend can follow my rides on the map.

The client pushes latitude and longitude and the server replies with the coordinates in angles and the name of the place. The name is retrieved through the Ruby Geonames API like so:

# Adds a new location
post '/locations' do
  location = params
  if then
      country_subdivision  = Geonames::WebService.country_subdivision(location.latitude, location.longitude)
      places_nearby = Geonames::WebService.find_nearby_place_name(location.latitude, location.longitude).first
      (location.description = "#{}, #{country_subdivision ? country_subdivision.admin_name_1 + ', ' : ''}#{places_nearby.country_name}"[0,2048].strip) and if places_nearby
    rescue Exception => exc      

Inline templates, external resources

The thing started as a small application. PHP was out of the question but Rails was overkill, too. I just wanted one single file.

I can use the great Haml syntax as inline templates. This goes for creating HTML as well as for CSS through Sass.

To get some basics, i also include one of the W3 core styles.

The only resources that lives outside biking.rb is JQuery for easy creation of the autorefreshing code and some images, namely the cute green bikers from Greensmilies.


It’s absolutely possible to write an application with thee MVC Pattern in mind without one of the “big” frameworks in just one single script.

I would go much further with this app using just one script, but for it’s current purpose, it’s great, i think.

I hope you enjoyed reading my little annotations as i enjoyed writing them and the application. The full source code is available right through and it’s published under the BSD license.


  1. Patrick Berkeley wrote:

    Looks great! Thanks for the share.

    Posted on July 29, 2009 at 5:07 PM | Permalink
  2. Michael wrote:

    Thanks Patrick for looking at it!

    Posted on July 29, 2009 at 10:04 PM | Permalink
  3. anon_anon wrote:

    Another XML parser worth looking into is vtd-xml

    Posted on August 17, 2009 at 9:22 PM | Permalink
One Trackback/Pingback
  1. […] recreating my Ruby / Sinatra based biking application: The original application is a very condensed Ruby […]

Post a Comment

Your email is never published nor shared. Required fields are marked *