class MyBanner::CalendarService

Attributes

calendar_name[RW]
location[RW]
meetings[RW]
section[RW]
time_zone[RW]

Public Class Methods

new(section) click to toggle source
# File lib/my_banner/calendar_service.rb, line 6
def initialize(section)
  @section = section
  validate_section
  @calendar_name = section.abbreviation
  @time_zone = section.time_zone
  @location = section.location
  @meetings = section.meetings
end

Public Instance Methods

calendar() click to toggle source
# File lib/my_banner/calendar_service.rb, line 26
def calendar
  @calendar ||= (find_calendar || create_calendar)
end
client() click to toggle source
# File lib/my_banner/calendar_service.rb, line 30
def client
  @client = CalendarClient.new
end
events() click to toggle source
# File lib/my_banner/calendar_service.rb, line 22
def events
  @events ||= client.upcoming_events(calendar)
end
execute() click to toggle source
# File lib/my_banner/calendar_service.rb, line 15
def execute
  meetings.map do |meeting|
    event = find_event(meeting.to_h)
    event ? update_event(event, meeting.to_h) : create_event(meeting.to_h)
  end
end

Private Instance Methods

calendar_attributes() click to toggle source
# File lib/my_banner/calendar_service.rb, line 105
def calendar_attributes
  { summary: calendar_name, time_zone: time_zone }
end
create_calendar() click to toggle source
# File lib/my_banner/calendar_service.rb, line 97
def create_calendar
  client.insert_calendar(new_calendar)
end
create_event(meeting_attrs) click to toggle source
# File lib/my_banner/calendar_service.rb, line 63
def create_event(meeting_attrs)
  client.insert_event(calendar.id, new_event(meeting_attrs))
end
delete_events() click to toggle source

EVENT OPERATIONS

# File lib/my_banner/calendar_service.rb, line 40
def delete_events
  events.map { |event| client.delete_event(calendar.id, event.id) }
end
event_attributes(meeting_attrs) click to toggle source
# File lib/my_banner/calendar_service.rb, line 71
def event_attributes(meeting_attrs)
  {
    summary: calendar_name,
    location: location,
    start: {
      date_time: meeting_attrs[:start_at].strftime("%Y-%m-%-dT%H:%M:%S"), # excludes offset, regardless of tz presence, to avoid maladjustment
      time_zone: time_zone
    },
    end: {
      date_time: meeting_attrs[:end_at].strftime("%Y-%m-%-dT%H:%M:%S"), # excludes offset, regardless of tz presence, to avoid maladjustment
      time_zone: time_zone
    },
    # description: "Agenda: https://.../units/1 \n \n Objectives: \n 1: ....  \n 2: ....  \n 3: ....", # todo
    # attendees: ["hello@gmail.com", "prof@my-school.edu", "student@my-school.edu"],
    # source: {title: "External link", url: "https://.../units/1"}
  }
end
find_calendar() click to toggle source

CALENDAR OPERATIONS

# File lib/my_banner/calendar_service.rb, line 93
def find_calendar
  client.calendars.find { |cal| cal.summary == calendar_name }
end
find_event(meeting_attrs) click to toggle source
# File lib/my_banner/calendar_service.rb, line 48
def find_event(meeting_attrs)
  events.find do |e|
    # match datetime events
    (
      e.start.date_time.try(:strftime, "%Y-%m-%dT%H:%M:%S") == meeting_attrs[:start_at].try(:strftime, "%Y-%m-%dT%H:%M:%S") &&
      e.end.date_time.try(:strftime, "%Y-%m-%dT%H:%M:%S") == meeting_attrs[:end_at].try(:strftime, "%Y-%m-%dT%H:%M:%S")
    ) ||
    # match date events
    (
      e.start.date.try(:strftime, "%Y-%m-%dT%H:%M:%S") == meeting_attrs[:start_at].try(:strftime, "%Y-%m-%dT%H:%M:%S") &&
      e.end.date.try(:strftime, "%Y-%m-%dT%H:%M:%S") == meeting_attrs[:end_at].try(:strftime, "%Y-%m-%dT%H:%M:%S")
    )
  end
end
new_calendar() click to toggle source
# File lib/my_banner/calendar_service.rb, line 101
def new_calendar
  Google::Apis::CalendarV3::Calendar.new(calendar_attributes)
end
new_event(meeting_attrs) click to toggle source
# File lib/my_banner/calendar_service.rb, line 67
def new_event(meeting_attrs)
  Google::Apis::CalendarV3::Event.new(event_attributes(meeting_attrs))
end
update_event(event, meeting_attrs) click to toggle source
# File lib/my_banner/calendar_service.rb, line 44
def update_event(event, meeting_attrs)
  client.update_event(calendar.id, event.id, new_event(meeting_attrs))
end
validate_section() click to toggle source
# File lib/my_banner/calendar_service.rb, line 111
def validate_section
  raise "OOPS, expecting a section object" unless section && section.is_a?(Section)
end