Skip to content

Instantly share code, notes, and snippets.

@lasseebert
Last active December 14, 2017 23:05
Show Gist options
  • Save lasseebert/2746a26baeb92f2de011100ebcb76bb1 to your computer and use it in GitHub Desktop.
Save lasseebert/2746a26baeb92f2de011100ebcb76bb1 to your computer and use it in GitHub Desktop.
defmodule Legolas.Geolocation do
@moduledoc """
Public interface for all geolocation related functionality
"""
alias Legolas.Geolocation.Cache
alias Legolas.Geolocation.CityLocation
alias Legolas.Geolocation.GoogleMaps.Client
@type address :: {zip :: String.t, city :: String.t, country :: String.t}
@type geolocation :: {latitude :: number, longitude :: number}
def child_spec([]) do
%{
id: __MODULE__,
start: {Cache, :start_link, []}
}
end
@doc """
Finds the geolocation for the given city address
"""
@spec lookup(address) :: {:ok, geolocation} | {:error, any}
def lookup(address) do
case Cache.fetch(address, fn -> Client.lookup(address) end) do
{:ok, %CityLocation{} = location} ->
{:ok, {location.latitude, location.longitude}}
{:error, reason} ->
{:error, reason}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment