Skip to content

Commit

Permalink
Adds more fixes to the static types
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn committed Jul 6, 2017
1 parent 623544e commit bd0bf85
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
18 changes: 10 additions & 8 deletions lib/recaptcha.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion lib/recaptcha/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/recaptcha/http.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit bd0bf85

Please sign in to comment.