Skip to content

Commit

Permalink
Fix compilation in project lacking Finch
Browse files Browse the repository at this point in the history
Closes #204
  • Loading branch information
nonninz committed May 12, 2023
1 parent 15debf0 commit e6858b8
Showing 1 changed file with 81 additions and 76 deletions.
157 changes: 81 additions & 76 deletions lib/exvcr/adapter/finch/converter.ex
@@ -1,86 +1,91 @@
defmodule ExVCR.Adapter.Finch.Converter do
@moduledoc """
Provides helpers to mock Finch methods.
"""

use ExVCR.Converter

alias ExVCR.Util

defp string_to_response(string) do
response = Enum.map(string, fn({x, y}) -> {String.to_atom(x), y} end)
response = struct(ExVCR.Response, response)

response =
if response.type == "error" do
body = string_to_error_reason(response.body)
%{response | body: body}
else
response
end

response =
if is_map(response.headers) do
headers = response.headers
|> Map.to_list
|> Enum.map(fn({k,v}) -> {k, v} end)
%{response | headers: headers}
else
response
end

response
end
if Code.ensure_loaded?(Finch) do
defmodule ExVCR.Adapter.Finch.Converter do
@moduledoc """
Provides helpers to mock Finch methods.
"""

defp string_to_error_reason(reason) do
{reason_struct, _} = Code.eval_string(reason)
reason_struct
end
use ExVCR.Converter

defp request_to_string([request, finch_module]) do
request_to_string([request, finch_module, []])
end
alias ExVCR.Util

defp request_to_string([request, _finch_module, opts]) do
url = Util.build_url(request.scheme, request.host, request.path, request.port, request.query)
defp string_to_response(string) do
response = Enum.map(string, fn {x, y} -> {String.to_atom(x), y} end)
response = struct(ExVCR.Response, response)

%ExVCR.Request{
url: parse_url(url),
headers: parse_headers(request.headers),
method: String.downcase(request.method),
body: parse_request_body(request.body),
options: parse_options(sanitize_options(opts))
}
end
response =
if response.type == "error" do
body = string_to_error_reason(response.body)
%{response | body: body}
else
response
end

# If option value is tuple, make it as list, for encoding as json.
defp sanitize_options(options) do
Enum.map(options, fn({key, value}) ->
if is_tuple(value) do
{key, Tuple.to_list(value)}
else
{key, value}
end
end)
end
response =
if is_map(response.headers) do
headers =
response.headers
|> Map.to_list()
|> Enum.map(fn {k, v} -> {k, v} end)

defp response_to_string({:ok, %Finch.Response{} = response}), do: response_to_string(response)
%{response | headers: headers}
else
response
end

defp response_to_string(%Finch.Response{} = response) do
%ExVCR.Response{
type: "ok",
status_code: response.status,
headers: parse_headers(response.headers),
body: to_string(response.body)
}
end
response
end

defp response_to_string({:error, reason}) do
%ExVCR.Response{
type: "error",
body: error_reason_to_string(reason)
}
end
defp string_to_error_reason(reason) do
{reason_struct, _} = Code.eval_string(reason)
reason_struct
end

defp request_to_string([request, finch_module]) do
request_to_string([request, finch_module, []])
end

defp error_reason_to_string(reason), do: Macro.to_string(reason)
defp request_to_string([request, _finch_module, opts]) do
url =
Util.build_url(request.scheme, request.host, request.path, request.port, request.query)

%ExVCR.Request{
url: parse_url(url),
headers: parse_headers(request.headers),
method: String.downcase(request.method),
body: parse_request_body(request.body),
options: parse_options(sanitize_options(opts))
}
end

# If option value is tuple, make it as list, for encoding as json.
defp sanitize_options(options) do
Enum.map(options, fn {key, value} ->
if is_tuple(value) do
{key, Tuple.to_list(value)}
else
{key, value}
end
end)
end

defp response_to_string({:ok, %Finch.Response{} = response}), do: response_to_string(response)

defp response_to_string(%Finch.Response{} = response) do
%ExVCR.Response{
type: "ok",
status_code: response.status,
headers: parse_headers(response.headers),
body: to_string(response.body)
}
end

defp response_to_string({:error, reason}) do
%ExVCR.Response{
type: "error",
body: error_reason_to_string(reason)
}
end

defp error_reason_to_string(reason), do: Macro.to_string(reason)
end
end

0 comments on commit e6858b8

Please sign in to comment.