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
23 changes: 23 additions & 0 deletions lib/today/links.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
defmodule Today.Links do
@moduledoc """
Module that deals with generating links for different social medias
"""

@doc """
Generates a list of tuples with the year and a twitter search link for a given account and year.

### Example

iex> generate_twitter_links("thejuliams", "2010")
"https://twitter.com/search?q=from%3Athejuliams%20since%3A2010-03-17%20until%3A2010-03-18&src=recent_search_click"


"""
@spec generate_twitter_link(String.t(), range :: String.t()) :: String.t()
def generate_twitter_link(account, year) do
today = Date.utc_today()
tomorrow = Date.utc_today() |> Date.add(1)

"https://twitter.com/search?q=from%3A#{account}%20since%3A#{year}-#{today.month}-#{today.day}%20until%3A#{year}-#{tomorrow.month}-#{tomorrow.day}&src=recent_search_click"
end
end
29 changes: 29 additions & 0 deletions lib/today_web/controllers/page_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,33 @@ defmodule TodayWeb.PageController do
def index(conn, _params) do
render(conn, "index.html")
end

def twitter(conn, _params) do
today = Date.utc_today()

assigns = %{
day: today.day,
month: today.month,
mooniunatik: %{username: "mooniunatik", years: ["2017", "2018", "2019", "2020", "2021"]},
thejuliams: %{
username: "thejuliams",
years: [
"2010",
"2011",
"2012",
"2013",
"2014",
"2015",
"2016",
"2017",
"2018",
"2019",
"2020",
"2021"
]
}
}

render(conn, "twitter.html", assigns)
end
end
1 change: 1 addition & 0 deletions lib/today_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ defmodule TodayWeb.Router do
pipe_through :browser

get "/", PageController, :index
get "/twitter", PageController, :twitter
end

# Other scopes may use custom stacks.
Expand Down
2 changes: 1 addition & 1 deletion lib/today_web/templates/layout/root.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<%= csrf_meta_tag() %>
<%= live_title_tag assigns[:page_title] || "Today", suffix: " · Phoenix Framework" %>
<%= live_title_tag assigns[:page_title] || "today" %>
<link phx-track-static rel="stylesheet" href={Routes.static_path(@conn, "/assets/app.css")}/>
<script defer phx-track-static type="text/javascript" src={Routes.static_path(@conn, "/assets/app.js")}></script>
</head>
Expand Down
8 changes: 8 additions & 0 deletions lib/today_web/templates/page/twitter.html.heex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<section class="row">
<article class="column">
<.display_years account={@thejuliams} day={@day} month={@month} />
</article>
<article class="column">
<.display_years account={@mooniunatik} day={@day} month={@month} />
</article>
</section>
22 changes: 22 additions & 0 deletions lib/today_web/views/page_view.ex
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
defmodule TodayWeb.PageView do
alias Today.Links

use TodayWeb, :view

def display_years(assigns) do
~H"""
<h2><%= @account.username %></h2>
<%= for year <- @account.years do %>
<h3><%= year %></h3>
<%= display_twitter_link(@account.username, year, @day, @month) %>
<p></p>
<% end %>

"""
end

def display_twitter_link(account, year, day, month) do
link = Links.generate_twitter_link(account, year)

~E"""
<p><a href="<%= link %>" target="_blank">These are your tweets for <%= day %>.<%= month %>.<%= year %></a></p>
"""
end
end
14 changes: 14 additions & 0 deletions test/today/links_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule Today.LinksTest do
@moduledoc false

use ExUnit.Case
alias Today.Links

test "when given an account and a year return a twitter search link today's day and month but in that year" do
today = Date.utc_today()
tomorrow = Date.utc_today() |> Date.add(1)

assert Links.generate_twitter_link("thejuliams", "2010") ==
"https://twitter.com/search?q=from%3Athejuliams%20since%3A2010-#{today.month}-#{today.day}%20until%3A2010-#{tomorrow.month}-#{tomorrow.day}&src=recent_search_click"
end
end
6 changes: 6 additions & 0 deletions test/today_web/controllers/page_controller_test.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
defmodule TodayWeb.PageControllerTest do
@moduledoc false
use TodayWeb.ConnCase

test "GET /", %{conn: conn} do
conn = get(conn, "/")
assert html_response(conn, 200) =~ "Welcome to Phoenix!"
end

test "GET /twitter", %{conn: conn} do
conn = get(conn, "/twitter")
assert html_response(conn, 200) =~ "These are your tweets for"
end
end