尝试使用 gem 显示来自 API 的联系人列表,但只能显示几个字段



我正在尝试在表格中显示HubSpot API(https://developers.hubspot.com/docs/methods/contacts/get_contacts)中的所有联系人列表。我正在使用 https://github.com/adimichele/hubspot-ruby 宝石,这主要是有帮助的。我能够使用以下代码获取要显示的大部分信息:

<div class ="container-fluid text-center">
<div class="row">
<h1>Contacts</h1>
</div>
<br />
<table class="table">
<thead>
<tr>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Company</th>
<th scope="col">Email</th>
</tr>
</thead>    
<tbody>
<% 
(Hubspot::Contact.all).each do |contact|
info = contact.properties
%>
<%= info %>
<tr>
<th scope="row"><%= info[:firstname] %></th>
<th scope="row"><%= info[:lastname] %></th>
<th scope="row"><%= info[:company] %></th>
</tr>
<% end %>
</tbody>
</table>

但是,默认情况下,Gem 仅显示联系人的 3 个属性。虽然 HubSpot API 发送更多内容,例如电子邮件,但我在挑选和选择我想要的属性时遇到了麻烦。我能够自己成功挑选出电子邮件属性:

<tbody>
<% 
( Hubspot::Contact.all({ property: 'email' }) ).each do |contact|
info = contact.properties
%>
<%= info %>
<tr>
<th scope="row"><%= info[:firstname] %></th>
<th scope="row"><%= info[:lastname] %></th>
<th scope="row"><%= info[:company] %></th>
<th scope="row"><%= info[:email] %></th>
</tr>
<% end %>
</tbody>

但是后来我不知道如何在电子邮件顶部添加更多内容。例如,我在以下方面收到语法错误:

( Hubspot::Contact.all({ property: 'email', 'firstname' }) ).each do 

( Hubspot::Contact.all({ property: 'email'})({ property: 'firstname'}) ).each do 

我考虑过放弃这一切,从一颗新的宝石开始。但是唯一一个看起来值得等待(https://github.com/HubSpot/rHAPI)的人将无法正确安装。所以我正在尝试让第一个解决方案起作用。

感谢您的任何帮助。我正在自学,以前从未在这里发过帖子。我很紧张在这里发帖,所以关于这个问题或我的代码的任何反馈对我来说都意味着世界。

你可以试试:

Hubspot::Contact.all.each do |contact|

然后,您抓取整个对象,您可以获得构建表格所需的部分,假设您需要的键位于从 Hubspot 返回的对象中。

我不熟悉那个宝石,但降低价值似乎不是一件正常的事情。如果密钥在 .all.each 中不可用,则不太可能使用筛选器使用它。

我快速浏览了 gem 的库,看不到任何会告诉我它会丢弃数据的内容。

谢谢!解决方案是:

{ property: ['email', 'firstname'] } 

最新更新