Add HTTP wrap func and Text Encodable:

- Refactor HTTP send func
- Impl. Text Encodable for URL
- Error when url is not found
This commit is contained in:
mitchell 2020-01-01 18:11:38 -05:00
parent b699e661e2
commit 1502d10cdc
8 changed files with 84 additions and 41 deletions

View file

@ -20,21 +20,24 @@ defmodule Shortnr.URL do
@spec create(String.t(), module()) :: {:ok, String.t()} | Transport.error()
def create(url, repo) do
url_struct = %__MODULE__{id: Util.gen_id(), url: URI.parse(url)}
url_struct = %__MODULE__{id: Util.generate_id(), url: URI.parse(url)}
{:ok, extant_url} = repo.get(url_struct.id)
if is_nil(extant_url) do
if extant_url do
create(url, repo)
else
:ok = repo.put(url_struct)
{:ok, url_struct.id}
else
create(url, repo)
end
end
@spec get(String.t(), module()) :: {:ok, t()} | Transport.error()
def get(key, repo) do
{:ok, _} = repo.get(key)
case repo.get(key) do
{:ok, nil} -> {:error, {:not_found, "url could not be found with the given id"}}
{:ok, url} -> {:ok, url}
end
end
@spec list(module()) :: {:ok, list(t())} | Transport.error()
@ -48,11 +51,17 @@ defmodule Shortnr.URL do
{:ok, "Success"}
end
defimpl Transport.Text.Encodable do
alias Shortnr.URL
@spec encode(URL.t()) :: String.t()
def encode(url), do: URI.to_string(url.url)
end
defimpl String.Chars do
alias Shortnr.URL
@spec to_string(URL.t()) :: String.t()
def to_string(url) do
"id=#{url.id} created=#{url.created_at} updated=#{url.updated_at} url=#{url.url}"
"id=#{url.id} created_at=#{url.created_at} updated_at=#{url.updated_at} url=#{url.url}"
end
end
end

View file

@ -3,8 +3,8 @@ defmodule Shortnr.URL.Util do
URL module utility functions.
"""
@id_chars String.codepoints("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXWYZ0123456789")
@spec gen_id() :: String.t()
def gen_id do
@spec generate_id() :: String.t()
def generate_id do
for _ <- 0..7,
into: "",
do: Enum.random(@id_chars)