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 14 Feb
Commit: 48fe3652ffb644af13f4a5651f9d80474b09f622

Adding new ad image for Pledgie campaign - campaign image for coderkitty fundraising
  • my mom is preventing me from doing work.. distracting me by offering fruits. sigh.. 2010-03-11
  • while creating mockups, i even find myself thinking in partials.. hmm.. too much ROR 2010-03-11
  • RT @makieduardo: "Keep me safe, O God... for in you I take refuge." (Psalm 16:1) 2010-03-11
  • @packetlog Philosophies of India? That wasn't in my list, but if you think its worth the read, then why not?! smile 2010-03-11
  • i'm on a voyage to read over 1101 books! smile 2010-03-10
  • @nightdreamer vampires? lol. i don't even like them as much as a commoner.. wink whoops! not to pull on any string there! tongue 2010-03-10
  • why is it that i find myself attracted more to classic novels?? 2010-03-10
  • @codinghorror lol. i'd love to see that happen tongue 2010-03-10
  • @gregmoreno had a look at that a long time ago. isn't it expensive though? or is it worth it? 2010-03-10
  • it took 3 yrs for Kindle to be introduced globally.. i wonder how long i have to wait for Nook sad 2010-03-09
  • Kill the zombie browser!!! 'Funeral' held for aging Web browser - CNN.com http://bit.ly/brlGYF.. 2010-03-08
  • @holtaway http://twitpic.com/17cws.. - adorable!!! 2010-03-08
  • http://twitpic.com/17cwo.. - Its hard to work when this is your setup.. 2010-03-08
  • tea + ambient music. puts my soul at ease 2010-03-08
  • 15 Quality Web-Based Applications to Create Mock-Ups and Wireframes | Spyre Studios http://bit.ly/azTHis.. 2010-03-07
  • 10 Completely Free Wireframe and Mockup Applications - Speckyboy Design Magazine http://bit.ly/9xhgFw.. 2010-03-07