Current section
Files
Jump to
Current section
Files
lib/webuntis/struct/period.ex
defmodule Webuntis.Struct.Period do
@code_regular "REGULAR"
@code_cancelled "CANCELLED"
@code_irregular "IRREGULAR"
@code_exam "EXAM"
defstruct [
:id,
:lesson_id,
:start_date_time,
:end_date_time,
:fore_color,
:back_color,
:inner_fore_color,
:inner_back_color,
:text,
:elements,
:can,
:is,
:homework,
:messenger_channel,
:exam
]
@doc since: "2.0.0"
def from_map(jason_map) do
%Webuntis.Struct.Period{
id: Map.fetch!(jason_map, "id"),
lesson_id: Map.fetch!(jason_map, "lessonId"),
start_date_time: Map.fetch!(jason_map, "startDateTime") |> webuntis_to_iso(),
end_date_time: Map.fetch!(jason_map, "endDateTime") |> webuntis_to_iso(),
fore_color: Map.fetch!(jason_map, "foreColor"),
back_color: Map.fetch!(jason_map, "backColor"),
inner_fore_color: Map.fetch!(jason_map, "innerForeColor"),
inner_back_color: Map.fetch!(jason_map, "innerBackColor"),
text: Map.fetch!(jason_map, "text"),
elements:
Map.fetch!(jason_map, "elements")
|> Enum.map(fn element -> Webuntis.Parser.parse_element(element) end),
can: Map.fetch!(jason_map, "can"),
is: Map.fetch!(jason_map, "is"),
homework: Map.fetch!(jason_map, "homeWorks"),
messenger_channel: Map.fetch!(jason_map, "messengerChannel"),
exam: Map.fetch!(jason_map, "exam")
}
end
def regular?(period) do
Enum.at(period.is, 0) == @code_regular
end
def special?(period) do
Enum.member?(period.is, @code_irregular) ||
Enum.member?(period.is, @code_exam)
end
def cancelled?(period) do
Enum.member?(period.is, @code_cancelled)
end
defp webuntis_to_iso(webuntis_date) do
iso =
webuntis_date
|> String.replace("Z", ":00Z")
{:ok, date, _utc_offset} = DateTime.from_iso8601(iso)
date
end
end