Skip to content

Commit 0615b46

Browse files
kennyballouSam Seay
authored andcommitted
Refactor configuration variable lookup (#13)
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.
1 parent 0584037 commit 0615b46

7 files changed

Lines changed: 50 additions & 8 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ By default the public and private keys are loaded via the `RECAPTCHA_PUBLIC_KEY`
4444

4545
```elixir
4646
config :recaptcha,
47-
public_key: System.get_env("RECAPTCHA_PUBLIC_KEY"),
48-
secret: System.get_env("RECAPTCHA_PRIVATE_KEY")
47+
public_key: {:system, "RECAPTCHA_PUBLIC_KEY"},
48+
secret: {:system, "RECAPTCHA_PRIVATE_KEY"}
4949
```
5050

5151
## Usage

config/config.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use Mix.Config
33
config :recaptcha,
44
verify_url: "https://www.google.com/recaptcha/api/siteverify",
55
timeout: 5000,
6-
public_key: System.get_env("RECAPTCHA_PUBLIC_KEY"),
7-
secret: System.get_env("RECAPTCHA_PRIVATE_KEY")
6+
public_key: {:system, "RECAPTCHA_PUBLIC_KEY"},
7+
secret: {:system, "RECAPTCHA_PRIVATE_KEY"}
88

99
import_config "#{Mix.env}.exs"

lib/recaptcha.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ defmodule Recaptcha do
55
See the [documentation](https://developers.google.com/recaptcha/docs/verify)
66
for more details.
77
"""
8+
9+
alias Recaptcha.Config
10+
811
@http_client Application.get_env(:recaptcha, :http_client, Recaptcha.Http)
912

1013
@doc """
@@ -48,7 +51,7 @@ defmodule Recaptcha do
4851

4952
defp request_body(response, options) do
5053
body_options = Keyword.take(options, [:remote_ip, :secret])
51-
application_options = [secret: Application.get_env(:recaptcha, :secret)]
54+
application_options = [secret: Config.get_env(:recaptcha, :secret)]
5255

5356
# override application secret with options secret if it exists
5457
application_options

lib/recaptcha/config.ex

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
defmodule Recaptcha.Config do
2+
@moduledoc """
3+
Provides application/system environment variable lookup at runtime
4+
"""
5+
6+
@doc """
7+
Returns the requested variable
8+
"""
9+
@spec get_env(atom, atom, atom | map) :: term
10+
def get_env(application, key, default \\ nil) do
11+
application
12+
|> Application.get_env(key, default)
13+
|> _get_env()
14+
end
15+
16+
defp _get_env({:system, env_variable}), do: System.get_env(env_variable)
17+
defp _get_env(value), do: value
18+
19+
end

lib/recaptcha/http.ex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ defmodule Recaptcha.Http do
22
@moduledoc """
33
Responsible for managing HTTP requests to the reCAPTCHA API
44
"""
5+
6+
alias Recaptcha.Config
7+
58
@headers [
69
{"Content-type", "application/x-www-form-urlencoded"},
710
{"Accept", "application/json"}
@@ -34,8 +37,8 @@ defmodule Recaptcha.Http do
3437
"""
3538
@spec request_verification(map, [timeout: integer]) :: {:ok, map} | {:error, [atom]}
3639
def request_verification(body, options \\ []) do
37-
timeout = options[:timeout] || Application.get_env(:recaptcha, :timeout, 5000)
38-
url = Application.get_env(:recaptcha, :verify_url, @default_verify_url)
40+
timeout = options[:timeout] || Config.get_env(:recaptcha, :timeout, 5000)
41+
url = Config.get_env(:recaptcha, :verify_url, @default_verify_url)
3942

4043
result =
4144
with {:ok, response} <- HTTPoison.post(url, body, @headers, timeout: timeout),

lib/recaptcha/template.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ defmodule Recaptcha.Template do
88
In future this module may be separated out into a Phoenix specific library.
99
"""
1010
require Elixir.EEx
11+
alias Recaptcha.Config
1112

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

@@ -17,7 +18,7 @@ defmodule Recaptcha.Template do
1718
To convert the string to html code, use Phoenix.HTML.Raw/1 method
1819
"""
1920
def display(options \\ []) do
20-
public_key = options[:public_key] || Application.get_env(:recaptcha, :public_key)
21+
public_key = options[:public_key] || Config.get_env(:recaptcha, :public_key)
2122
render_template(public_key: public_key, options: options)
2223
end
2324
end

test/recaptcha/config_test.exs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
defmodule RecaptchaConfigTest do
2+
use ExUnit.Case, async: true
3+
4+
test "config can read regular config values" do
5+
Application.put_env(:recaptcha, :test_var, "test")
6+
7+
assert Recaptcha.Config.get_env(:recaptcha, :test_var) == "test"
8+
end
9+
10+
test "config can read environment variables" do
11+
System.put_env("TEST_VAR", "test_env_vars")
12+
Application.put_env(:recaptcha, :test_env_var, {:system, "TEST_VAR"})
13+
14+
assert Recaptcha.Config.get_env(:recaptcha, :test_env_var) == "test_env_vars"
15+
end
16+
end

0 commit comments

Comments
 (0)