aboutsummaryrefslogtreecommitdiff
path: root/lib
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 /lib
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.
Diffstat (limited to 'lib')
-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
4 files changed, 30 insertions, 4 deletions
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