schedules_controller.rb 1.46 KB
Newer Older
1
2
3
4
5
require 'icalendar'
require 'time'

# Contains functionality for generating schedules.
class SchedulesController < ApplicationController
Zac Wood's avatar
Zac Wood committed
6
7
8
  resource_description do
    short 'Endpoints for generating iCal files'
  end
Zac Wood's avatar
Zac Wood committed
9
  # Render an iCal file containing the schedules of all the
10
  # course sections with the given CRNs.
Zac Wood's avatar
Zac Wood committed
11
  api :GET, '/schedules', 'Generate an iCal file with events for the given CRNs'
Zac Wood's avatar
Zac Wood committed
12
  param :crns, String, desc: 'Comma separated list of CRNs to include as events in the calendar', required: true
13
14
15
16
17
  def index
    crns = params["crns"].split ','
    @schedule = Schedule.new crns
    render plain: @schedule.to_ical # render a plaintext iCal file
  end
Zac Wood's avatar
Zac Wood committed
18

Zac Wood's avatar
Zac Wood committed
19
20
21
22
23
24
25
26
27
  DAYS = {
    "M": Date.new(2019, 1, 14),
    "T": Date.new(2019, 1, 15),
    "W": Date.new(2019, 1, 16),
    "R": Date.new(2019, 1, 17),
    "F": Date.new(2019, 1, 18),
    "S": Date.new(2019, 1, 19),
    "U": Date.new(2019, 1, 20)
  }.freeze
Zac Wood's avatar
Zac Wood committed
28

Zac Wood's avatar
Zac Wood committed
29
  def show
Zac Wood's avatar
Zac Wood committed
30
31
32
33
    all_sections = @cart.values
    # schedules = []
    all_sections.each_with_index do |sections, i|
    end
Zac Wood's avatar
Zac Wood committed
34
35
    @events = @cart.map do |_cid, sections|
      s = sections.first
Zac Wood's avatar
Zac Wood committed
36

Zac Wood's avatar
Zac Wood committed
37
38
39
40
      s.days.split('').map do |day|
        formatted_date = DAYS[day.to_sym].to_s.tr('-', '')
        time = Time.parse(s.start_time).strftime("%H%M%S")
        endtime = Time.parse(s.end_time).strftime("%H%M%S")
Zac Wood's avatar
Zac Wood committed
41

Zac Wood's avatar
Zac Wood committed
42
43
44
45
46
47
48
        {
          title: s.name,
          start: "#{formatted_date}T#{time}",
          end: "#{formatted_date}T#{endtime}"
        }
      end
    end.flatten
Zac Wood's avatar
Zac Wood committed
49
  end
50
end