Google Calendar API v3 対応
懸案になっていた Google Calendar API v3 対応ですが、
対応したバージョンをさきほど GitHub に登録しました[1]。
rubyにてgoogleカレンダーの情報を取得する
http://qiita.com/iron-breaker/items/2440c4ab41a482b1b096
を参考にして google-api-client をインストールし、取得した
.google-api.yaml を google-api.yaml という名前でカレント
ディレクトリに置きます[2]。
今年の祝日を取得するには下記のようなコードを使います。
require 'google/api_client'
require "yaml"
require "when_exe"
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')
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
詳細は、また別の記事で書く予定です。
[1] 例によって RubyGem の更新は次回です。
[2] Windows の場合は、さらに cacert.pem を
wget http://curl.haxx.se/ca/cacert.pem
のようにして取得して配置しておく必要があります。
( http://d.hatena.ne.jp/komiyak/20130508/1367993536 )
対応したバージョンをさきほど GitHub に登録しました[1]。
rubyにてgoogleカレンダーの情報を取得する
http://qiita.com/iron-breaker/items/2440c4ab41a482b1b096
を参考にして google-api-client をインストールし、取得した
.google-api.yaml を google-api.yaml という名前でカレント
ディレクトリに置きます[2]。
今年の祝日を取得するには下記のようなコードを使います。
require 'google/api_client'
require "yaml"
require "when_exe"
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')
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
詳細は、また別の記事で書く予定です。
[1] 例によって RubyGem の更新は次回です。
[2] Windows の場合は、さらに cacert.pem を
wget http://curl.haxx.se/ca/cacert.pem
のようにして取得して配置しておく必要があります。
( http://d.hatena.ne.jp/komiyak/20130508/1367993536 )
この記事へのコメント