Last active
January 6, 2018 17:17
-
-
Save crifat/8cb1714f131633df9d2fac509f1a8f10 to your computer and use it in GitHub Desktop.
Create/Update a Google Calendar Event. Create a .ics File with iCaneldar gem
This file contains hidden or 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
| if guide && (guide.user.access_token.present? && guide.user.refresh_token.present? && guide.user.gmail.present?) | |
| guide.refresh_access_token | |
| client = Google::APIClient.new | |
| client.authorization.access_token = guide.user.access_token | |
| service = client.discovered_api('calendar', 'v3') | |
| result = client.execute(:api_method => service.events.get, | |
| :parameters => {'calendarId' => 'primary', 'eventId' => "#{self.created_on.to_i}"}) | |
| event = result.data | |
| event = { | |
| 'summary' => "#{self.network.name} - #{self.start_time.in_time_zone('UTC').try(:strftime, '%I:%M %p')}", | |
| 'location' => "#{self.meeting_place_description}", | |
| 'description' => "#{self.network.name} - #{self.start_time.in_time_zone('UTC').try(:strftime, '%I:%M %p')} \nClient Name: #{self.client.full_name}\n#{self.half_day? ? 'Half Day' : (self.partial? ? 'Partial' : 'Full Day')}\nLocation: #{self.section.river.name} - #{self.section.name}", | |
| 'start' => { | |
| 'dateTime' => "#{self.assignment_date.strftime('%Y-%m-%dT')}#{self.start_time.in_time_zone('UTC').strftime('%H:%M:00')}#{Time.now.in_time_zone(self.network.time_zone).formatted_offset}" | |
| }, | |
| 'end' => { | |
| 'dateTime' => "#{self.assignment_date.strftime('%Y-%m-%dT')}#{self.end_time.in_time_zone('UTC').strftime('%H:%M:00')}#{Time.now.in_time_zone(self.network.time_zone).formatted_offset}" | |
| } | |
| } | |
| results = client.execute!( | |
| :api_method => service.events.update, | |
| :parameters => { | |
| :calendarId => 'primary', | |
| :eventId => "#{self.created_on.to_i}" | |
| }, | |
| :body_object => event) | |
| end |
This file contains hidden or 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
| def to_ics | |
| previous_trip = Assignment.where(client_id: self.client_id, guide_id: self.guide_id).last(2).first if !self | |
| description = "GuidePoint Items: #{}\nClient Note: #{self.notes}\nPrevious Trip Note: #{previous_trip.nil? ? "N/A" : previous_trip.notes}\nPrevious Trip Location: #{previous_trip.nil? ? 'N/A' : previous_trip.try(:section).try(:name)}\nMeeting Place: #{self.meeting_place}\nClient Phone Number: #{self.client.try(:mobile_phone)}" | |
| event = Icalendar::Event.new | |
| event.dtstart = self.assignment_date.strftime("%Y%m%dT%H%M%S") | |
| event.dtend = self.assignment_date.strftime("%Y%m%dT%H%M%S") | |
| # event.summary = self.title | |
| # event.client_note = self.notes | |
| # event.client_phone = self.client.mobile_phone | |
| event.description = description | |
| event.summary = self.try(:section).try(:name).to_s + " / " + self.client.full_name | |
| event.location = self.try(:meeting_place_text).try(:name) | |
| # event.klass = "PUBLIC" | |
| # event.created = self.created_at | |
| # event.last_modified = self.updated_at | |
| # event.uid = event.url = "#{PUBLIC_URL}events/#{self.id}" | |
| # event.add_comment("AF83 - Shake your digital, we do WowWare") | |
| event | |
| end |
This file contains hidden or 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
| if guide && (guide.user.access_token.present? && guide.user.refresh_token.present? && guide.user.gmail.present?) | |
| guide.refresh_access_token | |
| client = Google::APIClient.new | |
| client.authorization.access_token = guide.user.access_token | |
| service = client.discovered_api('calendar', 'v3') | |
| result = client.execute(:api_method => service.events.get, | |
| :parameters => {'calendarId' => 'primary', 'eventId' => "#{self.created_on.to_i}"}) | |
| event = result.data | |
| event = { | |
| 'summary' => "#{self.network.name} - #{self.start_time.in_time_zone('UTC').try(:strftime, '%I:%M %p')}", | |
| 'location' => "#{self.meeting_place_description}", | |
| 'description' => "#{self.network.name} - #{self.start_time.in_time_zone('UTC').try(:strftime, '%I:%M %p')} \nClient Name: #{self.client.full_name}\n#{self.half_day? ? 'Half Day' : (self.partial? ? 'Partial' : 'Full Day')}\nLocation: #{self.section.river.name} - #{self.section.name}", | |
| 'start' => { | |
| 'dateTime' => "#{self.assignment_date.strftime('%Y-%m-%dT')}#{self.start_time.in_time_zone('UTC').strftime('%H:%M:00')}#{Time.now.in_time_zone(self.network.time_zone).formatted_offset}" | |
| }, | |
| 'end' => { | |
| 'dateTime' => "#{self.assignment_date.strftime('%Y-%m-%dT')}#{self.end_time.in_time_zone('UTC').strftime('%H:%M:00')}#{Time.now.in_time_zone(self.network.time_zone).formatted_offset}" | |
| } | |
| } | |
| results = client.execute!( | |
| :api_method => service.events.update, | |
| :parameters => { | |
| :calendarId => 'primary', | |
| :eventId => "#{self.created_on.to_i}" | |
| }, | |
| :body_object => event) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment