Commit f5509fed authored by Zac Wood's avatar Zac Wood

Basic iCal generator working

parent d76e3910
# Configures the application.
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
protect_from_forgery with: :null_session
end
require 'icalendar'
require 'time'
class CalendarGeneratorController < ApplicationController
def generate; end
def generate
cal = Icalendar::Calendar.new
posted_sections = JSON.parse(request.body.read)
posted_sections.each do |posted_section|
section = Section.find_by_crn posted_section["crn"]
event = generate_event_from_section(section)
cal.add_event(event)
end
puts cal.to_ical
render plain: cal.to_ical
end
private
def generate_event_from_section(section)
event = Icalendar::Event.new
event.summary = section.name
event.description = section.title
event.dtstart = Icalendar::Values::DateTime.new(formatted_datetime_str(section.start_date, section.start_time))
event.dtend = Icalendar::Values::DateTime.new(formatted_datetime_str(section.start_date, section.end_time))
event.rrule = Icalendar::Values::Recur.new(recur_str(section))
event
end
def formatted_datetime_str(date, time)
formatted_date = date.to_s.tr('-', '')
formatted_time = Time.parse(time).strftime("%H%M%S")
"#{formatted_date}T#{formatted_time}"
end
DAYS = {
"M" => "MO",
"T" => "TU",
"W" => "WE",
"R" => "TH",
"F" => "FR",
"S" => "SA",
"U" => "SU"
}.freeze
def recur_str(section)
days = section.days.split("").map do |day|
DAYS[day]
end
"FREQ=WEEKLY;UNTIL=#{formatted_datetime_str(section.end_date, section.end_time)};BYDAY=#{days.join(',')}"
end
end
......@@ -6,7 +6,7 @@ Rails.application.routes.draw do
end
get 'search', controller: 'search', action: 'index'
get 'generate', controller: 'calendar_generator', action: 'generate'
post 'generate', controller: 'calendar_generator', action: 'generate'
end
root 'courses#index' # Set the root to be the courses API endpoint
......
......@@ -13,6 +13,7 @@ class App extends React.Component<any, State> {
this.state = { currentSchedule: [] };
this.addSectionToCurrentScheduleIfUnique = this.addSectionToCurrentScheduleIfUnique.bind(this);
this.generateSchedule = this.generateSchedule.bind(this);
}
addSectionToCurrentScheduleIfUnique(section: Section) {
......@@ -23,12 +24,25 @@ class App extends React.Component<any, State> {
}
}
generateSchedule() {
fetch('http://localhost:3000/api/generate', {
method: 'POST',
body: JSON.stringify(this.state.currentSchedule),
headers: {
'Content-Type': 'text/plain',
},
})
.then(response => response.text())
.then(text => console.log(text));
}
render() {
return (
<div>
<h1>Schedules</h1>
<Search addSearchResultCallback={this.addSectionToCurrentScheduleIfUnique} />
<SectionList sections={this.state.currentSchedule} />
<button onClick={this.generateSchedule}>Generate Schedule</button>
</div>
);
}
......
......@@ -13,6 +13,7 @@ export default class SectionList extends React.Component<Props, any> {
this.renderSelectSectionColumn = this.renderSelectSectionColumn.bind(this);
this.getSectionWithCRN = this.getSectionWithCRN.bind(this);
}
render() {
return (
<table>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment