-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add fantasy league calendar download
- Loading branch information
Showing
6 changed files
with
212 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
defmodule Ex338.ICalendar do | ||
@moduledoc false | ||
|
||
defmodule Event do | ||
@moduledoc false | ||
defstruct summary: nil, | ||
dtstart: nil, | ||
dtend: nil, | ||
dtstamp: nil, | ||
description: nil, | ||
uid: nil | ||
end | ||
|
||
def to_ics(events) when is_list(events) do | ||
events = Enum.map(events, &to_ics/1) | ||
|
||
""" | ||
BEGIN:VCALENDAR | ||
CALSCALE:GREGORIAN | ||
VERSION:2.0 | ||
PRODID:-//Ex338 ICalendar//EN | ||
#{events}END:VCALENDAR | ||
""" | ||
end | ||
|
||
def to_ics(event) do | ||
contents = to_kvs(event) | ||
|
||
""" | ||
BEGIN:VEVENT | ||
#{contents}END:VEVENT | ||
""" | ||
end | ||
|
||
defp to_kvs(event) do | ||
event | ||
|> Map.from_struct() | ||
|> Enum.map(&to_kv/1) | ||
|> List.flatten() | ||
|> Enum.sort() | ||
|> Enum.join() | ||
end | ||
|
||
defp to_kv({key, value}) do | ||
name = | ||
key | ||
|> to_string() | ||
|> String.upcase() | ||
|
||
build(name, value) | ||
end | ||
|
||
def build(_key, nil) do | ||
"" | ||
end | ||
|
||
def build(key, %Date{} = date) do | ||
"#{key}:#{Date.to_iso8601(date, :basic)}\n" | ||
end | ||
|
||
def build(key, %DateTime{} = datetime) do | ||
datetime = | ||
datetime | ||
|> DateTime.truncate(:second) | ||
|> DateTime.to_iso8601(:basic) | ||
|
||
"#{key}:#{datetime}\n" | ||
end | ||
|
||
def build(key, value) do | ||
"#{key}:#{value}\n" | ||
end | ||
end |
16 changes: 16 additions & 0 deletions
16
lib/ex338_web/controllers/fantasy_league/calendar_download_controller.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
defmodule Ex338Web.FantasyLeague.CalendarDownloadController do | ||
use Ex338Web, :controller | ||
|
||
alias Ex338.FantasyLeagues | ||
|
||
def show(conn, %{"fantasy_league_id" => fantasy_league_id}) do | ||
calendar = FantasyLeagues.generate_calendar(fantasy_league_id) | ||
|
||
send_download( | ||
conn, | ||
{:binary, calendar}, | ||
content_type: "text/calendar", | ||
filename: "338_calendar.ics" | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
test/ex338_web/controllers/fantasy_league/calendar_download_controller_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
defmodule Ex338Web.FantasyLeague.CalendarDownloadTest do | ||
use Ex338Web.ConnCase | ||
|
||
setup :register_and_log_in_user | ||
|
||
describe "get/2" do | ||
test "allows a user to download the calendar of events for a fantasy league", %{conn: conn} do | ||
fantasy_league = insert(:fantasy_league, year: 2017) | ||
sports_league = insert(:sports_league) | ||
insert(:league_sport, fantasy_league: fantasy_league, sports_league: sports_league) | ||
insert(:league_sport, fantasy_league: fantasy_league, sports_league: sports_league) | ||
championship = insert(:championship, sports_league: sports_league) | ||
|
||
_championship_event = | ||
insert(:championship, | ||
sports_league: sports_league, | ||
overall: championship, | ||
category: "event" | ||
) | ||
|
||
conn = get(conn, ~p"/fantasy_leagues/#{fantasy_league.id}/calendar_download") | ||
|
||
assert response_content_type(conn, :ics) | ||
end | ||
end | ||
end |