From bd0bf85586223e926be04a2b535fa2ae056aa623 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Thu, 6 Jul 2017 14:03:11 +0300 Subject: [PATCH] Adds more fixes to the static types --- lib/recaptcha.ex | 18 ++++++++++-------- lib/recaptcha/config.ex | 2 +- lib/recaptcha/http.ex | 2 +- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/recaptcha.ex b/lib/recaptcha.ex index 44141c1..78a73d4 100644 --- a/lib/recaptcha.ex +++ b/lib/recaptcha.ex @@ -6,9 +6,9 @@ defmodule Recaptcha do for more details. """ - alias Recaptcha.Config + alias Recaptcha.{Config, Http, Response} - @http_client Application.get_env(:recaptcha, :http_client, Recaptcha.Http) + @http_client Application.get_env(:recaptcha, :http_client, Http) @doc """ Verifies a reCAPTCHA response string. @@ -24,18 +24,20 @@ defmodule Recaptcha do {:ok, api_response} = Recaptcha.verify("response_string") """ - @spec verify(String.t, Keyword.t) :: {:ok, Recaptcha.Response.t} | {:error, [atom]} + @spec verify(String.t, Keyword.t) :: {:ok, Response.t} | {:error, [atom]} def verify(response, options \\ []) do - case @http_client.request_verification( + verification = @http_client.request_verification( request_body(response, options), Keyword.take(options, [:timeout]) - ) do + ) + + case verification do {:error, errors} -> {:error, errors} {:ok, %{"success" => false, "error-codes" => errors}} -> - {:error, Enum.map(errors, fn(error) -> atomise_api_error(error) end)} + {:error, Enum.map(errors, &atomise_api_error/1)} {:ok, %{"success" => true, "challenge_ts" => timestamp, "hostname" => host}} -> - {:ok, %Recaptcha.Response{challenge_ts: timestamp, hostname: host}} + {:ok, %Response{challenge_ts: timestamp, hostname: host}} {:ok, %{"success" => false, "challenge_ts" => _timestamp, "hostname" => _host}} -> {:error, [:challenge_failed]} end @@ -55,6 +57,6 @@ defmodule Recaptcha do defp atomise_api_error(error) do error |> String.replace("-", "_") - |> String.to_atom + |> String.to_existing_atom end end diff --git a/lib/recaptcha/config.ex b/lib/recaptcha/config.ex index 022fe41..6391c44 100644 --- a/lib/recaptcha/config.ex +++ b/lib/recaptcha/config.ex @@ -6,7 +6,7 @@ defmodule Recaptcha.Config do @doc """ Returns the requested variable """ - @spec get_env(atom, atom, atom | map) :: term + @spec get_env(atom, atom, any) :: any def get_env(application, key, default \\ nil) do application |> Application.get_env(key, default) diff --git a/lib/recaptcha/http.ex b/lib/recaptcha/http.ex index a9aaf0f..c20eb6b 100644 --- a/lib/recaptcha/http.ex +++ b/lib/recaptcha/http.ex @@ -35,7 +35,7 @@ defmodule Recaptcha.Http do remote_ip: "remote_ip" }) """ - @spec request_verification(map, [timeout: integer]) :: {:ok, map} | {:error, [atom]} + @spec request_verification(binary, [timeout: integer]) :: {:ok, map} | {:error, [atom]} def request_verification(body, options \\ []) do timeout = options[:timeout] || Config.get_env(:recaptcha, :timeout, 5000) url = Config.get_env(:recaptcha, :verify_url, @default_verify_url)