diff --git a/lib/icalendar.ex b/lib/icalendar.ex index a3373b9..04fda37 100644 --- a/lib/icalendar.ex +++ b/lib/icalendar.ex @@ -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") @@ -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 diff --git a/lib/icalendar/serialize.ex b/lib/icalendar/serialize.ex index 455b589..0b3228b 100644 --- a/lib/icalendar/serialize.ex +++ b/lib/icalendar/serialize.ex @@ -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 diff --git a/test/icalendar_test.exs b/test/icalendar_test.exs index 5473140..e48953e 100644 --- a/test/icalendar_test.exs +++ b/test/icalendar_test.exs @@ -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{