Search code examples
rubyapigoogle-apigoogle-contacts-api

How to access Google Contacts API in ruby


I'm struggling to access the Google Contacts API.First I tried the google-api-ruby-client gem but it turned out that it does not support the Contacts API.

Next shot was the google_contacts_api gem. I used oauth2 to access the authentication key(Getting authentication token guide question). But after passing the token correctly to the api it is producing an error.

`<main>': undefined method `[]' for #<GoogleContactsApi::GroupSet:0x000000039fcad8>` (NoMethodError).

Here is my code.

# get token using oauth2 gem, and use it below in the google_contacts_api.
google_contacts_user = GoogleContactsApi::User.new(token)
contacts = google_contacts_user.contacts
groups = google_contacts_user.groups

# group methods
group = groups[0]
group.contacts
puts group.contacts

# contact methods
puts contacts.count
puts groups.count
contact = contacts[0]
contact.primary_email
contact.emails

What am I doing wrong?

UPDATE:

As @alvin suggested it is working now. But the group contacts are not being printed out. Instead it is printing #<GoogleContactsApi::ContactSet:0x000000020e49d8>.Example: here is what is printed by this code

groups = google_contacts_user.groups

# group methods
groups.each do |group|
  group_contacts = group.contacts
  puts group_contacts
end

Output:

#<GoogleContactsApi::ContactSet:0x000000020e49d8>
#<GoogleContactsApi::ContactSet:0x0000000504aec0>
#<GoogleContactsApi::ContactSet:0x0000000518dfd0>
#<GoogleContactsApi::ContactSet:0x000000052d9290>
#<GoogleContactsApi::ContactSet:0x000000054280d8>
#<GoogleContactsApi::ContactSet:0x0000000558c2f8>
#<GoogleContactsApi::ContactSet:0x00000005746eb8>
#<GoogleContactsApi::ContactSet:0x000000058a3ea0>

How can I print the group contacts?


Solution

  • Edited to add info about the Enumerable implementation

    (I wrote the gem.)

    There was a bug in the documentation. groups and contacts are instances of classes that implement Enumerable, which doesn't provide the [] method, but does provide the first method.

    So, try groups.first instead of groups[0]. Likewise, use contacts.first instead of contacts[0]. My bad! (I probably did a to_a in my head.)


    Response to Update

    To answer the second half of the question, it looks like you found the relevant convenience methods for Contact and Group, in particular the Contact.primary_email method. See more methods in the (somewhat incomplete, sorry) YARD docs.

    To get all the emails, you basically need to iterate over the returned contacts. As I mentioned in the updated response to the first part of your question, groups and contacts have all the methods of Enumerable. (Enumerable documentation). Here are some examples:

    # What are all the groups called?
    user.groups.map(&:title)
    
    # Find group by title. (Returns nil if no such group.)
    group = user.groups.select { |g| g.title = "Group Name" }
    
    # Get all primary emails from a group
    group.contacts.map(&:primary_email)
    
    # Get all primary emails from all contacts regardless of group
    user.contacts.map(&:primary_email)
    

    You only need to use the Hashie::Mash methods to access data when no convenience accessor is provided (for example, if Google starts returning extra data the gem hasn't accounted for yet). The use case you described doesn't require this.

    P.S. In the future, you might want to open a new question instead of editing your existing question.