如何访问谷歌联系人API的红宝石 [英] How to access Google Contacts API in ruby

查看:193
本文介绍了如何访问谷歌联系人API的红宝石的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我竭力要访问谷歌联系人API.First我试过谷歌API的红宝石客户的宝石但事实证明,它不支持通讯录API

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.

接下来的镜头是<一个href=\"http://stackoverflow.com/questions/25637485/access-google-contacts-api-on-ruby\">google_contacts_api宝石。我用的oauth2 来访问认证密钥(的获取认证令牌引导问题)。但正确地传递令牌的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).

下面是我的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?

更新:

由于 @alvin 建议,现在正在工作。但该组触点不被打印出来。相反,它是印刷#&LT; GoogleContactsApi :: ContactSet:0x000000020e49d8&GT; 。例:这里是由该code打印

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

输出:

#<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?

推荐答案

编辑补充约可枚举实现信息

(我写的宝石。)

有是文档中的错误。 联系人是实现可枚举,它不提供 [类的实例] 方法,但提供第一方法。

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.

所以,尽量 groups.first 而不是组[0] 。同样,使用 contacts.first 而不是联系人[0] 。我的错! (我可能在我的头上做了 to_a

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.)

响应更新

要回答这个问题,下半年,它看起来像你找到了联系相关便利的方法,特别是 Contact.primary_email 方法。 请参见(有点不完整的,抱歉)YARD文档更多的方法。

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)

您只需要使用 HASHIE ::醪方法来访问数据时,不提供方便访问(例如,如果谷歌开始返回额外的数据宝石不是招 ŧ占还)。您所描述的用例并不需要这个。

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。在未来,你可能想打开一个新的问题,而不是编辑现有的问题。

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

这篇关于如何访问谷歌联系人API的红宝石的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆