我使用了 Github 上的 Google API Ruby 客户端示例存储库作为我的日历应用程序的起点。
它运行良好,通常没有问题。然而,最近我注意到我的应用程序在生产中并没有获取所有事件。通过 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 也没有按预期工作,但我认为这是一个不同的问题。
代码似乎不是这里的问题。
我猜测正在发生以下情况之一
在这种情况下我会怎么做。在决定下一步之前我会做以下事情
从其响应对象打印 api 客户端 gem 中的响应对象错误代码和 http 标头。我会寻找响应中的缓存标头是什么以及状态代码是什么。
如果你的生产机器上有ngrep,只需让它打印并显示整个http请求响应,而不是在gem中打印它。
如果响应时间超过 2 秒,请增加 net::http 的超时设置并检查。