Skip to content
This repository was archived by the owner on Mar 10, 2021. It is now read-only.

Commit 273a75f

Browse files
committed
Extracts normalize_url to utility function and adds doctests
1 parent ad854f2 commit 273a75f

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

lib/thesis/controller.ex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,7 @@ defmodule Thesis.Controller.Plug do
9090
end
9191

9292
def call(conn, _opts) do
93-
url = conn.request_path
94-
|> String.replace(~r/(?<=[^:])(\/\/)/, conn.request_path, "/") # Strip double slashes
95-
|> String.replace(~r/\/$/, "") # Strip trailing slash
93+
url = Thesis.Utilities.normalize_url(conn.request_path)
9694
current_page = store.page(url)
9795
page_contents = store.page_contents(current_page)
9896

lib/thesis/utilities.ex

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ defmodule Thesis.Utilities do
33
Module that provides helper functions.
44
"""
55

6+
67
def parameterize(str) do
78
str = Regex.replace(~r/[^a-z0-9\-\s\.]/i, str, "")
89
Regex.split(~r/\%20|\s/, str)
@@ -18,4 +19,18 @@ defmodule Thesis.Utilities do
1819
|> String.downcase
1920
|> binary_part(0, length)
2021
end
22+
23+
@doc """
24+
Takes a URL and strips unnecessary characters.
25+
26+
iex> Thesis.Utilities.normalize_url("http://infinite.red//ignite//foo")
27+
"http://infinite.red/ignite/foo"
28+
iex> Thesis.Utilities.normalize_url("https://infinite.red/ignite/foo/")
29+
"https://infinite.red/ignite/foo"
30+
"""
31+
def normalize_url(url) do
32+
url
33+
|> String.replace(~r/(?<=[^:])(\/\/)/, "/") # Strip double slashes
34+
|> String.replace(~r/\/$/, "") # Strip trailing slash
35+
end
2136
end

test/utilities_test.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
defmodule UtilitiesTest do
2+
use ExUnit.Case
3+
doctest Thesis.Utilities
4+
end

0 commit comments

Comments
 (0)