From 0615b46466cece8ce2160cc5093365f6dfe2081e Mon Sep 17 00:00:00 2001 From: Kenny Ballou Date: Fri, 7 Oct 2016 19:59:58 -0600 Subject: 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. --- README.md | 4 ++-- config/config.exs | 4 ++-- lib/recaptcha.ex | 5 ++++- lib/recaptcha/config.ex | 19 +++++++++++++++++++ lib/recaptcha/http.ex | 7 +++++-- lib/recaptcha/template.ex | 3 ++- test/recaptcha/config_test.exs | 16 ++++++++++++++++ 7 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 lib/recaptcha/config.ex create mode 100644 test/recaptcha/config_test.exs 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 -- cgit v1.2.1