2014-01-12 42 views
0

我在一個基於sinatra的應用程序中工作,其中我從google日曆獲取事件並將所有事件顯示爲列表。但是,當我嘗試獲取全天事件的開始和結束日期時,我遇到了一個不尋常的錯誤。谷歌:: ApiClient未定義的方法'dateTime'

由於全天事件有Date類型的對象,和定時事件具有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類型?

在谷歌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" 
    }, 

它們是不同的類型,這是導致錯誤的原因。

乾杯

+0

dateTime方法是從谷歌api客戶端不是從紅寶石 – Joshi

+0

請包括代碼... –

+0

你正在比較一個蘋果橙色。你可以顯示什麼'dateTime'看起來像一個定時甚至整天的事件?你應該從你從Google獲得的'dateTime'創建一個Rails DateTime對象,然後比較它們。 – Beartech

回答

0

的方法是不event.start.date_timeevent.start.dateTime

對於沒有時間關聯的事件,event.start.date_time將返回零。然後,您可以撥打event.start.date,這應該只返回日期。

相關問題