diff --git a/README.md b/README.md index 01512a3..ea55788 100644 --- a/README.md +++ b/README.md @@ -63,5 +63,5 @@ The URL will change every time Ngrok starts, but you can retrieve the URL by running the following: ```elixir -Ngrok.Settings.get("public_url") # => http://(.*).ngrok.io/ +Ngrok.public_url # => http://(.*).ngrok.io/ ``` diff --git a/lib/api.ex b/lib/api.ex index e3d88e3..9a2581d 100644 --- a/lib/api.ex +++ b/lib/api.ex @@ -7,8 +7,8 @@ defmodule Ngrok.Api do @type successful_parse :: {:ok, map} @type successful_get :: {:ok, String.t} - @spec first_tunnel_settings() :: error | successful_parse - def first_tunnel_settings do + @spec tunnel_settings() :: error | successful_parse + def tunnel_settings do with api_url = Application.get_env(:ex_ngrok, :api_url), {:ok, body} <- get(api_url), {:ok, parsed} <- parse(body) do diff --git a/lib/ex_ngrok.ex b/lib/ex_ngrok.ex index 057cd11..bbb63a9 100644 --- a/lib/ex_ngrok.ex +++ b/lib/ex_ngrok.ex @@ -17,4 +17,16 @@ defmodule Ngrok do opts = [strategy: :rest_for_one, name: Ngrok.Supervisor] Supervisor.start_link(children, opts) end + + @doc """ + Retrieves the public URL of the Ngrok tunnel + + ## Example + + Ngrok.public_url # => http://(.*).ngrok.io/ + """ + @spec public_url :: String.t + def public_url do + Ngrok.Settings.get("public_url") + end end diff --git a/lib/settings.ex b/lib/settings.ex index eb848ab..4f15f6f 100644 --- a/lib/settings.ex +++ b/lib/settings.ex @@ -6,7 +6,7 @@ defmodule Ngrok.Settings do require Logger def start_link do - Agent.start_link(fn -> announce_settings end, name: __MODULE__) + Agent.start_link(fn -> fetch_and_announce_settings end, name: __MODULE__) end @doc """ @@ -25,28 +25,28 @@ defmodule Ngrok.Settings do Agent.get(__MODULE__, &Map.get(&1, field_name)) end - @spec announce_settings :: map - defp announce_settings do - settings = first_tunnel_settings - announce(settings) - settings + @spec fetch_and_announce_settings :: map + defp fetch_and_announce_settings do + tunnel_settings + |> announce end - @spec first_tunnel_settings :: map - defp first_tunnel_settings(), do: first_tunnel_settings(0, "") - defp first_tunnel_settings(6, error_message), do: raise "Unable to retrieve setting from Ngrok: #{error_message}" - defp first_tunnel_settings(total_attempts, _) do + @spec tunnel_settings :: map + defp tunnel_settings(), do: tunnel_settings(0, "") + defp tunnel_settings(6, error_message), do: raise "Unable to retrieve setting from Ngrok: #{error_message}" + defp tunnel_settings(total_attempts, _) do :timer.sleep(total_attempts * 100) - case Ngrok.Api.first_tunnel_settings do + case Ngrok.Api.tunnel_settings do {:ok, settings} -> settings {:error, message} -> - first_tunnel_settings(total_attempts + 1, message) + tunnel_settings(total_attempts + 1, message) end end - @spec announce(map) :: :ok + @spec announce(map) :: map defp announce(settings) do Logger.info "ex_ngrok: Ngrok tunnel available at #{settings["public_url"]}" + settings end end diff --git a/mix.exs b/mix.exs index f941f26..e5b6bb0 100644 --- a/mix.exs +++ b/mix.exs @@ -3,7 +3,7 @@ defmodule Ngrok.Mixfile do def project do [app: :ex_ngrok, - version: "0.2.1", + version: "0.3.0", elixir: "~> 1.3", build_embedded: Mix.env == :prod, start_permanent: Mix.env == :prod, @@ -30,7 +30,7 @@ defmodule Ngrok.Mixfile do {:ex_doc, "~> 0.14", only: :dev}, {:credo, "~> 0.5", only: [:dev, :test]}, {:dialyxir, "~> 0.4", only: [:dev]}, - {:httpoison, "~> 0.9"}, + {:httpoison, "~> 0.10"}, {:poison, "~> 2.0"} ] end diff --git a/mix.lock b/mix.lock index 9167079..c5bced4 100644 --- a/mix.lock +++ b/mix.lock @@ -1,6 +1,6 @@ %{"bunt": {:hex, :bunt, "0.1.6", "5d95a6882f73f3b9969fdfd1953798046664e6f77ec4e486e6fafc7caad97c6f", [:mix], []}, "certifi": {:hex, :certifi, "0.7.0", "861a57f3808f7eb0c2d1802afeaae0fa5de813b0df0979153cbafcd853ababaf", [:rebar3], []}, - "credo": {:hex, :credo, "0.5.2", "92e8c9f86e0ffbf9f688595e9f4e936bc96a52e5606d2c19713e9e4d191d5c74", [:mix], [{:bunt, "~> 0.1.6", [hex: :bunt, optional: false]}]}, + "credo": {:hex, :credo, "0.5.3", "0c405b36e7651245a8ed63c09e2d52c2e2b89b6d02b1570c4d611e0fcbecf4a2", [:mix], [{:bunt, "~> 0.1.6", [hex: :bunt, optional: false]}]}, "dialyxir": {:hex, :dialyxir, "0.4.0", "53ac3014bb4aef647728a697052b4db3a84c6742de7aab0e0a1c863ea274007b", [:mix], []}, "earmark": {:hex, :earmark, "1.0.3", "89bdbaf2aca8bbb5c97d8b3b55c5dd0cff517ecc78d417e87f1d0982e514557b", [:mix], []}, "ex_doc": {:hex, :ex_doc, "0.14.3", "e61cec6cf9731d7d23d254266ab06ac1decbb7651c3d1568402ec535d387b6f7", [:mix], [{:earmark, "~> 1.0", [hex: :earmark, optional: false]}]}, diff --git a/test/ex_ngrok_test.exs b/test/ex_ngrok_test.exs index 2d3412c..9f693a8 100644 --- a/test/ex_ngrok_test.exs +++ b/test/ex_ngrok_test.exs @@ -13,7 +13,7 @@ defmodule NgrokTest do test "it stores the settings" do :ok = Application.start(:ex_ngrok) - assert Ngrok.Settings.get("public_url") =~ ~r/http(s)?:\/\/(.*)\.ngrok\.io/ + assert Ngrok.public_url =~ ~r/http(s)?:\/\/(.*)\.ngrok\.io/ end @custom_configuration api_url: "http://localhost:0"