Skip to content

Commit

Permalink
mix format
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Moloney authored and stephenmoloney committed Mar 3, 2018
1 parent 1dadd20 commit ed525cc
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
63 changes: 34 additions & 29 deletions lib/fastimage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}),
Expand All @@ -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

Expand Down
22 changes: 10 additions & 12 deletions lib/fastimage/stream.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}}
Expand All @@ -220,27 +219,26 @@ 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(%{
acc
| num_chunks_to_fetch: num_chunks_to_fetch - 1,
acc_num_chunks: acc_num_chunks + 1,
acc_data: <<acc_data::binary, data::binary>>,
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}
Expand Down Expand Up @@ -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
Expand Down
16 changes: 13 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,26 @@ 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

defp credo_args 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
Expand Down

0 comments on commit ed525cc

Please sign in to comment.