ブログ

読んで思い出す。忘れるために書く

Google Calendar API を介して予定を作成する

前提条件は「Ruby Quickstart」に同じ

今回は読み取りだけでなく、作成してみた

# ...
# ※ Quickstart と重複する部分は省略
# ...

require 'active_support'
require 'active_support/core_ext/date_time/calculations'

#
# 予定の開始・終了時刻を設定
#

# 「次の日」の14時に日時を設定
start_date_time =
  DateTime.now
          .advance(days: 1)
          .change(hour: 14, minutes: 0, seconds: 0)

# 「50分後」に時刻を再設定
end_date_time =
  start_date_time.advance(
    minutes: 50
  )

event = Google::Apis::CalendarV3::Event.new(
  summary: '面談 (候補日)',
  location: '',
  description: '面談日程 確定前候補 (Google Calendar API からのテスト作成)',
  start: {
    date_time: start_date_time.to_s,
    time_zone: 'Asia/Tokyo'
  },
  end: {
    date_time: end_date_time.to_s,
    time_zone: 'Asia/Tokyo'
  }
  # 定期的なイベント設定
  # recurrence: [
  #   'RRULE:FREQ=DAILY;COUNT=2'
  # ],

  # 参加者設定
  # attendees: [
  #   {email: 'example@example.com'},
  # ],
)

#
# カレンダーに予定を作成
#
service = Google::Apis::CalendarV3::CalendarService.new
service.client_options.application_name = APPLICATION_NAME
service.authorization = authorize # 認証手続きを呼び出して 認可情報を返す
result = service.insert_event('primary', event)
puts "Event created: #{result.html_link}" # 作成したイベント情報にアクセスできる URL を表示

Links