Population v0.1.0 Population.API

This module defines functions for fetching, handling and replying the data from the population.io REST API.

Summary

Functions

Fetches the data from the population.io API given a URL path and handles the result

Handles the reply back to the GenServer that invoked this function, given a response of the format {:ok, response} | {:error, detail}, and the current server state

Handles explicitely the reply back to the GenServer that invoked this function, given a response of the format {:ok, response} | {:error, detail}

Types

failure()
failure() :: {:error, String.t}
response()
response() :: map | [map]
state()
state() :: map | [String.t | map]
success()
success() :: {:ok, response}

Functions

fetch_data(path)
fetch_data(String.t) :: success | failure

Fetches the data from the population.io API given a URL path and handles the result.

Returns {:ok, response} if the call succeed, otherwise {:error, reason}.

Examples

iex> Population.API.fetch_data("countries")
{:ok,
 %{"countries" => ["Afghanistan", "Albania", "Algeria", ...]}}
iex> Population.API.fetch_data("population/Westeros/today-and-tomorrow/")
{:error,
 "Westeros is an invalid value for the parameter "country", the list of valid values can be retrieved from the endpoint /countries"}
handle_reply(expr, state)
handle_reply(success | failure, state) :: {:reply, success | failure, state}

Handles the reply back to the GenServer that invoked this function, given a response of the format {:ok, response} | {:error, detail}, and the current server state.

Examples

iex> Population.API.handle_reply({:ok, %{"foo" => :bar, "bar" => :foo}}, %{})
{:reply, {:ok, %{bar: :foo, foo: :bar}}, %{bar: :foo, foo: :bar}}
iex> Population.API.handle_reply({:error, "reason"}, %{bar: :foo, foo: :bar})
{:reply, {:error, "reason"}, %{bar: :foo, foo: :bar}}
handle_reply!(expr)
handle_reply!(success | failure) ::
  {:reply, response, state} |
  no_return

Handles explicitely the reply back to the GenServer that invoked this function, given a response of the format {:ok, response} | {:error, detail}.

Examples

iex> Population.API.handle_reply!({:ok, %{"foo" => :bar, "bar" => :foo}})
{:reply, %{bar: :foo, foo: :bar}, %{bar: :foo, foo: :bar}}
iex> Population.API.handle_reply!({:error, "reason"})
** (RuntimeError) reason