From f1f4e718c2ed8e6da92234ed645747b044618344 Mon Sep 17 00:00:00 2001 From: Nick Gartmann Date: Fri, 18 Jun 2021 08:23:44 -0500 Subject: [PATCH 1/3] Added support for arbitrary calendar headers --- lib/icalendar.ex | 4 +++- test/icalendar_test.exs | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/icalendar.ex b/lib/icalendar.ex index a3373b9..12d02f3 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/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{ From ff528061406d914b955a2088cdead8afcca1a5a6 Mon Sep 17 00:00:00 2001 From: Nick Gartmann Date: Fri, 18 Jun 2021 08:31:46 -0500 Subject: [PATCH 2/3] Added a bit of documentation --- lib/icalendar/serialize.ex | 2 ++ 1 file changed, 2 insertions(+) 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 From a5aece8b944667812a321f8878244f1072d01db3 Mon Sep 17 00:00:00 2001 From: Maarten Jacobs Date: Wed, 11 Sep 2024 16:34:25 +0200 Subject: [PATCH 3/3] syntax mistake --- lib/icalendar.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/icalendar.ex b/lib/icalendar.ex index 12d02f3..04fda37 100644 --- a/lib/icalendar.ex +++ b/lib/icalendar.ex @@ -57,7 +57,7 @@ defimpl ICalendar.Serialize, for: ICalendar do id = Keyword.get(options, :id) headers = Keyword.get(options, :headers, []) - [vendor: vendor, name: name, id: id | headers] + [vendor: vendor, name: name, id: id] ++ headers |> Enum.reject(fn {_, v} -> is_nil(v) or v == "" end) |> Enum.map(&attribute/1) |> Enum.join("\n")