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

Commit

Permalink
Extracts normalize_url to utility function and adds doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
jamonholmgren committed Mar 14, 2017
1 parent ad854f2 commit 273a75f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
4 changes: 1 addition & 3 deletions lib/thesis/controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ defmodule Thesis.Controller.Plug do
end

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

Expand Down
15 changes: 15 additions & 0 deletions lib/thesis/utilities.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Thesis.Utilities do
Module that provides helper functions.
"""


def parameterize(str) do
str = Regex.replace(~r/[^a-z0-9\-\s\.]/i, str, "")
Regex.split(~r/\%20|\s/, str)
Expand All @@ -18,4 +19,18 @@ defmodule Thesis.Utilities do
|> String.downcase
|> binary_part(0, length)
end

@doc """
Takes a URL and strips unnecessary characters.
iex> Thesis.Utilities.normalize_url("http://infinite.red//ignite//foo")
"http://infinite.red/ignite/foo"
iex> Thesis.Utilities.normalize_url("https://infinite.red/ignite/foo/")
"https://infinite.red/ignite/foo"
"""
def normalize_url(url) do
url
|> String.replace(~r/(?<=[^:])(\/\/)/, "/") # Strip double slashes
|> String.replace(~r/\/$/, "") # Strip trailing slash
end
end
4 changes: 4 additions & 0 deletions test/utilities_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule UtilitiesTest do
use ExUnit.Case
doctest Thesis.Utilities
end

0 comments on commit 273a75f

Please sign in to comment.