Created
May 17, 2020 10:56
-
-
Save KamilLelonek/77fcdfeebd8f1b68f1e0cab570c3bd89 to your computer and use it in GitHub Desktop.
This file contains 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 Download do | |
use Pipe | |
def start(api_key) do | |
api_key | |
~> download() | |
~> parse() | |
end | |
defp download(123), do: {:ok, "valid result"} | |
defp download(_), do: {:error, "invalid API key"} | |
defp parse({:ok, message}), do: %{message: message, downloaded_at: Time.utc_now()} | |
end | |
Download.start(123) | |
#=> %{downloaded_at: ~T[10:53:31.643047], message: "valid result"} | |
Download.start("invalid") |> IO.inspect() | |
#=> {:error, "invalid API key"} |
This file contains 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 Pipe do | |
defmacro __using__(_) do | |
quote do | |
require unquote(__MODULE__) | |
import unquote(__MODULE__) | |
end | |
end | |
defmacro left ~> right do | |
quote do | |
case unquote(left) do | |
{:error, error} -> | |
{:error, error} | |
result -> | |
result |> unquote(right) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment