Quick fixing Blackbook gem for Gmail

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:

1
2
@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. wink

Cheers, I hope this helps.

Recommend me on Working With Rails

If you found this post of any use, then please take the time to recommend me on Working with Rails.

3 Responses to “Quick fixing Blackbook gem for Gmail”

  • purzelrakete
     

    thanks for posting this fix. i’ve copied the project and moved it to github, where collaboration is much easier. the fix is included. project now here: http://github.com/purzelrakete/blackbook/tree/master

  • purzelrakete
     

    thanks for posting this fix. i’ve copied the project and moved it to github, where collaboration is much easier. the fix is included. project now here: http://github.com/purzelrakete/blackbook/tree/master

  • coderkitty
     

    Thanks, I found it here: http://github.com/purzelrakete/blackbook/tree/master/lib/blackbook/importer/gmail.rb :)

Leave a Reply

Hi, I've been busy you know. wink If you don't know yet, this site and my blog codes are hosted in . I recently moved to Git and found that they have support for private Git repositories. Below are details of my last git push.

Maricris S. Nonato on 26 Mar
Commit: e734bd6878629223c5067326471d7ec0aac7d6e4

Updated display of ads and popular content