Packages

A wrapper for the Tibia Data API.

Current section

Files

Jump to
tibia_data lib util.ex
Raw

lib/util.ex

defmodule Util do
@moduledoc """
Util module containing helpful functions used in the project.
"""
@doc """
Function for format string and replace spaces with '+' for url format.
## Parameters
- text (string): Text to be formatted in the url format.
## Examples
iex> Util.format_string("Macabra Pune")
"Macabra+Pune"
"""
def format_string(text) do
text
|> String.trim
|> String.replace(" ", "+")
end
@doc """
Function to extract the body from the response hash map.
## Parameters
- response (HTTPoison.Response): A response from httpoison request.
## Examples
iex> Util.extract_body(response)
{:ok, body}
"""
def extract_body(response) do
case response do
%HTTPoison.Response{body: body, status_code: 200} ->
{:ok, body}
%HTTPoison.Response{body: _, status_code: status_code} ->
{:error, "Error #{status_code} in request."}
_ ->
{:error, "Unable to parse response."}
end
end
@doc """
Function to force extract the full body from the response hash map.
## Parameters
- response (HTTPoison.Response): A response from httpoison request.
## Examples
iex> Util.extract_body!(response)
body
"""
def extract_body!(response) do
case response do
%HTTPoison.Response{body: body, status_code: 200} ->
body
%HTTPoison.Response{body: _, status_code: _} ->
:error
_ ->
:error
end
end
end