Current section

Files

Jump to
indieweb lib indieweb url.ex
Raw

lib/indieweb/url.ex

defmodule IndieWeb.URL do
@doc "Transforms a string into a valid URL."
@spec canonalize(binary()) :: binary()
def canonalize(url)
def canonalize(url) when is_binary(url) do
url |> URI.parse() |> canonalize
end
def canonalize(%URI{path: nil} = url) do
url |> URI.merge("/") |> canonalize
end
def canonalize(%URI{} = url) do
URI.to_string(url)
end
@doc "Resolves any redirection on the provided URL."
@spec resolve_redirect(binary()) :: binary()
def resolve_redirect(url) do
case IndieWeb.Http.get(url) do
{:ok, %{url: resolved_url}} -> resolved_url
_ -> url
end
end
end