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
Functions
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"}
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}}
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