-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.rb
65 lines (53 loc) · 1.96 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
require 'sinatra'
require 'json'
require './timetabler'
class Octangles < Sinatra::Base
configure do
set :sort_options, {'days' => 'Least days at uni',
'hours' => 'Least hours at uni',
'sleep_in_time' => 'Sleep in time',
'end_time' => 'Earlier end times',
'hours_sd' => 'Minimise differences between hours each day'}
end
helpers do
def get_params
@timetables = []
@input_courses = params[:courses]
@clash = params[:clash]
@sort_by = params[:sort_by]
@sort_by_ordered = params[:sort_by_ordered]
@force_courses = params[:force_courses]
@force_course_times = params[:force_course_times]
@include_closed = params[:include_closed] == 'true' ? true : false
@sort_options = settings.sort_options
end
end
get '/' do
@title = "Octangles"
get_params
erb :index
end
post '/generate.json' do
content_type :json
get_params
courses = []
warnings = []
course_names = @input_courses.split(',').map{|x| x.strip.upcase}.select{|x| x != '' }.uniq
course_names.each do |c|
new_course = Course.new(c, warnings, :include_closed =>
@include_closed)
if new_course.activities != {}
courses << new_course
else
warnings << "No classes found for #{c}"
end
end
force_courses = @force_courses.split(',');
force_course_times = @force_course_times.split(',');
timetables = Timetabler::generate(courses, :clash => @clash.to_i,
:sort_by => @sort_by_ordered,
:force_courses =>
force_courses.zip(force_course_times))
{:timetables => timetables, :courses => course_names, :warnings => warnings.uniq}.to_json
end
end