Last active
August 29, 2015 14:00
-
-
Save FromAtom/11184728 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
require 'rubygems' | |
require 'google/api_client' | |
require 'yaml' | |
require 'time' | |
require 'date' | |
require 'idobata' | |
class GoogleCalendarManager | |
def initialize(api_yaml_path) | |
@client = Google::APIClient.new | |
@params = nil | |
oauth_yaml = YAML.load_file(api_yaml_path) | |
@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'] | |
end | |
def params=(params) | |
@params = params | |
end | |
def find_calendar_id(calendar_name) | |
cal = @client.discovered_api('calendar', 'v3') | |
calendar_list = @client.execute(:api_method => cal.calendar_list.list) | |
calendar_list.data.items.each do |item| | |
if item['summary'] == calendar_name | |
return item['id'] | |
end | |
end | |
return nil | |
end | |
def events | |
unless @params | |
puts("'params' is empty") | |
return [] | |
end | |
events = [] | |
cal = @client.discovered_api('calendar', 'v3') | |
result = @client.execute(:api_method => cal.events.list, :parameters => @params) | |
result.data.items.each do |item| | |
events << item | |
end | |
return events | |
end | |
end | |
class TimeCalculator | |
def self.get_term_today | |
return self.get_term_in_n_days(0) | |
end | |
def self.get_term_in_n_days(n) | |
today = Date.today | |
in_n_days = today + n | |
year = in_n_days.year | |
month = in_n_days.month | |
day = in_n_days.day | |
time_min = Time.utc(year, month, day, 0).iso8601 | |
time_max = Time.utc(year, month, day, 23).iso8601 | |
term = { | |
'time_min' => time_min, | |
'time_max' => time_max, | |
} | |
return term | |
end | |
end | |
googleCalendarManager = GoogleCalendarManager.new('.google-api.yaml') | |
calendar_id = googleCalendarManager.find_calendar_id('YOUR_CALENDAR_NAME') | |
term = TimeCalculator.get_term_today | |
params = { | |
'calendarId' => calendar_id, | |
'orderBy' => 'startTime', | |
'timeMin' => term['time_min'], | |
'timeMax' => term['time_max'], | |
'singleEvents' => 'True' | |
} | |
googleCalendarManager.params=(params) | |
events = googleCalendarManager.events | |
hook_url = "https://idobata.io/hook/YOUR_HOOK_URL" | |
Idobata.hook_url = hook_url | |
events.each do |event| | |
Idobata::Message.create(source: event['summary']) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment