Files
archived-teslamate/test/support/mocks/srtm.ex
2020-04-11 14:49:44 +02:00

39 lines
938 B
Elixir

defmodule SRTMMock do
use GenServer
defstruct [:pid, :responses]
alias __MODULE__, as: State
# API
def start_link(opts) do
GenServer.start_link(__MODULE__, opts, name: Keyword.fetch!(opts, :name))
end
def get_elevation(name, client, lat, lng) do
GenServer.call(name, {:get_elevation, client, lat, lng})
end
# Callbacks
@impl true
def init(opts) do
{:ok, %State{pid: Keyword.fetch!(opts, :pid), responses: Keyword.fetch!(opts, :responses)}}
end
@impl true
def handle_call({:get_elevation, client, lat, lng}, _, %State{responses: r} = state) do
lat = with %Decimal{} <- lat, do: Decimal.to_float(lat)
lng = with %Decimal{} <- lng, do: Decimal.to_float(lng)
send(state.pid, {SRTM, {:get_elevation, client, lat, lng}})
response =
with {:ok, elevation} <- Map.fetch!(r, {lat, lng}).() do
{:ok, elevation, client}
end
{:reply, response, state}
end
end