aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKenny Ballou <kennyballou@users.noreply.github.com>2016-10-07 19:59:58 -0600
committerSam Seay <sam@manuka.co>2016-10-08 14:59:58 +1300
commit0615b46466cece8ce2160cc5093365f6dfe2081e (patch)
tree02d14e88b8e4ab35e1998576ac896d53154a62ab
parent0584037b09355e8e574227dfa74aedb139d6a1a2 (diff)
downloadrecaptcha-0615b46466cece8ce2160cc5093365f6dfe2081e.tar.gz
recaptcha-0615b46466cece8ce2160cc5093365f6dfe2081e.tar.xz
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.
-rw-r--r--README.md4
-rw-r--r--config/config.exs4
-rw-r--r--lib/recaptcha.ex5
-rw-r--r--lib/recaptcha/config.ex19
-rw-r--r--lib/recaptcha/http.ex7
-rw-r--r--lib/recaptcha/template.ex3
-rw-r--r--test/recaptcha/config_test.exs16
7 files changed, 50 insertions, 8 deletions
diff --git a/README.md b/README.md
index dce5a7e..8605fa4 100644
--- a/README.md
+++ b/README.md
@@ -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
diff --git a/config/config.exs b/config/config.exs
index 3c34501..1b4e60c 100644
--- a/config/config.exs
+++ b/config/config.exs
@@ -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"
diff --git a/lib/recaptcha.ex b/lib/recaptcha.ex
index 3108add..0757785 100644
--- a/lib/recaptcha.ex
+++ b/lib/recaptcha.ex
@@ -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 """
@@ -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
diff --git a/lib/recaptcha/config.ex b/lib/recaptcha/config.ex
new file mode 100644
index 0000000..022fe41
--- /dev/null
+++ b/lib/recaptcha/config.ex
@@ -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
diff --git a/lib/recaptcha/http.ex b/lib/recaptcha/http.ex
index b21f8f0..a9aaf0f 100644
--- a/lib/recaptcha/http.ex
+++ b/lib/recaptcha/http.ex
@@ -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"}
@@ -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),
diff --git a/lib/recaptcha/template.ex b/lib/recaptcha/template.ex
index 008a63d..d3970d5 100644
--- a/lib/recaptcha/template.ex
+++ b/lib/recaptcha/template.ex
@@ -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]
@@ -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
diff --git a/test/recaptcha/config_test.exs b/test/recaptcha/config_test.exs
new file mode 100644
index 0000000..93120c8
--- /dev/null
+++ b/test/recaptcha/config_test.exs
@@ -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