From 7250c899374cc08082eb568510a793e462735fe8 Mon Sep 17 00:00:00 2001 From: Constantin Rack Date: Fri, 17 Aug 2018 20:58:53 +0200 Subject: [PATCH] Problem: source is not formatted properly Solution: add .formatter.exs and run 'mix format' --- .formatter.exs | 3 +++ lib/mix/tasks/update_ips.ex | 6 ++---- lib/plug_cloudflare.ex | 15 +++++++-------- mix.exs | 9 ++++----- test/plug_cloudflare_test.exs | 12 +++++------- 5 files changed, 21 insertions(+), 24 deletions(-) create mode 100644 .formatter.exs diff --git a/.formatter.exs b/.formatter.exs new file mode 100644 index 0000000..b11432d --- /dev/null +++ b/.formatter.exs @@ -0,0 +1,3 @@ +[ + inputs: [".formatter.exs", "mix.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/lib/mix/tasks/update_ips.ex b/lib/mix/tasks/update_ips.ex index 4703907..c77df33 100644 --- a/lib/mix/tasks/update_ips.ex +++ b/lib/mix/tasks/update_ips.ex @@ -1,5 +1,4 @@ defmodule Mix.Tasks.Update.Ips do - @moduledoc false use Mix.Task @@ -13,12 +12,11 @@ defmodule Mix.Tasks.Update.Ips do def run(_) do System.cmd("rm", ["ips-v4", "ips-v6"]) - for url <- @urls, do: fetch url + for url <- @urls, do: fetch(url) end defp fetch(url) do - IO.puts "Fetching #{url}" + IO.puts("Fetching #{url}") System.cmd("wget", ["--quiet", url]) end - end diff --git a/lib/plug_cloudflare.ex b/lib/plug_cloudflare.ex index 7b881ef..53df587 100644 --- a/lib/plug_cloudflare.ex +++ b/lib/plug_cloudflare.ex @@ -1,5 +1,4 @@ defmodule Plug.CloudFlare do - @moduledoc """ Parses CloudFlare's CF-Connecting-IP header into Plug.Conn's remote_ip field. """ @@ -11,7 +10,7 @@ defmodule Plug.CloudFlare do @doc "Callback implementation for Plug.call/2" def call(conn, _options) do - if (conn.remote_ip |> is_from_cloudflare) do + if conn.remote_ip |> is_from_cloudflare do conn |> Conn.get_req_header("cf-connecting-ip") |> parse(conn) else conn @@ -19,23 +18,23 @@ defmodule Plug.CloudFlare do end defp parse([], conn), do: conn + defp parse([ip_address], conn) do - case (ip_address |> String.to_char_list |> :inet.parse_address) do + case ip_address |> String.to_charlist() |> :inet.parse_address() do {:ok, remote_ip} -> %Conn{conn | remote_ip: remote_ip} - {:error, _} -> conn + {:error, _} -> conn end end cidrs = [__DIR__, "../ips-v4"] - |> Path.join + |> Path.join() |> File.stream!([], :line) - |> Enum.to_list + |> Enum.to_list() |> Enum.map(&CIDR.parse/1) |> Enum.map(&Macro.escape/1) defp is_from_cloudflare(ip), do: is_from_cloudflare(unquote(cidrs), ip) - defp is_from_cloudflare([h|t], ip), do: CIDR.match!(h, ip) or is_from_cloudflare(t, ip) + defp is_from_cloudflare([h | t], ip), do: CIDR.match!(h, ip) or is_from_cloudflare(t, ip) defp is_from_cloudflare([], _ip), do: false - end diff --git a/mix.exs b/mix.exs index 66b9d0b..1fae870 100644 --- a/mix.exs +++ b/mix.exs @@ -4,11 +4,11 @@ defmodule Plug.Cloudflare.Mixfile do def project do [ app: :plug_cloudflare, - build_embedded: Mix.env == :prod, + build_embedded: Mix.env() == :prod, deps: [ - {:cidr, ">= 1.0.0"}, + {:cidr, ">= 1.0.0"}, {:credo, ">= 0.3.5", only: [:dev, :test]}, - {:plug, ">= 1.1.2"} + {:plug, ">= 1.1.2"} ], description: """ Convert CloudFlare's CF-Connecting-IP header to Plug.Conn's remote_ip field. @@ -29,7 +29,7 @@ defmodule Plug.Cloudflare.Mixfile do "GitHub" => "https://github.com/c-rack/plug_cloudflare" } }, - start_permanent: Mix.env == :prod, + start_permanent: Mix.env() == :prod, version: "1.3.0" ] end @@ -37,5 +37,4 @@ defmodule Plug.Cloudflare.Mixfile do def application do [applications: []] end - end diff --git a/test/plug_cloudflare_test.exs b/test/plug_cloudflare_test.exs index dd06244..22dac85 100644 --- a/test/plug_cloudflare_test.exs +++ b/test/plug_cloudflare_test.exs @@ -1,16 +1,15 @@ defmodule TestRouter do use Plug.Router - plug :match - plug :dispatch - plug Plug.CloudFlare + plug(:match) + plug(:dispatch) + plug(Plug.CloudFlare) get "/" do send_resp(conn, 200, "test") end - match _, do: send_resp(conn, 404, "not found") - + match(_, do: send_resp(conn, 404, "not found")) end defmodule Plug.CloudFlareTest do @@ -66,7 +65,7 @@ defmodule Plug.CloudFlareTest do assert conn.status == 200 assert conn.resp_body == "test" assert conn.remote_ip == {192, 168, 1, 1} - #103.21.244.0 + # 103.21.244.0 end test "should not skip if from CloudFlare IP" do @@ -78,5 +77,4 @@ defmodule Plug.CloudFlareTest do assert conn.resp_body == "test" assert conn.remote_ip == {192, 168, 1, 1} end - end