48 lines
1.4 KiB
Elm
48 lines
1.4 KiB
Elm
module ClassSchedule.Decode exposing (..)
|
|
import ClassSchedule.Model exposing (..)
|
|
import Json.Decode exposing (..)
|
|
|
|
dayOfWeek : Decoder DayOfWeek
|
|
dayOfWeek =
|
|
let
|
|
runDayOfWeek s =
|
|
case s of
|
|
"M" -> succeed Monday
|
|
"T" -> succeed Tuesday
|
|
"W" -> succeed Wednesday
|
|
"R" -> succeed Thursday
|
|
"F" -> succeed Friday
|
|
_ -> fail "Not a valid day of the week!"
|
|
in
|
|
string |> andThen runDayOfWeek
|
|
|
|
duration : Decoder (Time, Time)
|
|
duration =
|
|
let mkRange i j = (from24 i, addMinutes (round (j*60)) (from24 i))
|
|
in field "hour" int |> andThen (\i -> field "duration" float |> map (mkRange i))
|
|
|
|
time : Decoder (DayOfWeek, Time, Time)
|
|
time = map2 (\x (y, z) -> (x, y, z))
|
|
(field "day" dayOfWeek)
|
|
duration
|
|
|
|
crn : Decoder Crn
|
|
crn = string
|
|
|> andThen (\s ->
|
|
case String.split " " s of
|
|
[c, is] ->
|
|
case String.toInt is of
|
|
Just i -> succeed (c, i)
|
|
Nothing -> fail "Invalid course number!"
|
|
_ -> fail "Invalid course code!")
|
|
|
|
course : Decoder Course
|
|
course = map4 Course
|
|
(field "code" crn)
|
|
(field "title" string)
|
|
(field "instructor" (map List.singleton string))
|
|
(field "times" (list time))
|
|
|
|
response : Decoder (List Course)
|
|
response = field "courses" (list course)
|