The blackbook gem, though not really maintained well right now has still been a good solution for my needs in networking stuffs. Although, recently pointed out by Marco Fang was some trouble with Gmail changing the way data is retrieved via the API.

Previously, a query for contacts via

@bl = Blackbook.get(:username => username, :password => pass)

would produce something like:

{:name=>"My contact's name", :email=>"contact_email@gmail.com"}

but since some changes from Gmail's end have taken place, then the previous type of result would only be true for those contacts that don't have other notes and other details (like YM, mobile number, a secondary email, etc).

Now, a query from Gmail's addressbook via Blackbook gem would look like this if you have some notes:

{:name=>"Friend's Name", :email=>"friendemail@gmail.com<span class=\\"n\\"> - +639181234567 555-1234 YM: friendemail</span>"}

Its not really another hash or extra encoded stuffs that were being appended but the notes that you have for this contact.

As you might have checked out http://pastie.org/315399, you'll see that I no longer check on the vailidity of the email addresses. This might be a fault too especially when the email provider does not do prior checking to email sending and contact info saving. In truth, the problem with the Gmail changes could have been caught in that very same line where the fault might spring up.

@contacts << [item[:name], item[:email]]

So, as a quick fix, its better to check that the email address is a valid one, effectively removing all the extra characters attached to it by doing the following:

@clean_email = item[:email][/[a-zA-Z0-9._%+-]+@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,4}/]
@contacts << [item[:name], @clean_email] unless @clean_email.blank?

Pros of this solution:

  • It doesn't need a more than one liner fix
  • It conveniently removes an implicit bug of taking into consideration wrong email formats
  • It gets the email from Gmail's email parameter without so much effort
  • It uses a fast regular expression

    Cons of this solution:

  • It looks too easy, it might be faulty in the end

  • The cleaned email might not be equal to the original expected email (though better than sending an email to an invalid address, but just as worse as sending the mail to another email recipient because of character discrepancy)

    Anyhow, this quick fix will work for Gmail only. If the other protocols would change soon, then I think we'll have to adjust again, or fork the existing copy of the gem. ;)

    Cheers, I hope this helps.