Last active
December 14, 2017 23:05
-
-
Save lasseebert/2746a26baeb92f2de011100ebcb76bb1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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