From a3a1fa993d09f23619a6605bce7d3e0824ba096c Mon Sep 17 00:00:00 2001 From: Samuel Seay Date: Mon, 5 Sep 2016 20:20:54 +1200 Subject: Add strict credo checks to build, fix line length issues. --- .credo.exs | 59 ++++++++++++++++++++++++++++++++++ .travis.yml | 2 +- lib/recaptcha.ex | 27 +++++++++++----- lib/recaptcha/http.ex | 22 ++++++++++--- lib/recaptcha/http/mock_http_client.ex | 3 +- lib/recaptcha/template.ex | 4 +-- 6 files changed, 101 insertions(+), 16 deletions(-) create mode 100644 .credo.exs diff --git a/.credo.exs b/.credo.exs new file mode 100644 index 0000000..a1c1ef5 --- /dev/null +++ b/.credo.exs @@ -0,0 +1,59 @@ +%{ + configs: [ + %{ + name: "default", + files: %{ + included: ["lib/", "src/", "web/", "apps/"], + excluded: [~r"/_build/", ~r"/deps/"] + }, + requires: [], + check_for_updates: true, + checks: [ + {Credo.Check.Consistency.ExceptionNames}, + {Credo.Check.Consistency.LineEndings}, + {Credo.Check.Consistency.SpaceAroundOperators}, + {Credo.Check.Consistency.SpaceInParentheses}, + {Credo.Check.Consistency.TabsOrSpaces}, + {Credo.Check.Design.AliasUsage, priority: :low}, + {Credo.Check.Design.DuplicatedCode, excluded_macros: []}, + {Credo.Check.Design.TagTODO, exit_status: 2}, + {Credo.Check.Design.TagFIXME}, + {Credo.Check.Readability.FunctionNames}, + {Credo.Check.Readability.LargeNumbers}, + {Credo.Check.Readability.MaxLineLength, priority: :low, max_length: 90}, + {Credo.Check.Readability.ModuleAttributeNames}, + {Credo.Check.Readability.ModuleDoc}, + {Credo.Check.Readability.ModuleNames}, + {Credo.Check.Readability.ParenthesesInCondition}, + {Credo.Check.Readability.PredicateFunctionNames}, + {Credo.Check.Readability.TrailingBlankLine}, + {Credo.Check.Readability.TrailingWhiteSpace}, + {Credo.Check.Readability.VariableNames}, + {Credo.Check.Refactor.ABCSize}, + {Credo.Check.Refactor.CondStatements}, + {Credo.Check.Refactor.FunctionArity}, + {Credo.Check.Refactor.MatchInCondition}, + {Credo.Check.Refactor.PipeChainStart}, + {Credo.Check.Refactor.CyclomaticComplexity}, + {Credo.Check.Refactor.NegatedConditionsInUnless}, + {Credo.Check.Refactor.NegatedConditionsWithElse}, + {Credo.Check.Refactor.Nesting}, + {Credo.Check.Refactor.UnlessWithElse}, + {Credo.Check.Warning.IExPry}, + {Credo.Check.Warning.IoInspect}, + {Credo.Check.Warning.NameRedeclarationByAssignment}, + {Credo.Check.Warning.NameRedeclarationByCase}, + {Credo.Check.Warning.NameRedeclarationByDef}, + {Credo.Check.Warning.NameRedeclarationByFn}, + {Credo.Check.Warning.OperationOnSameValues}, + {Credo.Check.Warning.BoolOperationOnSameValues}, + {Credo.Check.Warning.UnusedEnumOperation}, + {Credo.Check.Warning.UnusedKeywordOperation}, + {Credo.Check.Warning.UnusedListOperation}, + {Credo.Check.Warning.UnusedStringOperation}, + {Credo.Check.Warning.UnusedTupleOperation}, + {Credo.Check.Warning.OperationWithConstantResult} + ] + } + ] +} diff --git a/.travis.yml b/.travis.yml index a125047..7d38ef9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,4 +2,4 @@ language: elixir elixir: - 1.3.2 -script: mix test +script: mix test && mix credo --strict diff --git a/lib/recaptcha.ex b/lib/recaptcha.ex index 0037480..ed7e9b7 100644 --- a/lib/recaptcha.ex +++ b/lib/recaptcha.ex @@ -2,7 +2,8 @@ defmodule Recaptcha do @moduledoc """ A module for verifying reCAPTCHA version 2.0 response strings. - See the [documentation](https://developers.google.com/recaptcha/docs/verify) for more details. + See the [documentation](https://developers.google.com/recaptcha/docs/verify) + for more details. """ @secret Application.get_env(:recaptcha, :secret) @http_client Application.get_env(:recaptcha, :http_client, Recaptcha.Http) @@ -13,14 +14,17 @@ defmodule Recaptcha do ## Options * `:timeout` - the timeout for the request (defaults to 5000ms) - * `:secret` - the secret key used by recaptcha (defaults to the secret provided in application config) + * `:secret` - the secret key used by recaptcha (defaults to the secret + provided in application config) * `:remote_ip` - the IP address of the user (optional and not set by default) ## Example {:ok, api_response} = Recaptcha.verify("response_string") """ - @spec verify(String.t, [timeout: integer, secret: String.t, remote_ip: String.t]) :: {:ok, Recaptcha.Response.t} | {:error, [atom]} + @spec verify(String.t, [timeout: integer, secret: String.t, + remote_ip: String.t]) :: {:ok, Recaptcha.Response.t} | + {:error, [atom]} def verify(response, options \\ []) def verify(nil = _response, _) do @@ -28,10 +32,16 @@ defmodule Recaptcha do end def verify(response, options) do - case @http_client.request_verification(request_body(response, options), Keyword.take(options, [:timeout])) do - {:error, errors} -> {:error, errors} - {:ok, %{"success" => false, "error-codes" => errors}} -> {:error, Enum.map(errors, fn(error) -> atomise_api_error(error) end) } - {:ok, %{"success" => true, "challenge_ts" => timestamp, "hostname" => host}} -> {:ok, %Recaptcha.Response{challenge_ts: timestamp, hostname: host}} + case @http_client.request_verification( + request_body(response, options), + Keyword.take(options, [:timeout]) + ) do + {:error, errors} -> + {:error, errors} + {:ok, %{"success" => false, "error-codes" => errors}} -> + {:error, Enum.map(errors, fn(error) -> atomise_api_error(error) end)} + {:ok, %{"success" => true, "challenge_ts" => timestamp, "hostname" => host}} -> + {:ok, %Recaptcha.Response{challenge_ts: timestamp, hostname: host}} end end @@ -47,7 +57,8 @@ defmodule Recaptcha do end defp atomise_api_error(error) do - String.replace(error, "-", "_") + error + |> String.replace("-", "_") |> String.to_atom end end diff --git a/lib/recaptcha/http.ex b/lib/recaptcha/http.ex index 01c088a..0182a7c 100644 --- a/lib/recaptcha/http.ex +++ b/lib/recaptcha/http.ex @@ -2,14 +2,18 @@ defmodule Recaptcha.Http do @moduledoc """ Responsible for managing HTTP requests to the reCAPTCHA API """ - @headers [{"Content-type", "application/x-www-form-urlencoded"}, {"Accept", "application/json"}] + @headers [ + {"Content-type", "application/x-www-form-urlencoded"}, + {"Accept", "application/json"} + ] @url Application.get_env(:recaptcha, :verify_url) @timeout Application.get_env(:recaptcha, :timeout, 5000) @doc """ Sends an HTTP request to the reCAPTCHA version 2.0 API. - See the [documentation](https://developers.google.com/recaptcha/docs/verify#api-response) for more details on the API response. + See the [docs](https://developers.google.com/recaptcha/docs/verify#api-response) + for more details on the API response. ## Options @@ -17,12 +21,22 @@ defmodule Recaptcha.Http do ## Example - {:ok, %{ "success" => success, "challenge_ts" => ts, "hostname" => host, "error-codes" => errors}} = Recaptcha.Http.request_verification(%{ secret: "secret", response: "response", remote_ip: "remote_ip"}) + {:ok, %{ + "success" => success, + "challenge_ts" => ts, + "hostname" => host, + "error-codes" => errors + }} = Recaptcha.Http.request_verification(%{ + secret: "secret", + response: "response", + remote_ip: "remote_ip" + }) """ @spec request_verification(map, [timeout: integer]) :: {:ok, map} | {:error, [atom]} def request_verification(body, options \\ []) do + timeout = options[:timeout] || @timeout result = - with {:ok, response} <- HTTPoison.post(@url, body, @headers, timeout: options[:timeout] || @timeout), + with {:ok, response} <- HTTPoison.post(@url, body, @headers, timeout: timeout), {:ok, data} <- Poison.decode(response.body) do {:ok, data} end diff --git a/lib/recaptcha/http/mock_http_client.ex b/lib/recaptcha/http/mock_http_client.ex index 2355dd3..492376a 100644 --- a/lib/recaptcha/http/mock_http_client.ex +++ b/lib/recaptcha/http/mock_http_client.ex @@ -2,6 +2,7 @@ defmodule Recaptcha.Http.MockClient do @moduledoc """ A mock HTTP client used for testing. """ + alias Recaptcha.Http def request_verification(body, options \\ []) @@ -13,6 +14,6 @@ defmodule Recaptcha.Http.MockClient do # every other match is a pass through to the real client def request_verification(body, options) do send self(), {:request_verification, body, options} - Recaptcha.Http.request_verification(body, options) + Http.request_verification(body, options) end end diff --git a/lib/recaptcha/template.ex b/lib/recaptcha/template.ex index d644266..28db29c 100644 --- a/lib/recaptcha/template.ex +++ b/lib/recaptcha/template.ex @@ -2,8 +2,8 @@ defmodule Recaptcha.Template do @moduledoc """ Responsible for rendering boilerplate recaptcha HTML code, supports noscript fallback. - Currently the [explicit render](https://developers.google.com/recaptcha/docs/display#explicit_render) functionality - is not supported. + [Some](https://developers.google.com/recaptcha/docs/display#explicit_render) + functionality is not currently supported. In future this module may be separated out into a Phoenix specific library. """ -- cgit v1.2.1