0

Googleカレンダーからイベントを取得し、すべてのイベントをリストとして表示するsinatraベースのアプリケーションに取り組んでいます。しかし、終日イベントの開始日と終了日を取得しようとしたときに、異常なエラーが発生しました。

終日のイベントには Date 型のオブジェクトがあり、時限イベントには dateTime 型のオブジェクトがあるため、これら 2 つのオブジェクトは表示されません。エラーは次のとおりです。

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"
   },

それらは異なるタイプであり、これがエラーの原因です。

乾杯

4

1 に答える 1