Skip to content

Instantly share code, notes, and snippets.

@leite
Created July 24, 2020 21:22
Show Gist options
  • Save leite/6e41a00b9c38caccd5463c4a0a78f093 to your computer and use it in GitHub Desktop.
Save leite/6e41a00b9c38caccd5463c4a0a78f093 to your computer and use it in GitHub Desktop.
get list of municipalities with neighbourhoods ...
defmodule CEP.Neighbourhoods do
@moduledoc """
Documentation for `CEP.Neighbourhoods`.
"""
alias CEP.{Utils, Neighbourhoods}
defstruct has_neighbourhoods?: false,
neighbourhoods: %{},
timeout: 900,
cache: nil
@neighbourhoods_url "https://sidra.ibge.gov.br/Territorio/Unidades?nivel=102"
@doc """
Get neighbourhoods
"""
def get(
%CEP{all?: all?, neighbourhoods?: neighbourhoods?} = struct
) when all? and not neighbourhoods? do
struct
end
@doc """
Get neighbourhoods
"""
def get(%CEP{
all?: all?,
neighbourhoods?: neighbourhoods?,
cache_exists?: cache_exists?,
cache: cache,
timeout: timeout
} = struct) when all? or neighbourhoods? do
sections = if cache_exists?, do: Utils.list_sections(cache), else: []
%{ neighbourhoods: neighbourhoods } = %Neighbourhoods{
timeout: timeout,
cache: cache,
has_neighbourhoods?: neighbourhoods? and Enum.member?(sections, "neighbourhoods")
}
|> load_neighbourhoods_from_cache
|> get_neighbourhoods
%CEP{ struct | neighbourhoods: neighbourhoods }
end
def get_neighbourhoods(
%Neighbourhoods{has_neighbourhoods?: has_neighbourhoods?, cache: cache, timeout: timeout} = struct
) when not has_neighbourhoods? do
case HTTPoison.get(@neighbourhoods_url, [], timeout: 15_000, recv_timeout: timeout * 1_000) do
{:ok, %{status_code: 200, body: body}} ->
Utils.write_cache(body, cache, :neighbourhoods)
|> Poison.decode
|> parse_neighbourhoods(struct)
{:ok, %{status_code: status}} ->
exit {:error, "something went wrong, http code ##{status}"}
{:error, %{reason: reason}} ->
exit {:error, "something else went really wrong, reason: #{reason}"}
end
end
def load_neighbourhoods_from_cache(
%Neighbourhoods{has_neighbourhoods?: has_neighbourhoods?} = struct
) when not has_neighbourhoods? do
struct
end
def load_neighbourhoods_from_cache(
%Neighbourhoods{has_neighbourhoods?: has_neighbourhoods?, cache: cache} = struct
) when has_neighbourhoods? do
Utils.read_cache(cache, :neighbourhoods) |> parse_neighbourhoods(struct)
end
def parse_neighbourhoods a, b, c, d, e \\ []
def parse_neighbourhoods(
[neighbor | n_tail], [municipality | m_tail], [state | s_tail], struct, n_cache
) do
parse_neighbourhoods(
n_tail,
m_tail,
s_tail,
struct,
[%{key: "#{municipality}_#{state}", municipality: municipality, neighbourhood: neighbor, state: state} | n_cache]
)
end
def parse_neighbourhoods [], [], [], struct, n_cache do
%Neighbourhoods{struct | neighbourhoods: Enum.group_by(n_cache, & &1[:key])}
end
def parse_neighbourhoods(
{:ok, %{"Nivel"=>%{"Id"=>102}, "Nomes"=>neighbourhoods, "SiglasUF"=>states, "Complementos1"=>municipalities}},
struct
) do
parse_neighbourhoods neighbourhoods, municipalities, states, struct
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment