Skip to content

Commit

Permalink
Merge 5f54136 into aedbe68
Browse files Browse the repository at this point in the history
  • Loading branch information
BlakeWilliams committed Mar 3, 2014
2 parents aedbe68 + 5f54136 commit 9bba471
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 81 deletions.
29 changes: 2 additions & 27 deletions lib/exvcr/adapter/hackney/converter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,7 @@ defmodule ExVCR.Adapter.Hackney.Converter do
Provides helpers to mock :hackney methods.
"""

@doc """
Parse string fromat into original request / response format.
"""
def convert_from_string([{"request", request}, {"response", response}]) do
[ request: string_to_request(request), response: string_to_response(response) ]
end

@doc """
Parse request and response parameters into string format.
"""
def convert_to_string(request, response) do
[ request: request_to_string(request), response: response_to_string(response) ]
end

defp string_to_request(string) do
Enum.map(string, fn({x,y}) -> {binary_to_atom(x),y} end) |> ExVCR.Request.new
end
use ExVCR.Converter

defp string_to_response(string) do
Enum.map(string, fn({x, y}) -> {binary_to_atom(x), y} end) |> ExVCR.Response.new
Expand Down Expand Up @@ -51,13 +35,4 @@ defmodule ExVCR.Adapter.Hackney.Converter do
body: atom_to_binary(reason)
)
end

defp parse_headers(headers) do
do_parse_headers(headers, [])
end

defp do_parse_headers([], acc), do: Enum.reverse(acc)
defp do_parse_headers([{key,value}|tail], acc) do
do_parse_headers(tail, [{to_string(key), to_string(value)}|acc])
end
end
end
29 changes: 2 additions & 27 deletions lib/exvcr/adapter/httpc/converter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,7 @@ defmodule ExVCR.Adapter.Httpc.Converter do
Provides helpers to mock :httpc methods.
"""

@doc """
Parse string fromat into original request / response format.
"""
def convert_from_string([{"request", request}, {"response", response}]) do
[ request: string_to_request(request), response: string_to_response(response) ]
end

@doc """
Parse request and response parameters into string format.
"""
def convert_to_string(request, response) do
[ request: request_to_string(request), response: response_to_string(response) ]
end

defp string_to_request(string) do
Enum.map(string, fn({x,y}) -> {binary_to_atom(x),y} end) |> ExVCR.Request.new
end
use ExVCR.Converter

defp string_to_response(string) do
response = Enum.map(string, fn({x, y}) -> {binary_to_atom(x), y} end) |> ExVCR.Response.new
Expand Down Expand Up @@ -68,13 +52,4 @@ defmodule ExVCR.Adapter.Httpc.Converter do
body: atom_to_binary(reason)
)
end

defp parse_headers(headers) do
do_parse_headers(headers, [])
end

defp do_parse_headers([], acc), do: Enum.reverse(acc)
defp do_parse_headers([{key,value}|tail], acc) do
do_parse_headers(tail, [{to_string(key), to_string(value)}|acc])
end
end
end
29 changes: 2 additions & 27 deletions lib/exvcr/adapter/ibrowse/converter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,7 @@ defmodule ExVCR.Adapter.IBrowse.Converter do
Provides helpers to mock :ibrowse methods.
"""

@doc """
Parse string fromat into original request / response format.
"""
def convert_from_string([{"request", request}, {"response", response}]) do
[ request: string_to_request(request), response: string_to_response(response) ]
end

@doc """
Parse request and response parameters into string format.
"""
def convert_to_string(request, response) do
[ request: request_to_string(request), response: response_to_string(response) ]
end

defp string_to_request(string) do
Enum.map(string, fn({x,y}) -> {binary_to_atom(x),y} end) |> ExVCR.Request.new
end
use ExVCR.Converter

defp string_to_response(string) do
response = Enum.map(string, fn({x, y}) -> {binary_to_atom(x), y} end) |> ExVCR.Response.new
Expand Down Expand Up @@ -81,13 +65,4 @@ defmodule ExVCR.Adapter.IBrowse.Converter do
if is_binary(x), do: binary_to_atom(x), else: x
end) |> list_to_tuple
end

defp parse_headers(headers) do
do_parse_headers(headers, [])
end

defp do_parse_headers([], acc), do: Enum.reverse(acc)
defp do_parse_headers([{key,value}|tail], acc) do
do_parse_headers(tail, [{to_string(key), to_string(value)}|acc])
end
end
end
43 changes: 43 additions & 0 deletions lib/exvcr/converter.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
defmodule ExVCR.Converter do
@moduledoc """
Provides helpers for adapter converters
"""

defmacro __using__(_) do
quote do
@doc """
Parse string fromat into original request / response format.
"""
def convert_from_string([{"request", request}, {"response", response}]) do
[ request: string_to_request(request), response: string_to_response(response) ]
end
defoverridable [convert_from_string: 1]

@doc """
Parse request and response parameters into string format.
"""
def convert_to_string(request, response) do
[ request: request_to_string(request), response: response_to_string(response) ]
end
defoverridable [convert_to_string: 2]

defp string_to_request(string) do
Enum.map(string, fn({x,y}) -> {binary_to_atom(x),y} end) |> ExVCR.Request.new
end
defoverridable [string_to_request: 1]

defp parse_headers(headers) do
do_parse_headers(headers, [])
end
defoverridable [parse_headers: 1]

defp do_parse_headers([], acc), do: Enum.reverse(acc)
defp do_parse_headers([{key,value}|tail], acc) do
replaced_value = to_string(value) |> ExVCR.Filter.filter_sensitive_data
do_parse_headers(tail, [{to_string(key), replaced_value}|acc])
end
defoverridable [do_parse_headers: 2]
end

end
end

0 comments on commit 9bba471

Please sign in to comment.