Ruby on Rails

Depends how you interpret things

Monthly Archives: August 2010

Enhancing script/console

I Used Rails ./script/console a lot for debugging my rails apps. Every time when I started debugging something, I have to keep my console log[*.log] file to see what corresponding queries being generated to carried out the result I desired.

I found a better solution for that. I enhanced my script/console, which enabled on-screen query logging for my rails app. I made a small change to #{RAILS_ROOT}/script/console.rb file, which it will look to load any additional ruby file resides in #{RAILS_ROOT}/console_script/ if available.

I have also included the script I wanted to load in the first place, I find it quite handy to be able to see what kind of queries my commands are generating.

#updates /script/console.rb

LOAD_HOOK_DIRECTORY = “#{RAILS_ROOT}/console_scripts”

Find.find( LOAD_HOOK_DIRECTORY ) do |filename|
if filename =~ /\.rb$/
puts “Adding #{filename} to load-path”
libs << ” -r #{filename}”
#add sql_log.rb file to /console_script/
def log_to(stream=STDOUT, colorize=true)
ActiveRecord::Base.logger =
ActiveRecord::Base.colorize_logging = colorize

Black Book Web mail import gem-add mail provider

Black book is one the easiest way to add web email importing feature in your application. Recently I am working on a Social network cum movie portal site. By default, black book provides support for gmail, yahoo, hotmail, AOL and csv. But as per my requirement I have to add 20-30 mail providers from different countries. I started with

require “blackbook”
require “hpricot”
require ‘blackbook/importer/page_scraper’

class Free < Blackbook::Importer::PageScraper
# your code

How to login into the users’ account for which we are fetching contacts.

def login
page = agent.get(‘;)
form = page.form_with(:name => “implogin”)
form.imapuser = options[:username]
form.fields[4].value = options[:password]
page = agent.submit(form,form.buttons.first)
raise( Blackbook::BadCredentialsError, “That username and password was not accepted. Please check them and try again.” ) if page.body =~ /Username and password do not match/

def scrape_contacts
unless agent.cookies.find{|c| == “Horde”}
raise( Blackbook::BadCredentialsError, “Must be authenticated to access contacts.” )

page = agent.get(‘;)
contact_rows = (“form[@name=contacts]”))
data = Hpricot(contact_rows.to_s)
data = (data/”table[1]”)/”.listitem”
data.collect do |row|
:name  => (row/”a”).first.inner_text,
:email => (row/”a”).last.inner_text

Points to keep track

  • scraping contacts differs from one mail provider to another.
  • dependencies- mechanize, faster-csv for page scraping
  • we can extend  the mail provider within the black book gem as well as with in ur app[no need to patch gem]