Skip to content

Commit

Permalink
updates calendar_uploader to use CalendarV3
Browse files Browse the repository at this point in the history
  • Loading branch information
abrophy committed Sep 6, 2017
1 parent 9ad28ff commit f045a47
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions app/services/calendar_uploader.rb
@@ -1,38 +1,29 @@
# frozen_string_literal: true

class CalendarUploader
require 'google/apis/calendar_v3'

attr_reader :summary, :date, :access_token, :email

def initialize(summary:, date:, access_token:, email:)
@summary = summary
@date = date
@access_token = access_token
@email = email

@calendar_service = Google::Apis::CalendarV3::CalendarService.new
@calendar_service.authorization = AccessToken.new(@access_token)
end

def upload_event
client.authorization.access_token = access_token

service = client.discovered_api('calendar', 'v3')
parsed_date = Chronic.parse(date, endian_precedence: %i[little median]).to_time.iso8601

event = {
'summary' => summary,
'start' => { 'dateTime' => parsed_date },
'end' => { 'dateTime' => parsed_date }
summary: summary,
start: { date_time: parsed_date },
end: { date_time: parsed_date }
}

params = {
api_method: service.events.insert,
parameters: { 'calendarId' => email, 'sendNotifications' => true },
body: JSON.dump(event),
headers: { 'Content-Type' => 'application/json' }
}

client.execute!(params)
end

def client
@client ||= Google::APIClient.new(application_name: ENV['GOOGLE_APP_NAME'])
event = @calendar_service.insert_event('primary', event, send_notifications: true)
end
end

0 comments on commit f045a47

Please sign in to comment.