Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
SRCT
schedules
Commits
f5509fed
Commit
f5509fed
authored
May 02, 2018
by
Zac Wood
Browse files
Basic iCal generator working
parent
d76e3910
Changes
5
Hide whitespace changes
Inline
Side-by-side
schedules_api/app/controllers/application_controller.rb
View file @
f5509fed
# Configures the application.
class
ApplicationController
<
ActionController
::
Base
protect_from_forgery
with: :
except
ion
protect_from_forgery
with: :
null_sess
ion
end
schedules_api/app/controllers/calendar_generator_controller.rb
View file @
f5509fed
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
schedules_api/config/routes.rb
View file @
f5509fed
...
...
@@ -6,7 +6,7 @@ Rails.application.routes.draw do
end
get
'search'
,
controller:
'search'
,
action:
'index'
ge
t
'generate'
,
controller:
'calendar_generator'
,
action:
'generate'
pos
t
'generate'
,
controller:
'calendar_generator'
,
action:
'generate'
end
root
'courses#index'
# Set the root to be the courses API endpoint
...
...
schedules_web/src/components/App.tsx
View file @
f5509fed
...
...
@@ -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
>
);
}
...
...
schedules_web/src/components/SectionList.tsx
View file @
f5509fed
...
...
@@ -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
>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment