diff --git a/lib/get_geocode.ex b/lib/get_geocode.ex index 5e871f7..38a1532 100644 --- a/lib/get_geocode.ex +++ b/lib/get_geocode.ex @@ -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 diff --git a/lib/get_geocode/apis/nominatim.ex b/lib/get_geocode/apis/nominatim.ex index 77c6303..85a3591 100644 --- a/lib/get_geocode/apis/nominatim.ex +++ b/lib/get_geocode/apis/nominatim.ex @@ -1,11 +1,39 @@ defmodule GetGeocode.Apis.Nominatim do + @moduledoc """ + ## Nominatim API. + """ @url "https://nominatim.openstreetmap.org/search?q=&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 diff --git a/lib/get_geocode/apis/via_cep.ex b/lib/get_geocode/apis/via_cep.ex index ee97e31..102c9a1 100644 --- a/lib/get_geocode/apis/via_cep.ex +++ b/lib/get_geocode/apis/via_cep.ex @@ -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 @@ -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 diff --git a/lib/get_geocode/application.ex b/lib/get_geocode/application.ex index 36dd6c7..3857fdf 100644 --- a/lib/get_geocode/application.ex +++ b/lib/get_geocode/application.ex @@ -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 diff --git a/test/apis/via_cep_test.exs b/test/apis/via_cep_test.exs index 2a0ba93..a08adc3 100644 --- a/test/apis/via_cep_test.exs +++ b/test/apis/via_cep_test.exs @@ -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 diff --git a/test/getgeocode_test.exs b/test/getgeocode_test.exs index 16e94bd..7990892 100644 --- a/test/getgeocode_test.exs +++ b/test/getgeocode_test.exs @@ -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 diff --git a/test/struct_test.exs b/test/struct_test.exs index d07a17e..1957094 100644 --- a/test/struct_test.exs +++ b/test/struct_test.exs @@ -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