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
  • i miss JP sad :( :( 2010-03-19
  • frustration on books/ebooks and ereaders kicking in.. sigh. got to get my mind off them for now 2010-03-19
  • i am worth more than how you see me. my faith and belief in it is greater today than yesterday #fb 2010-03-19
  • nice.. http://bit.ly/dAr1Co.. 2010-03-19
  • There can be no happiness if the things we believe in are different from the things we do. - Freya Stark #fb 2010-03-15
  • LivingLife :: My first cabby receipt: Today seems a normal day.  I have good vibes for today.  smile  At first I f... http://bit.ly/cAZJiL.. #fb 2010-03-15
  • would you believe that Filipinos "trust" Lea Salonga second to Rosa Rosal (in Reader's Digests' Who do you trust poll)? weird. 2010-03-12
  • have you seen a chinese owl? i can't get this image out of my head: http://bit.ly/czilyD.. 2010-03-12
  • i just said this earlier before i got my mail from GodWhisper and it matches! http://bit.ly/b2ZWsy.. 2010-03-12
  • i don't like it when you start a business and all you think about is how to earn fast 2010-03-12
  • i am not inspired. i feel like there are monkeys at my back 2010-03-12
  • RT @crypticmess: RT @ihatequotes: Behind every untrusting woman is the guy who taught her to be that way. -Claudia Cardinale #ihatequotes 2010-03-11
  • @redbaks hahaha.. thanks! i'm up to it anyway! smile 2010-03-11
  • 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