Skip to content

Commit

Permalink
Merge pull request #9 from joshuafleck/select_consistent_tunnel
Browse files Browse the repository at this point in the history
Select consistent tunnel
  • Loading branch information
Joshua Fleck authored Nov 20, 2016
2 parents 0a4196a + 8eefa42 commit c007729
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
17 changes: 12 additions & 5 deletions lib/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule Ngrok.Api do
with api_url = Application.get_env(:ex_ngrok, :api_url),
{:ok, body} <- get(api_url),
{:ok, parsed} <- parse(body) do
first_tunnel(parsed)
find_tunnel(parsed)
end
end

Expand Down Expand Up @@ -41,14 +41,21 @@ defmodule Ngrok.Api do
end
end

@spec first_tunnel(map) :: error | successful_parse
defp first_tunnel(parsed) do
case List.first(Map.fetch!(parsed, "tunnels")) do
@spec find_tunnel(map) :: error | successful_parse
defp find_tunnel(parsed) do
tunnels = Map.fetch!(parsed, "tunnels")
protocol = Application.get_env(:ex_ngrok, :protocol)
case Enum.find(tunnels, &tunnel_for_protocol(&1, protocol)) do
nil ->
{:error, "No Ngrok tunnels found"}
{:error, "No Ngrok tunnels found for protocol: #{protocol}"}

tunnel ->
{:ok, tunnel}
end
end

@spec tunnel_for_protocol(map, String.t) :: boolean
defp tunnel_for_protocol(tunnel, protocol) do
Map.fetch!(tunnel, "proto") == protocol
end
end
2 changes: 1 addition & 1 deletion 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.0",
version: "0.2.1",
elixir: "~> 1.3",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
Expand Down

0 comments on commit c007729

Please sign in to comment.