Skip to content

Commit

Permalink
Adds dialyzer support
Browse files Browse the repository at this point in the history
This commit adds static analysis tool `dialyzer` to the
list of dependencies. It is required to check, that every
parameter in function calls are in place.

This commit also fixes all found static-typing issues.

This commit also adds `CONCTRIBUTING.md` with the development
instructions. Refs #16
  • Loading branch information
sobolevn committed Jul 6, 2017
1 parent 8526ca7 commit 49a55a7
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 20 deletions.
31 changes: 31 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Contributing

## Pull Requests Welcome

1. Fork the project
2. Create a topic branch
3. Make logically-grouped commits with clear commit messages
4. Push commits to your fork
5. Open a pull request against `recaptcha/master`

## Issues

If you believe there to be a bug, please provide the maintainers with enough
detail to reproduce or a link to an app exhibiting unexpected behavior. For
help, please start with Stack Overflow.

## Development and testing

To set up the development environment you would have to follow several steps:

0. setup `elixir`, we are using the latest release, but supporting everything from `1.2`
1. clone the repo
2. `mix deps.get && mix compile`

To test if everything is working:

1. `mix test` will run unit-tests
2. `mix credo --strict` will run linting
3. `mix dialyzer` will run static analyzer tool to check that everything is fine with your code. It may take some time at the first run

If everything is fine - feel free to submit your code!
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ if you have any problems with migrating.
```elixir
defp deps do
[
{:recaptcha, "~> 2.0"},
{:recaptcha, "~> 2.0"},
]
end
```
Expand Down Expand Up @@ -76,7 +76,6 @@ Recaptcha provides the `verify/2` method. Below is an example using a Phoenix co

```elixir
def create(conn, params) do
# some code
case Recaptcha.verify(params["g-recaptcha-response"]) do
{:ok, response} -> do_something
{:error, errors} -> handle_error
Expand All @@ -100,10 +99,7 @@ Option | Action

## Contributing

* Fork the project.
* Make your feature addition or bug fix. (If you're not sure raise an issue to discuss first)
* Commit
* Send me a pull request
Check out [CONTRIBUTING.md](/CONTRIBUTING.md) if you want to help.

## License

Expand Down
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
8 changes: 5 additions & 3 deletions lib/recaptcha/template.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ defmodule Recaptcha.Template do
require Elixir.EEx
alias Recaptcha.Config

EEx.function_from_file :defp, :render_template, "lib/template.html.eex", [:assigns]
EEx.function_from_file :defp, :render_template,
"lib/template.html.eex", [:assigns]

@doc """
Returns a string with reCAPTCHA code
To convert the string to html code, use Phoenix.HTML.Raw/1 method
"""
def display(options \\ []) do
public_key = options[:public_key] || Config.get_env(:recaptcha, :public_key)
render_template(public_key: public_key, options: options)
public_key = options[:public_key] || Config.get_env(
:recaptcha, :public_key)
render_template(%{public_key: public_key, options: options})
end
end
10 changes: 9 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ defmodule Recaptcha.Mixfile do
"coveralls.post": :test,
"coveralls.html": :test,
],

# Dialyzer:
dialyzer: [
plt_add_deps: :apps_direct,
plt_add_apps: [:poison]
]
]
end

def application do
[applications: [:logger, :httpoison]]
[applications: [:logger, :httpoison, :eex]]
end

defp description do
Expand All @@ -39,8 +45,10 @@ defmodule Recaptcha.Mixfile do
[
{:httpoison, "~> 0.11.0"},
{:poison, "~> 1.5 or ~> 2.0 or ~> 3.0"},

{:credo, "~> 0.6", only: [:dev, :test]},
{:ex_doc, ">= 0.0.0", only: :dev},
{:dialyxir, "~> 0.5", only: [:dev]},
{:excoveralls, "~> 0.6", only: :test},
]
end
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
%{"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm"},
"certifi": {:hex, :certifi, "1.2.1", "c3904f192bd5284e5b13f20db3ceac9626e14eeacfbb492e19583cf0e37b22be", [:rebar3], [], "hexpm"},
"credo": {:hex, :credo, "0.6.0", "44a82f82b94eeb4ba6092c89b8a6730ca1a3291c7940739d5acc8806d25ac991", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}], "hexpm"},
"dialyxir": {:hex, :dialyxir, "0.5.0", "5bc543f9c28ecd51b99cc1a685a3c2a1a93216990347f259406a910cf048d1d7", [:mix], [], "hexpm"},
"earmark": {:hex, :earmark, "1.0.1", "2c2cd903bfdc3de3f189bd9a8d4569a075b88a8981ded9a0d95672f6e2b63141", [:mix], [], "hexpm"},
"ex_doc": {:hex, :ex_doc, "0.13.0", "aa2f8fe4c6136a2f7cfc0a7e06805f82530e91df00e2bff4b4362002b43ada65", [:mix], [{:earmark, "~> 1.0", [hex: :earmark, repo: "hexpm", optional: false]}], "hexpm"},
"excoveralls": {:hex, :excoveralls, "0.6.5", "2d3faf8fe40e77c980620f1344f6d3df2c43b18ab9c21a2c1781a898cc710184", [:mix], [{:exjsx, ">= 3.0.0", [hex: :exjsx, repo: "hexpm", optional: false]}, {:hackney, ">= 0.12.0", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"},
Expand Down

0 comments on commit 49a55a7

Please sign in to comment.