【问题标题】:The Google API ruby client apparently does not fetch all eventsGoogle API ruby​​ 客户端显然没有获取所有事件
【发布时间】:2014-08-10 01:53:10
【问题描述】:

我使用 Github 上的 Google API Ruby 客户端 example repo 作为我的日历应用程序的起点。

它运行良好,通常没有问题。然而,最近,我注意到我的生产应用程序并没有获取所有事件。在通过 ruby​​ 客户端请求了几个日历之后,我在表格中呈现事件。在我的初次通话中,只有我使用我的应用程序生成的日期,根本没有从日历中获取任何数据。在第二次请求时,一些日历将返回数据并重新加载应用程序几次,所有日历将返回事件。

所以对我来说,似乎正在进行一些缓存。如果数据返回的速度不够快,则会向我发送一个空响应。再次请求会发送一些数据,请求越多,返回的数据就越多。

也许我的设置有问题,我认为这并不完美:

get '/' do
  #fetch all calendars
  result = api_client.execute(:api_method => calendar_api.calendar_list.list,
                              :authorization => user_credentials)
  cals = result.data.items.map do |i|
    #skip id calendar does not belong to owner or is the "private" primary one
    next if i.primary || i.accessRole != "owner"
    i.summary
  end
  cals.compact!
  #save all users mentioned in calendars in a set
  events_list = result.data.items.map do |i|
    #skip calendar if primary or not owned by user (cannot be changed anyway)
    next if i.primary || i.accessRole != "owner"
    r = api_client.execute(:api_method => calendar_api.events.list,
                           :parameters => {'calendarId' => i.id},
                           :timeMax => DateTime.now.next_month,
                           :timeMin => DateTime.now.beginning_of_month)
    #capture all calendars and their events and map it to an Array
    r.data.items.delete_if { |item| item.status == "cancelled" }
  end
  #remove skipped entries (=nil)
  events_list.compact! 
  slim :home, :locals => { :title => Konfig::TITLE, :events_list => events_list, :cals => cals}
end

顺便说一句::timeMax 和 :timeMin 也没有按预期工作,但我想这是一个不同的问题。

【问题讨论】:

    标签: ruby google-api-ruby-client


    【解决方案1】:

    这里的代码似乎不是问题。

    我猜会发生以下情况之一

    1. 您受到速率限制? (使用小应用程序不太可能达到限制,但使用响应代码仍然很容易检查)
    2. 加载耗时超过 2 秒(默认 net http 响应超时为 2 秒,默认法拉第设置为 net:http)

    在这种情况下我会怎么做。在决定接下来的步骤之前,我会做以下事情

    1. 从响应对象打印 api 客户端 gem 中的响应对象错误代码和 http 标头。我会寻找响应中的缓存标头是什么以及状态代码是什么。

    2. 如果您的生产机器上有 ngrep,只需让它打印并显示整个 http 请求响应,而不是在 gem 中打印。

    3. 如果响应时间超过 2 秒,请增加 net::http 的超时设置并检查。

    【讨论】:

    • 非常感谢。无法真正复制我自己的错误,但你的回答对我来说很有意义,所以我想我会把 50 分奖励给你 :)
    猜你喜欢
    • 2013-11-25
    • 2020-01-04
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    • 2014-10-10
    • 2015-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多