From ed525ccc74ee4c706fd2a81bb14730726955d1b9 Mon Sep 17 00:00:00 2001 From: Stephen Moloney Date: Mon, 26 Feb 2018 10:00:28 +0000 Subject: [PATCH] mix format --- .travis.yml | 2 +- lib/fastimage.ex | 63 ++++++++++++++++++++++------------------- lib/fastimage/stream.ex | 22 +++++++------- mix.exs | 16 +++++++++-- 4 files changed, 58 insertions(+), 45 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0cfe5e8..16b8167 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,6 +27,6 @@ script: mix format mix.exs lib/**/*.{ex,exs} --check-formatted && \ mix format mix.exs test/**/*.{ex,exs} --check-formatted fi; -# - mix credo --strict --ignore maxlinelength,cyclomaticcomplexity,todo + - mix credo --strict --ignore maxlinelength,cyclomaticcomplexity,todo - mix test - mix coveralls diff --git a/lib/fastimage.ex b/lib/fastimage.ex index 1ebf9f2..24ed0d3 100644 --- a/lib/fastimage.ex +++ b/lib/fastimage.ex @@ -12,11 +12,11 @@ defmodule Fastimage do dimensions: %Dimensions{} @type t :: %Fastimage{ - source: binary() | nil, - source_type: source_type() | nil, - image_type: image_type() | nil, - dimensions: Dimensions.t() - } + source: binary() | nil, + source_type: source_type() | nil, + image_type: image_type() | nil, + dimensions: Dimensions.t() + } @doc ~S""" Returns the type of image. Accepts a source as a url, binary @@ -55,8 +55,11 @@ defmodule Fastimage do case Utils.get_source_type(source) do :other -> {:error, %Error{reason: :invalid_input}} - source_type -> - {:ok, %Stream.Acc{image_type: type, stream_ref: stream_ref}} = get_acc_with_type(source, source_type, opts) + + source_type -> + {:ok, %Stream.Acc{image_type: type, stream_ref: stream_ref}} = + get_acc_with_type(source, source_type, opts) + Utils.close_stream(stream_ref) {:ok, type} end @@ -125,14 +128,15 @@ defmodule Fastimage do """ @spec info(binary()) :: {:ok, Fastimage.t()} | {:error, Error.t()} - def info(source, opts \\ []) when is_binary(source) do - case Utils.get_source_type(source) do - :other -> - {:error, %Error{reason: :invalid_input}} - source_type -> - info(source, source_type, opts) - end + def info(source, opts \\ []) when is_binary(source) do + case Utils.get_source_type(source) do + :other -> + {:error, %Error{reason: :invalid_input}} + + source_type -> + info(source, source_type, opts) end + end @doc """ Returns a `%Fastimage{}` struct with information such as @@ -237,14 +241,14 @@ defmodule Fastimage do } acc = - if source_type == :url do - acc - |> maybe_put_option(:stream_timeout, stream_timeout) - |> maybe_put_option(:max_error_retries, max_error_retries) - |> maybe_put_option(:max_redirect_retries, max_redirect_retries) - else - acc - end + if source_type == :url do + acc + |> maybe_put_option(:stream_timeout, stream_timeout) + |> maybe_put_option(:max_error_retries, max_error_retries) + |> maybe_put_option(:max_redirect_retries, max_redirect_retries) + else + acc + end with {:ok, %Stream.Acc{} = updated_acc} <- Stream.stream_data(acc), bytes <- :erlang.binary_part(updated_acc.acc_data, {0, 2}), @@ -254,17 +258,18 @@ defmodule Fastimage do end defp info(source, source_type, opts) do - with {:ok, %Stream.Acc{image_type: type} = acc} <- get_acc_with_type(source, source_type, opts), + with {:ok, %Stream.Acc{image_type: type} = acc} <- + get_acc_with_type(source, source_type, opts), {:ok, %Dimensions{} = size} = Parser.size(type, acc) do Utils.close_stream(acc.stream_ref) {:ok, - %Fastimage{ - source: source, - source_type: source_type, - image_type: type, - dimensions: size - }} + %Fastimage{ + source: source, + source_type: source_type, + image_type: type, + dimensions: size + }} end end diff --git a/lib/fastimage/stream.ex b/lib/fastimage/stream.ex index ba8b22a..f4764fe 100644 --- a/lib/fastimage/stream.ex +++ b/lib/fastimage/stream.ex @@ -191,7 +191,7 @@ defmodule Fastimage.Stream do num_chunks_to_fetch: num_chunks_to_fetch, acc_num_chunks: acc_num_chunks, acc_data: acc_data, - max_redirect_retries: _max_redirect_retries, + max_redirect_retries: _max_redirect_retries } = acc ) do cond do @@ -202,7 +202,6 @@ defmodule Fastimage.Stream do _next_chunk = :hackney.stream_next(stream_ref) receive do - {:hackney_response, stream_ref, {:status, status_code, reason}} when status_code >= 400 -> reason = {:hackney_response_error, {acc.source, status_code, reason}} @@ -220,12 +219,12 @@ defmodule Fastimage.Stream do retry_for_redirects(%{acc | stream_ref: stream_ref}, to_url) {:hackney_response, stream_ref, :done} -> - stream_data( - %{acc | - num_chunks_to_fetch: 0, + stream_data(%{ + acc + | num_chunks_to_fetch: 0, stream_ref: stream_ref, - stream_state: :done} - ) + stream_state: :done + }) {:hackney_response, stream_ref, data} -> stream_data(%{ @@ -233,14 +232,13 @@ defmodule Fastimage.Stream do | num_chunks_to_fetch: num_chunks_to_fetch - 1, acc_num_chunks: acc_num_chunks + 1, acc_data: <>, - stream_ref: stream_ref, + stream_ref: stream_ref }) unexpected_reply -> reason = {:unexpected_http_streaming_error, acc.source, unexpected_reply} potential_error = {:error, Error.exception(reason)} maybe_retry_for_errors(acc, potential_error) - after stream_timeout -> reason = {:unexpected_http_streaming_error, acc.source} @@ -287,9 +285,9 @@ defmodule Fastimage.Stream do defp maybe_retry_for_errors( %Acc{ - stream_ref: stream_ref, - error_retries: error_retries, - max_error_retries: max_error_retries + stream_ref: stream_ref, + error_retries: error_retries, + max_error_retries: max_error_retries } = acc, error ) do diff --git a/mix.exs b/mix.exs index d8003f5..8faaa9b 100644 --- a/mix.exs +++ b/mix.exs @@ -73,7 +73,13 @@ defmodule Fastimage.Mixfile do defp aliases do [ - prep: ["clean", "format #{format_args()}", "compile", "credo #{credo_args()}"] + prep: [ + "clean", + "format #{format_args(:lib)}", + "format #{format_args(:test)}", + "compile", + "credo #{credo_args()}" + ] ] end @@ -81,8 +87,12 @@ defmodule Fastimage.Mixfile do "--strict --ignore maxlinelength,cyclomaticcomplexity,todo" end - defp format_args do - "mix.exs lib/**/*.{ex,exs}test/**/*.{ex,exs}" + defp format_args(:lib) do + "mix.exs lib/**/*.{ex,exs}" + end + + defp format_args(:test) do + "mix.exs test/**/*.{ex,exs}" end def coveralls do