Posts tagged with 'sinatra':

Rails-like console for any Rack based ruby web app


I always miss script/console from Rails while developing my Sinatra apps, especially ones built with DataMapper where I need to auto-migrate my db. Sinatra doesn't come with any comparable solution as it's not a full framework, but rather library for creating simple web apps. Recently I tried Heroku platform and their "heroku console" command inspired me to create something similar - racksh aka Rack::Shell.

racksh is a console for Rack based ruby web applications. It's like Rails' script/console or Merb's merb -i, but for any app built on Rack. You can use it to load application environment for Rails, Merb, Sinatra, Camping, Ramaze or your own framework provided there is config.ru file in app's root directory.

It's purpose is to allow developer to introspect his application and/or make some initial setup, ie. running mentioned DataMapper.auto_migrate!. It's mainly aimed at apps that don't have similar facility (like Sinatra) but can be used without problems with Merb or Rails apps.

How it works? It loads whole application environment like Rack web server, but it doesn't run the app. Simply, methods like use or run which are normally invoked on Rack::Builder instance are being stubbed.

Instalation is as easy as:

gem install racksh -s http://gemcutter.org

Then to open console run following inside rack application directory (containing config.ru file):

racksh

To specify location of config.ru set CONFIG_RU env variable:

CONFIG_RU=~/projects/foobar/config.ru racksh

Executing ruby code inside application environment and printing results is also supported:

racksh Order.all
racksh "Order.first :created_at => Date.today"

Default Rack environment is set to development but it can be changed by setting RACK_ENV env variable:

RACK_ENV=production racksh

Now I don't need to create some kind of console.rb for my new Rack app, I just use racksh. Enjoy!

UPDATE: Read here for more info.

View Comments

Rack middleware using HTML Tidy


Did you waste some time trying to spot the problem in html markup in your ruby web app? Are you strict validation freak? If yes, then read on.

I had several situations when something disappeared from the page or layout broke and the problem was in some missing closing tags or invalid nesting of elements. As I recently do my web development in Merb and Sinatra (this blog actually runs on Sinatra) and both of them are built on top of Rack I thought that I could write a middleware which can show me the problems in my markup.

So here it is. It uses well known Tidy tool to check for markup errors in actions output. Following code snippet shows the middleware class and its use in the simplest Sinatra app.

After running this tiny webapp and opening root url in your browser you will get following output in terminal:

It prints any warning and error messages to the console so you can easily spot any minor and major problems in the markup.

View Comments