Skip to content

Commit

Permalink
Merge pull request #10 from joshuafleck/simpler_api
Browse files Browse the repository at this point in the history
Expose public_url method
  • Loading branch information
Joshua Fleck authored Nov 20, 2016
2 parents c007729 + 3d92c0a commit dc5b13e
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/
```
4 changes: 2 additions & 2 deletions lib/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions lib/ex_ngrok.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 13 additions & 13 deletions lib/settings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand All @@ -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
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
@@ -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]}]},
Expand Down
2 changes: 1 addition & 1 deletion test/ex_ngrok_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit dc5b13e

Please sign in to comment.