我正在开发一个基于 sinatra 的应用程序,其中我从谷歌日历获取事件并将所有事件显示为列表。但是,当我尝试获取全天事件的开始和结束日期时遇到了一个不寻常的错误。
由于全天事件有一个 Date 类型的对象,而定时事件有一个 dateTime 类型的对象,这两个对象将不会显示,我得到的错误是:
没有名为 dateTime 的此类方法
它在只有定时事件(dateTime 对象)事件时工作正常,但在全天事件(日期对象)时不能正常工作。
任何帮助都会很棒。
代码:
require 'rubygems'
require 'google/api_client'
require 'date'
# modified from 1m
# Update these to match your own apps credentials
service_account_email = "" # Email of service account
key_file = "" # File containing your private key
key_secret = 'notasecret' # Password to unlock private key
# Get the Google API client
client = Google::APIClient.new(:application_name => 'GCalendar',
:application_version => '1.0.0')
# Load your credentials for the service account
key = Google::APIClient::KeyUtils.load_from_pkcs12(key_file, key_secret)
client.authorization = Signet::OAuth2::Client.new(
:token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
:audience => 'https://accounts.google.com/o/oauth2/token',
:scope => 'https://www.googleapis.com/auth/calendar',
:issuer => service_account_email,
:signing_key => key)
motd = Array.new
summary = ""
description = ""
tickerEvent = " "
# Start the scheduler
# Request a token for our service account
client.authorization.fetch_access_token!
# Get the calendar API
calendar = client.discovered_api('calendar','v3')
today = DateTime.now().to_s
# Execute the query
result = client.execute(:api_method => calendar.events.list,
:parameters => {'calendarId' => 'idNo',
'timeMin' => today,
'singleEvents' => true,
'orderBy' => 'startTime'})
events = result.data.items
events.each do |e|
if(DateTime.now() >= e.start.dateTime)
summary = e.summary
description = e.description
tickerEvent = summary.to_s + " - " +description.to_s
motd.push(tickerEvent)
elsif(DateTime.now() >= e.end.dateTime)
motd.delete(e)
end
end
motd.clear
end
有没有办法检查事件是 Date 类型还是 DateTime 类型?
在 google api 中,开始日期时间和结束日期时间看起来像(这是一个定时事件):
"start": {
"dateTime": "2013-12-03T12:30:00+13:00",
"timeZone": "Pacific/Auckland"
},
"end": {
"dateTime": "2013-12-03T13:00:00+13:00",
"timeZone": "Pacific/Auckland"
},
而一整天的活动看起来像:
"start": {
"date": "2014-01-17"
},
"end": {
"date": "2014-01-18"
},
它们属于不同的类型,这就是导致错误的原因。
干杯