forked from huginn/huginn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_calendar.rb
69 lines (55 loc) · 2.02 KB
/
google_calendar.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class GoogleCalendar
def initialize(config, logger)
@config = config
if @config['google']['key'].present?
@key = OpenSSL::PKCS12.new(@config['google']['key'], @config['google']['key_secret']).key
else
@key = Google::APIClient::PKCS12.load_key(@config['google']['key_file'], @config['google']['key_secret'])
end
@client = Google::APIClient.new(application_name: "Huginn", application_version: "0.0.1")
@client.retries = 2
@logger ||= logger
@calendar = @client.discovered_api('calendar','v3')
@logger.info("Setup")
@logger.debug @calendar.inspect
end
def auth_as
@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: @config['google']['service_account_email'],
signing_key: @key
});
@client.authorization.fetch_access_token!
end
# who - String: email of user to add event
# details - JSON String: see https://developers.google.com/google-apps/calendar/v3/reference/events/insert
def publish_as(who, details)
auth_as
@logger.info("Attempting to create event for " + who)
@logger.debug details.to_yaml
ret = @client.execute(
api_method: @calendar.events.insert,
parameters: {'calendarId' => who, 'sendNotifications' => true},
body: details.to_json,
headers: {'Content-Type' => 'application/json'}
)
@logger.debug ret.to_yaml
ret
end
def events_as(who, date)
auth_as
date ||= Date.today
@logger.info("Attempting to receive events for "+who)
@logger.debug details.to_yaml
ret = @client.execute(
api_method: @calendar.events.list,
parameters: {'calendarId' => who, 'sendNotifications' => true},
body: details.to_json,
headers: {'Content-Type' => 'application/json'}
)
@logger.debug ret.to_yaml
ret
end
end