Created
June 11, 2020 17:19
-
-
Save CliffS/0094217b71da214222e02eb0d02c5de1 to your computer and use it in GitHub Desktop.
Library to use Google Calendar
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
require '../utils/formats' | |
PThrottle = require 'p-throttle' | |
Google = require('googleapis').google | |
class Calendar | |
constructor: (config) -> | |
@calendars = Google.calendar 'v3' | |
@calid = config.calendarId | |
initialise: -> | |
if @cal | |
Promise.resolve() | |
else | |
auth = new Google.auth.GoogleAuth | |
scopes: [ | |
'https://www.googleapis.com/auth/calendar' | |
] | |
Promise.all [ | |
auth.getClient() | |
auth.getProjectId() | |
] | |
.then (results) => | |
[ @auth, project ] = results | |
@calendars.calendars.get | |
auth: @auth | |
calendarId: @calid | |
.then (result) => | |
@cal = result.data | |
listCalendars: -> | |
@initialise() | |
.then => | |
@calendars.calendarList.list | |
auth: @auth | |
.then (results) => | |
results.data | |
createCalendar: (title, description) -> | |
@initialise() | |
.then => | |
@calendars.calendars.insert | |
auth: @auth | |
requestBody: | |
summary: title | |
description: description | |
.then (results) => | |
results | |
colours: -> | |
@initialise() | |
.then => | |
@calendars.colors.get | |
calendarId: @calid | |
auth: @auth | |
.then (result) => | |
result.data | |
addUser: (email) -> | |
@initialise() | |
.then => | |
@calendars.acl.insert | |
auth: @auth | |
calendarId: @calid | |
requestBody: | |
# id: email | |
role: 'owner' | |
scope: | |
type: 'user' | |
value: email | |
delete: (eventID) -> | |
@initialise() | |
.then => | |
@calendars.events.delete | |
auth: @auth | |
calendarId: @calid | |
eventId: eventID | |
clear: -> | |
throttled = PThrottle (eventID) => | |
@delete eventID | |
, 2, 1000 | |
@initialise() | |
.then => | |
@calendars.events.list | |
auth: @auth | |
calendarId: @calid | |
maxResults: 2500 | |
.then (events) => | |
console.log events.data.items.length | |
throttled event.id for event in events.data.items | |
.then (results) => | |
Promise.all results | |
list: (params = {}) -> | |
@initialise() | |
.then => | |
params.calendarId = @calid | |
params.auth = @auth | |
@calendars.events.list params | |
.then (results) => | |
results.data | |
insert: (res) -> | |
@initialise() | |
.then => | |
event = @buildEvent res | |
@calendars.events.insert | |
auth: @auth | |
calendarId: @calid | |
requestBody: event | |
.then (result) => | |
result.data | |
get: (id) -> | |
@initialise() | |
.then => | |
@calendars.events.get | |
auth: @auth | |
calendarId: @calid | |
eventId: id | |
.then (result) => | |
result.data | |
update: (id, res, title) -> | |
@initialise() | |
.then => | |
event = @buildEvent res, title | |
@calendars.events.update | |
auth: @auth | |
calendarId: @calid | |
eventId: id | |
requestBody: event | |
.then (result) => | |
result.data | |
module.exports = Calendar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment