Skip to content

Commit

Permalink
replace case clauses with functions
Browse files Browse the repository at this point in the history
  • Loading branch information
goofansu committed Sep 1, 2024
1 parent 922af50 commit f52cb53
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions lib/open_graph.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,25 @@ defmodule OpenGraph do
def fetch(url, req_options \\ []) do
options = Keyword.merge(req_options, Application.get_env(:ogp, :req_options, []))

case Req.get(url, options) do
{:ok, %Req.Response{status: status} = response} when status in 200..299 ->
{:ok, parse(response.body)}

{:ok, %Req.Response{status: status}} when status in 300..399 ->
{:error,
%OpenGraph.Error{
reason: {:missing_redirect_location, status}
}}

{:ok, %Req.Response{status: status}} ->
{:error,
%OpenGraph.Error{
reason: {:unexpected_status_code, status}
}}
url
|> Req.get(options)
|> handle_response()
end

{:error, error} ->
{:error,
%OpenGraph.Error{
reason: {:request_error, Exception.message(error)}
}}
end
defp handle_response({:ok, %Req.Response{status: status} = response}) when status in 200..299 do
{:ok, parse(response.body)}
end

defp handle_response({:ok, %Req.Response{status: status}}) when status in 300..399 do
{:error, %OpenGraph.Error{reason: {:missing_redirect_location, status}}}
end

defp handle_response({:ok, %Req.Response{status: status}}) do
{:error, %OpenGraph.Error{reason: {:unexpected_status_code, status}}}
end

defp handle_response({:error, error}) do
{:error, %OpenGraph.Error{reason: {:request_error, Exception.message(error)}}}
end

@doc """
Expand Down

0 comments on commit f52cb53

Please sign in to comment.