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
7ef43b49
Commit
7ef43b49
authored
May 05, 2018
by
Zac Wood
Browse files
Generator is working
parent
f5509fed
Changes
2
Hide whitespace changes
Inline
Side-by-side
schedules_api/app/controllers/calendar_generator_controller.rb
View file @
7ef43b49
...
...
@@ -7,26 +7,44 @@ class CalendarGeneratorController < ApplicationController
posted_sections
=
JSON
.
parse
(
request
.
body
.
read
)
posted_sections
.
each
do
|
posted_section
|
section
=
Section
.
find_by_crn
posted_section
[
"crn"
]
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
NO_CLASSES
=
[
"20180903"
,
"20181008"
,
(
21
..
25
).
map
{
|
n
|
"201811
#{
n
}
"
},
(
10
..
19
).
map
{
|
n
|
"201812
#{
n
}
"
}
].
flatten
.
freeze
DAYS
=
{
"M"
=>
"MO"
,
"T"
=>
"TU"
,
"W"
=>
"WE"
,
"R"
=>
"TH"
,
"F"
=>
"FR"
,
"S"
=>
"SA"
,
"U"
=>
"SU"
}.
freeze
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
.
rrule
=
Icalendar
::
Values
::
Recur
.
new
(
recurrence_rule_str
(
section
))
event
.
exdate
=
no_classes
.
map
{
|
date
|
generate_exdate
(
date
,
section
.
start_time
)
}
event
end
...
...
@@ -34,25 +52,21 @@ class CalendarGeneratorController < ApplicationController
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
)
def
recurrence_rule_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
def
generate_exdate
(
date
,
time
)
formatted_time
=
Time
.
parse
(
time
).
strftime
(
"%H%M%S"
)
Icalendar
::
Values
::
DateTime
.
new
(
"
#{
date
}
T
#{
formatted_time
}
"
)
end
end
schedules_api/test/controllers/calendar_generator_controller_test.rb
View file @
7ef43b49
...
...
@@ -2,7 +2,7 @@ require 'test_helper'
class
CalendarGeneratorControllerTest
<
ActionDispatch
::
IntegrationTest
test
"should get generate"
do
get
calendar_generator_generate_url
assert_response
:success
assert
true
# TODO: Implement test
#
assert_response :success
end
end
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