lpil / icalendar

🗓️ A small library for reading and writing ICalendar files.
MIT License
103 stars 56 forks source link

Phoenix view to output .ics format #21

Closed pinx closed 6 years ago

pinx commented 6 years ago

To create an ics feed, we could hook into https://hexdocs.pm/phoenix/views.html. Maybe the trick is to define a format_encoder for ics? https://hexdocs.pm/phoenix/Phoenix.Template.html#module-format-encoders

lpil commented 6 years ago

This could be an additional package, but I don't want to have any support for Phoenix or any other library built in.

walter commented 6 years ago

Once you have icalendar in your Phoenix app, It's really simple to implement returning ics. Not sure it requires anything more than addition to README under Usage.

Note I use the same pipeline as html and detect format rather than create a separate pipeline which has been overkill for my case.

# lib/your_app_web/router.ex
  pipeline :browser do
    plug :accepts, ["html", "ics"]
...

# corresponding controller action needs to use symbol for action in render call
# so that corresponding template for format is matched against `action.ics.eex`
def show(conn, %{"id" => id}) do
    ...
    render(conn, :show, ...)
 end

# you can then have an ics template, but doesn't make much sense
# so I use pattern matching to override the ics case in the corresponding view
def render("show.ics", %{conn: conn, ...}) do
  event = %ICalendar.Event{...}
   %ICalendar{events: [event]} |> ICalendar.to_ics |> String.replace(~r/\n/, "\r\n")
end
pinx commented 6 years ago

I experimented a bit, and made a PR to describe what it could look like: #22

lpil commented 6 years ago

After seeing the PR I think this would be a good addition. Thanks :)

lpil commented 6 years ago

I've released a new version with this new feature

walter commented 6 years ago

I experimented a bit, and made a PR to describe what it could look like: #22

Yeah, small change that makes it even cleaner. Good work.