Setup Dialyxir and Credo code analyzers:

- Refactor codebase according to analyzers
This commit is contained in:
mitchell 2019-12-30 21:50:39 -05:00
parent 593f29d271
commit b699e661e2
15 changed files with 74 additions and 44 deletions

View file

@ -1,8 +1,13 @@
defmodule Shortnr.Transport.HTTP do
@moduledoc """
This module contains functions that can be used to handle HTTP requests and send responses, by
manipulating Plug.Conn.
"""
import Plug.Conn
@type ok_error :: {:ok, term(), Plug.Conn.t()} | error()
@type error :: {:error, {atom(), String.t()}, Plug.Conn.t()}
@type ok_error :: {:ok, term(), Plug.Conn.t()} | error()
@spec handle(ok_error(), (... -> ok_error())) :: ok_error()
def handle(error = {:error, _sub_error, _conn}, _func), do: error

View file

@ -1,28 +0,0 @@
defmodule Shortnr.Transport.Json do
import Plug.Conn
alias Shortnr.Transport.HTTP
@spec decode_request(Plug.Conn.t(), module()) :: HTTP.ok_error()
def decode_request(conn, struct_module) do
{:ok, body, conn} = read_body(conn)
{:ok, params} = Jason.decode(body)
params_list =
params
|> Map.to_list()
|> Enum.map(fn {key, value} -> {String.to_atom(key), value} end)
{:ok, struct(struct_module, params_list), conn}
end
@spec encode_response(HTTP.ok_error()) :: HTTP.ok_error()
def encode_response({:error, {status, response}, conn}) do
{:ok, json_body} = Jason.encode(%{data: response})
{:error, {status, json_body}, conn}
end
def encode_response({:ok, response, conn}) do
{:ok, json_body} = Jason.encode(%{data: response})
{:ok, json_body, conn}
end
end

View file

@ -1,4 +1,8 @@
defmodule Shortnr.Transport.Text do
@moduledoc """
This modules contains functions to decode and encode text formatted http requests and responses.
"""
import Plug.Conn
alias Shortnr.Transport.HTTP
alias Shortnr.URL

View file

@ -1,4 +1,7 @@
defmodule Shortnr.Transport do
@moduledoc """
This module houses generic Transport types.
"""
@type error :: {:error, {atom(), String.t()}}
@type ok_error :: {:ok, term()} | error()
end