Skip to content

Commit

Permalink
Refactor configuration variable lookup (#13)
Browse files Browse the repository at this point in the history
Refactor configuration variable lookup.

Change the `Application.get_env/3` calls to use  `Recaptcha.Config.get_env/3` to perform the lookups. Doing so allows the project to defer environment variable lookup to runtime instead of only at compile-time.
  • Loading branch information
kennyballou authored and Sam Seay committed Oct 8, 2016
1 parent 0584037 commit 0615b46
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ By default the public and private keys are loaded via the `RECAPTCHA_PUBLIC_KEY`

```elixir
config :recaptcha,
public_key: System.get_env("RECAPTCHA_PUBLIC_KEY"),
secret: System.get_env("RECAPTCHA_PRIVATE_KEY")
public_key: {:system, "RECAPTCHA_PUBLIC_KEY"},
secret: {:system, "RECAPTCHA_PRIVATE_KEY"}
```

## Usage
Expand Down
4 changes: 2 additions & 2 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use Mix.Config
config :recaptcha,
verify_url: "https://www.google.com/recaptcha/api/siteverify",
timeout: 5000,
public_key: System.get_env("RECAPTCHA_PUBLIC_KEY"),
secret: System.get_env("RECAPTCHA_PRIVATE_KEY")
public_key: {:system, "RECAPTCHA_PUBLIC_KEY"},
secret: {:system, "RECAPTCHA_PRIVATE_KEY"}

import_config "#{Mix.env}.exs"
5 changes: 4 additions & 1 deletion lib/recaptcha.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ defmodule Recaptcha do
See the [documentation](https://developers.google.com/recaptcha/docs/verify)
for more details.
"""

alias Recaptcha.Config

@http_client Application.get_env(:recaptcha, :http_client, Recaptcha.Http)

@doc """
Expand Down Expand Up @@ -48,7 +51,7 @@ defmodule Recaptcha do

defp request_body(response, options) do
body_options = Keyword.take(options, [:remote_ip, :secret])
application_options = [secret: Application.get_env(:recaptcha, :secret)]
application_options = [secret: Config.get_env(:recaptcha, :secret)]

# override application secret with options secret if it exists
application_options
Expand Down
19 changes: 19 additions & 0 deletions lib/recaptcha/config.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule Recaptcha.Config do
@moduledoc """
Provides application/system environment variable lookup at runtime
"""

@doc """
Returns the requested variable
"""
@spec get_env(atom, atom, atom | map) :: term
def get_env(application, key, default \\ nil) do
application
|> Application.get_env(key, default)
|> _get_env()
end

defp _get_env({:system, env_variable}), do: System.get_env(env_variable)
defp _get_env(value), do: value

end
7 changes: 5 additions & 2 deletions lib/recaptcha/http.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ defmodule Recaptcha.Http do
@moduledoc """
Responsible for managing HTTP requests to the reCAPTCHA API
"""

alias Recaptcha.Config

@headers [
{"Content-type", "application/x-www-form-urlencoded"},
{"Accept", "application/json"}
Expand Down Expand Up @@ -34,8 +37,8 @@ defmodule Recaptcha.Http do
"""
@spec request_verification(map, [timeout: integer]) :: {:ok, map} | {:error, [atom]}
def request_verification(body, options \\ []) do
timeout = options[:timeout] || Application.get_env(:recaptcha, :timeout, 5000)
url = Application.get_env(:recaptcha, :verify_url, @default_verify_url)
timeout = options[:timeout] || Config.get_env(:recaptcha, :timeout, 5000)
url = Config.get_env(:recaptcha, :verify_url, @default_verify_url)

result =
with {:ok, response} <- HTTPoison.post(url, body, @headers, timeout: timeout),
Expand Down
3 changes: 2 additions & 1 deletion lib/recaptcha/template.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule Recaptcha.Template do
In future this module may be separated out into a Phoenix specific library.
"""
require Elixir.EEx
alias Recaptcha.Config

EEx.function_from_file :defp, :render_template, "lib/template.html.eex", [:assigns]

Expand All @@ -17,7 +18,7 @@ defmodule Recaptcha.Template do
To convert the string to html code, use Phoenix.HTML.Raw/1 method
"""
def display(options \\ []) do
public_key = options[:public_key] || Application.get_env(:recaptcha, :public_key)
public_key = options[:public_key] || Config.get_env(:recaptcha, :public_key)
render_template(public_key: public_key, options: options)
end
end
16 changes: 16 additions & 0 deletions test/recaptcha/config_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule RecaptchaConfigTest do
use ExUnit.Case, async: true

test "config can read regular config values" do
Application.put_env(:recaptcha, :test_var, "test")

assert Recaptcha.Config.get_env(:recaptcha, :test_var) == "test"
end

test "config can read environment variables" do
System.put_env("TEST_VAR", "test_env_vars")
Application.put_env(:recaptcha, :test_env_var, {:system, "TEST_VAR"})

assert Recaptcha.Config.get_env(:recaptcha, :test_env_var) == "test_env_vars"
end
end

0 comments on commit 0615b46

Please sign in to comment.