クライアントプログラムから Google Calendar API v3 を使用するには、まず google-api-client をインストールします。
( google-api-client for gem - https://rubygems.org/gems/google-api-client )
$ gem install google-api-client
次に Calendar API を有効にして、アクセストークンを取得します。
→「rubyにてgoogleカレンダーの情報を取得する」参照
ただし、参照サイトの手順中の 'google-api oauth-2-login' の実行の際 OpenSSL が cacert.pem を参照するので、Microsoft Windows の場合は事前に cacert.pem を取得しておく必要があります。
→「Windows7+Ruby+google-api-clientで、GoogleAPI向けOAuth認証用のYAMLファイルを取得する」参照
参照するカレンダーは下記のID(calendarId)で指定します。
| 私用 | primary |
| 国別祝日 | 言語コード.国名#holiday@group.v.calendar.google.com |
→国別祝日で使用可能な組み合わせ
本ライブラリは google-api-client のクライアントオブジェクトとサービスオブジェクトに Calendar API v3 の制御を依頼します。
これらのオブジェクトを生成するサンプルコードを下記に示します。
(インストールの項で取得したアクセストークンはファイル google-api.yaml に保持しているものとしました)
require 'google/api_client'
require 'yaml'
oauth_yaml = YAML.load_file('google-api.yaml')
client = Google::APIClient.new(:application_name => 'when_exe',
:application_version => When::VERSION)
client.authorization.client_id = oauth_yaml['client_id']
client.authorization.client_secret = oauth_yaml['client_secret']
client.authorization.scope = oauth_yaml['scope']
client.authorization.refresh_token = oauth_yaml['refresh_token']
client.authorization.access_token = oauth_yaml['access_token']
service = client.discovered_api('calendar', 'v3')
Google Calendar API v3 で参照したイベント情報全体を保持するコンテナとして When::GoogleAPI::Calendarクラスを追加しました。
クラスメソッド Calendar.list で Calendar インスタンスを生成し、インスタンスメソッド Calendar#enum_for で個々のイベント日時を取得します。
calendar = When::GoogleAPI::Calendar.list(client, service,
'en.japanese#holiday@group.v.calendar.google.com')
calendar.enum_for(When.when?('20150101/1231')).each do |date|
p [date, date.events[0].summary] #=>
# [2015-01-01, "New Year's Day"]
# [2015-01-12, "Coming of Age Day"]
# [2015-02-11, "National Foundation Day"]
# [2015-03-21, "Spring Equinox"]
# [2015-04-29, "Shōwa Day"]
# [2015-05-03, "Constitution Memorial Day"]
# [2015-05-04, "Greenery Day"]
# [2015-05-05, "Children's Day"]
# [2015-05-06, "Constitution Memorial Day observed"]
# [2015-07-20, "Sea Day"]
# [2015-09-21, "Respect for the Aged Day"]
# [2015-09-22, "Bridge Public holiday"]
# [2015-09-23, "Autumn Equinox"]
# [2015-10-12, "Sports Day"]
# [2015-11-03, "Culture Day"]
# [2015-11-23, "Labor Thanksgiving Day"]
# [2015-12-23, "Emperor's Birthday"]
end
前項を実現するため When::V::Eventクラスのコンストラクタで、Google Calendar API v3 の events.list コマンドの応答を直接扱えるようにしました。
下記のサンプルコードでは応答中のイベント情報(Google::APIClient::Schema::Calendar::V3::Eventクラス)を to_hash メソッドで Hash 化して、そのまま When::V::Event のコンストラクタの引数としています
result = client.execute(:api_method => service.events.list,
:parameters => {'calendarId' => 'en.japanese#holiday@group.v.calendar.google.com'})
result.data.items.each do |e|
p e.class #=> Google::APIClient::Schema::Calendar::V3::Event
event = When::V::Event.new(e.to_hash)
p event.class #=> When::V::Event
p event.google_api_props #=>
# {"kind"=>"calendar#event",
# <..snip..>
# "summary"=>"Coming of Age Day",
# "creator"=>
# {"email"=>"en.japanese#holiday@group.v.calendar.google.com",
# "displayName"=>"Holidays in Japan",
# "self"=>true},
# "organizer"=>
# {"email"=>"en.japanese#holiday@group.v.calendar.google.com",
# "displayName"=>"Holidays in Japan",
# "self"=>true},
# "start"=>{"date"=>"2014-01-13"},
# "end"=>{"date"=>"2014-01-14"},
# "transparency"=>"transparent",
# <..snip..>
end
| 2015-05-25 | gcalapi と google-api-client |
| 2015-05-27 | 閏時の扱い |