Skip to content

Commit

Permalink
handle more possible errors and implement more tests and coverage 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
ericklima-ca committed Oct 28, 2021
1 parent a59d4e1 commit 4f4330f
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 13 deletions.
16 changes: 12 additions & 4 deletions lib/get_geocode.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,21 @@ defmodule GetGeocode do
end

defp get_viacep(cep) do
ViaCep.get_cep(cep)
|> builder_from_viacep()
result = ViaCep.get_cep(cep)

case result do
%{"erro" => _} -> msg_invalid_input()
_ -> builder_from_viacep(result)
end
end

defp get_nominatim(addr) do
Nominatim.get_data(addr)
|> builder_from_nominatim()
result = Nominatim.get_data(addr)

case result do
{_, _} -> result
_ -> builder_from_nominatim(result)
end
end

defp addr?(addr) do
Expand Down
32 changes: 30 additions & 2 deletions lib/get_geocode/apis/nominatim.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
defmodule GetGeocode.Apis.Nominatim do
@moduledoc """
## Nominatim API.
"""
@url "https://nominatim.openstreetmap.org/search?q=<QUERY>&format=json"

@doc """
Gets data from an `addr`ess.
Results in a list with the data, or a tuple `{:ok, "No result"}`.
## Examples
iex> GetGeocode.Apis.Nominatim.get_data "Rua Izaurina Braga"
%{
"boundingbox" => ["-3.1058605", "-3.105157", "-60.0550895", "-60.0542833"],
"class" => "highway",
"display_name" => "Rua Izaurina Braga, Compensa, Manaus, Região Geográfica Imediata de Manaus, Região Geográfica Intermediária de Manaus, Amazonas, Região Norte, 69000-000, Brasil",
"importance" => 0.4,
"lat" => "-3.1054153",
"licence" => "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
"lon" => "-60.0547259",
"osm_id" => 662237608,
"osm_type" => "way",
"place_id" => 233020447,
"type" => "residential"
}
"""
def get_data(addr) do
{:ok, %HTTPoison.Response{body: body}} = HTTPoison.get(gen_query(addr))

Jason.decode!(body)
|> hd()
result = Jason.decode!(body)

case result do
[] -> {:ok, "No result"}
_ -> hd(result)
end
end

defp sanitize(query) do
Expand Down
22 changes: 17 additions & 5 deletions lib/get_geocode/apis/via_cep.ex
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
defmodule GetGeocode.Apis.ViaCep do
@moduledoc """
## ViaCEP API
### https://viacep.com.br/ws/
From https://viacep.com.br/
"""
@url "https://viacep.com.br/ws"

def get_cep(cep) do
cep = sanitize(cep)
{:ok, %HTTPoison.Response{body: body}} = HTTPoison.get("#{@url}/#{cep}/json")
Jason.decode!(body)
def get_cep(cep) when is_binary(cep) do
cep_sanitized = sanitize(cep)

cond do
String.length(cep_sanitized) == 8 -> send_request(cep_sanitized)
true -> {:error, "CEP must be 8 digits long"}
end
end

def get_cep(_) do
{:error, "CEP must be a string with 8 digits"}
end

defp sanitize(cep) do
Expand All @@ -17,4 +24,9 @@ defmodule GetGeocode.Apis.ViaCep do
|> String.replace("-", "")
|> String.replace(" ", "")
end

defp send_request(cep) do
{:ok, %HTTPoison.Response{body: body}} = HTTPoison.get("#{@url}/#{cep}/json")
Jason.decode!(body)
end
end
2 changes: 1 addition & 1 deletion lib/get_geocode/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule GetGeocode.Application do
def start(_type, _args) do
children = [
# Starts a worker by calling: GetGeocode.Worker.start_link(arg)
# {GetGeocode.Worker, arg}
# {GetGeocodeCash, %{}}
]

# See https://hexdocs.pm/elixir/Supervisor.html
Expand Down
10 changes: 10 additions & 0 deletions test/apis/via_cep_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,14 @@ defmodule GetGeocodeApisViaCepTest do
%{"localidade" => cidade} = ViaCep.get_cep("69035350")
assert cidade == "Manaus"
end

test "get_cep/1 with input other than string" do
result = ViaCep.get_cep(1)
assert result == {:error, "CEP must be a string with 8 digits"}
end

test "get_cep/1 with input less than 8 digits" do
result = ViaCep.get_cep("690353")
assert result == {:error, "CEP must be 8 digits long"}
end
end
18 changes: 17 additions & 1 deletion test/getgeocode_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,24 @@ defmodule GetGeocodeTest do
error = get(1)
assert error == {:error, "Invalid input"}
end
test "get/0 returning error message in no input suplied" do

test "get/0 returning error message with no input supplied" do
error = get()
assert error == {:error, "Invalid input"}
end

test "get/1 returning 'No result' return from nominatim" do
error = get("asdasdasd asdasdasd")
assert error == {:ok, "No result"}
end

test "get/1 returning error from viacep with CEP invalid" do
error = get("12345678")
assert error == {:error, "Invalid input"}
end

test "get/1 returning error 'cause the input is neither cep nor the address " do
error = get("asdasdasdasdasdasdasdasdasd")
assert error == {:error, "Invalid input"}
end
end
6 changes: 6 additions & 0 deletions test/struct_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@ defmodule StructTest do
@moduledoc """
## TODO
"""
use ExUnit.Case, async: true

test "init struct Geocode" do
struct_geocode = %GetGeocode.Geocode{}
assert Map.has_key?(struct_geocode, :postalcode)
end
end

0 comments on commit 4f4330f

Please sign in to comment.