Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/icalendar.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ defimpl ICalendar.Serialize, for: ICalendar do
vendor = Keyword.get(options, :vendor, "Elixir ICalendar")
name = Keyword.get(options, :name)
id = Keyword.get(options, :id)
headers = Keyword.get(options, :headers, [])

[vendor: vendor, name: name, id: id]
[vendor: vendor, name: name, id: id] ++ headers
|> Enum.reject(fn {_, v} -> is_nil(v) or v == "" end)
|> Enum.map(&attribute/1)
|> Enum.join("\n")
Expand All @@ -65,4 +66,5 @@ defimpl ICalendar.Serialize, for: ICalendar do
defp attribute({:vendor, vendor}), do: "PRODID:-//Elixir ICalendar//#{vendor}//EN"
defp attribute({:name, name}), do: "X-WR-CALNAME:#{name}"
defp attribute({:id, id}), do: "X-WR-RELCALID:#{id}"
defp attribute({key, value}), do: "#{key}:#{value}"
end
2 changes: 2 additions & 0 deletions lib/icalendar/serialize.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ defprotocol ICalendar.Serialize do
`X-WR-CALNAME:Calendar Name`.
* `id` a string containing the calendar's id. Will produce
`X-WR-RELCALID:123456`.
* `headers` a keyword list containing the headers to
be placed in the calendar header: `[{"Method", "Request"}]`
"""
def to_ics(data, options \\ [])
end
Expand Down
13 changes: 13 additions & 0 deletions test/icalendar_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ defmodule ICalendarTest do
"""
end

test "ICalendar.to_ics/1 of empty calendar with custom headers" do
ics = %ICalendar{} |> ICalendar.to_ics(headers: [{"METHOD", "REQUEST"}])

assert ics == """
BEGIN:VCALENDAR
CALSCALE:GREGORIAN
VERSION:2.0
PRODID:-//Elixir ICalendar//Elixir ICalendar//EN
METHOD:REQUEST
END:VCALENDAR
"""
end

test "ICalendar.to_ics/1 of a calendar with an event, as in README" do
events = [
%ICalendar.Event{
Expand Down