Dead Simple Sinatra + MongoMapper

WARNING: This is just a development note.

Preparation

To play around with Sinatra, the best thing is to put real code to work.

$ mkdir todo
$ cd todo 
$ touch main.rb config.ru

We’ve created a working directory named “todo” and two files: “main.rb” and “config.ru”.

Actually, we could just have one file to make Sinatra app running. But, here, we would like to run the Sinatra app as a Rack app. Furthermore, this config.ru is necessary when we deploy our app on heroku.

It is always good to do development, even with small codebase, with a good practice. Let’s manage our code using Git. in your working directory directory do:

$ git init
$ git add .
$ git commit -a -m "Initial commit"

By then, we could track changes through Git version control system.

The code

Fire up your editor, and edit the main.rb, here I’m using vim

$ vim main.rb

Then, we could add this following code:

require 'rubygems'     
require 'sinatra'         
get '/' do        
"Hello world!"     
do

Don’t forget to install the sinatra gem.

$ sudo gem install sinatra

Afterwards, we open the config.ru:

$ vim config.ru

We then add the following code to our config.ru:

require 'main'     
run Sinatra::Application

After having these two files edited. We could fire our Webserver to serve the app. In here, I’m using Thin

$ thin --rackup config.ru start

You could then open your browser and go to: http://localhost:3000 or you could use curl to check it out:

$ curl http://localhost:3000

Then you’ll receive a “Hello world!” message:

$ Hello world!

Done with simple Hello World app, we could extend this app to use template engine to generate views. Here I’m using erb. Inside your working directory, create a views directory. This is the default directory where you put your *.erb files. Since, we will make changes in our working directory, we could do commit now.

$ git commit -a -m "commit before changes"
$ mkdir views
$ cd views 
$ vim index.erb

In index.erb, we put following code:

<h1>Hello, world!</h1>

Open the main.rb and edit the code:

require 'rubygems'
require 'sinatra'

get '/' do
  erb :index
do

And we’re done. We could run the web server and actually see the result from browser: http://localhost:3000

So, we would like to create a dead-simple CRUD app integrating Sinatra and MongoMapper. I hosted my data in MongoHQ.

To make a connection to MongoHQ, you can simply add following lines to your main.rb

...
require 'mongo_mapper'

...


MongoMapper.connection = Mongo::Connection.new('{server-name}.mongohq.com',27062, :pool_size => 5, :timeout => 5)
MongoMapper.database = '{data-name}'
MongoMapper.database.authenticate('{user-name}','{user-password}')

It is mandatory to install mongo_mapper gem and having an account in MongoHQ. Well, you can also do this in your localhost if you have mongoDB installed in your machine.

To deploy it on heroku, you should also provide .gems file.

$ vim .gems

sinatra
bson
bson_ext
mongo
i18n
mongo_mapper

You can fetch the complete source from: https://github.com/wiradipa/todo

You can see the live demo at: http://tarada.heroku.com

Posted