Monthly Archives: September 2005

Really cool IDE feature

Ages ago Carl sent me a link to a crappy looking Ruby IDE that has one really cool feature, a visual code overview. I am not sure just how useful it would be, but it looks cool.

Free broadband speed increase!

I recieved an email today from Virgin containing some very good news indeed!

We’re increasing the speed of your Virgin.net Broadband Plan One service, so you’ll be able to download up to 40 times faster than a standard dial up connection – that’s up to 2 Mbps.

This is fantastic! Except that my ISP is Pipex…

This wasn’t a spam trying to sell me broadband, Virgin are genuinely trying to inform me that they are upgrading my line.

I would love to know how Virgin got hold of the information that I have a DSL line, and why they think I am their customer.

Net::IMAP::Hairy

The Ruby Net::IMAP module has a bit of a warty interface, but it does work. It could do with a much nicer interface (perhaps a Net::IMAP::Simple equivalent?) and better docs. Here is a working example:

#!/usr/bin/env ruby

require 'rubygems'
require 'net/imap'
require_gem 'rmail'

imap = Net::IMAP.new('host')
imap.login('username', 'password')
imap.examine('inbox')
imap.search(["SUBJECT", "Some text to look for", "SINCE", "11-Sep-2005"]).each do |message_id|
message = RMail::Parser.read(imap.fetch(message_id, ["BODY[]"])[0].attr['BODY[]'])
puts message.header.subject
end

Not too difficult once you work out what you need to be doing.

Quoting in ActiveRecord

Quite often I need to take a string from an HTML form, such as “one two three” and search a database field for each of the words contained in the string, eg:

SELECT * FROM table WHERE text ilike '%one%' AND text ilike '%two%' AND text ilike '%three%'

Obviously the strings need quoting, or I am going to be vulnerable to SQL injection attacks. In perl I used to do something like this:

join(" AND ", map {"field ilike " . $dbh->quote("%" . $_ . "%")} qw/one two three/)

I was hoping that ActiveRecord would have a nice way of doing this automatically when passed a string or array of strings, but there doesn’t seem to be one. Instead I came up with this:

@items = Item.find(:all, :conditions => @params["search"].split.map{|x| "item ilike " + Item.quote("%" + x + "%")}.join(" AND "))

The interesting bit broken out looks like this:

"some string seperated by spaces".split.map{|x| "item ilike " + Item.quote("%" + x + "%")}.join(" AND ")

In particular the #quote method of the Item object: Item.quote("%" + x + "%")

The #quote method depends on a PostgreSQL database adaptor being used. The MySQL adapter has a #quote_string method that looks like it does the same thing, but I have not experimented with it.

It would be nice to know if there was a better way of doing this, particularly one that was independent of the database adaptor.

Thanks to leeo on IRC who pointed out that #quote is a method of AbstractConnector (from which both the PG and MySql addapers inherit)

Follow

Get every new post delivered to your Inbox.