Posts tagged with 'console':

More Rack::Shell goodies for all Rack worshippers


Rack::Shell got a lot of attention lately and I received some feature requests/ideas from great ruby hackers. Daniel Neighman, currently working on Pancake, pointed me towards Bryan Helmkamp's rack-test. This awesome piece of code is now used by racksh to simulate HTTP requests to your Rack application. Yay!

% racksh
Rack::Shell v0.9.4 started in development environment.
>> $rack.get "/"
=> #<Rack::MockResponse:0xb68fa7bc @body="<html>...", @headers={"Content-Type"=>"text/html", "Content-Length"=>"1812"}, @status=200, ...

Check out README for details. Here are just few examples what's possible:

$rack.get "/", {}, { 'REMOTE_ADDR' => '123.45.67.89' }
$rack.header "User-Agent", "Firefox"
$rack.post "/users", :user => { :name => "Jola", :email => "jola@misi.ak" }

Now you can build and test Sinatra apps in single racksh session like this.

Another nice thing in new version is support for session setup through config files. Rack::Shell supports configuration file .rackshrc which is loaded from two places during startup: user's home dir and application directory (in this order). You can put any ruby code in it, but it's purpose is to setup your session, ie. setting headers which will be used for all $rack.get/post/... requests.

For example to set user agent to Firefox and re-migrate db if loaded environment is test put following in .rackshrc:

# .rackshrc

$rack.header "User-Agent", "Firefox"
DataMapper.auto_migrate! if $rack.env == "test"

You can also make requests in config file:

# .rackshrc

$rack.put "/signin", :login => "jola", :password => "misiacz"

This will ensure you are always logged in when you start racksh :)

Full documentation and sources are on github, gems for all versions on gemcutter.org. Enjoy!

View Comments

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

Rainbow gem updated for Ruby 1.9.1


Thanks to Chad from Spicycode my rainbow gem now supports Ruby 1.9.1 (and it’s backwards compatible with Ruby 1.8.x). It doesn’t provide any new features so there is no need for upgrade for Ruby 1.8.x users. I’ve released new version 1.0.2 at rubyforge.

View Comments

Colorizing console output with Rainbow ruby gem


If you’re working on some cool ruby console-based application or just want to add a little style to your script here is a nice gem for you. It’s called Rainbow and it extends ruby String class adding methods to wrap the string with ANSI escape codes.

Look at example irb session and see example usage:

Rainbow adds following methods to String class:

  • foreground(color) (with color and colour aliases)
  • background(color)
  • reset
  • bright
  • italic (not well supported by terminal emulators).
  • underline
  • blink
  • inverse
  • hide.

Color can be one of following symbols: :black, :red, :green, :yellow, :blue, :magenta, :cyan, :white and :default.

Each of those methods returns string wrapped with some ANSI codes so you can chain calls as in example above.

It also has Windows support (uses win32console gem if installed, otherwise strings are returned unaltered).

The gem is on rubyforge.org so install it by:

sudo gem install rainbow

and require it in your script.

The sources are on github, as usual.

Note: I know that there is similar gem on rubyforge called colored_. But, first, it adds too many methods to String class (the Rails way, method per color + method per background color + method per foregroundbackground color…). Second, as I had it already implemented creating a gem from this code was a snap, so why not do it?

View Comments