Packages

Trackr for every movement you make.

Current section

Files

Jump to
trackr lib trackr core server server.ex
Raw

lib/trackr/core/server/server.ex

defmodule Trackr.Core.Server do
use GenServer
require Logger
def init(destinations) do
{:ok, destinations}
end
def handle_cast({:track, info}, state) do
{action, metadata, headers } = info
track(action, metadata, headers, state)
{:noreply, state}
end
def handle_info(_msg, state) do
{:noreply, state}
end
defp track(action, metadata, headers, destinations) do
body = %{action: action, metadata: metadata}
fire(destinations, body, headers)
end
defp fire(destinations, payload, headers) do
destinations
|> get_destinations()
|> Stream.map(fn destination -> destination end)
|> Enum.map(fn destination ->
Task.async(fn ->
url = Map.get(destination, :url)
method = Map.get(destination, :method)
headers = Map.get(destination, :headers) ++ headers
apply(Trackr.Services.HTTP, String.to_atom(method), [url,payload, headers])
end)
end)
end
defp get_destinations(destinations) do
destinations
|> Enum.flat_map(fn destination -> destination end)
end
end