Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/exvcr/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,8 @@ defmodule ExVCR.Config do
def strict_mode(value) do
Setting.set(:strict_mode, value)
end

def json_toolkit(json_toolkit) do
Setting.set(:json_toolkit, json_toolkit)
end
end
12 changes: 12 additions & 0 deletions lib/exvcr/config_loader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,17 @@ defmodule ExVCR.ConfigLoader do
if env[:strict_mode] != nil do
Config.strict_mode(env[:strict_mode])
end

config_json_toolkit = env[:json_toolkit]

if config_json_toolkit != nil && Code.ensure_loaded?(config_json_toolkit) do
Config.json_toolkit(config_json_toolkit)
else
cond do
Code.ensure_loaded?(JSX) -> Config.json_toolkit(ExVCR.JsonAdapter.JSXAdapter)
Code.ensure_loaded?(Jason) -> Config.json_toolkit(ExVCR.JsonAdapter.JasonAdapter)
true -> raise "no json toolkit"
end
end
end
end
4 changes: 2 additions & 2 deletions lib/exvcr/iex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ defmodule ExVCR.IEx do
:meck.unload(unquote(adapter.module_name))
ExVCR.MockLock.release_lock()
Recorder.get(recorder)
|> JSX.encode!
|> JSX.prettify!
|> ExVCR.JsonAdapter.encode!()
|> ExVCR.JsonAdapter.prettify!()
|> IO.puts
end
:ok
Expand Down
6 changes: 3 additions & 3 deletions lib/exvcr/json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ defmodule ExVCR.JSON do
json = recordings
|> Enum.map(&encode_binary_data/1)
|> Enum.reverse()
|> JSX.encode!()
|> JSX.prettify!()
|> ExVCR.JsonAdapter.encode!()
|> ExVCR.JsonAdapter.prettify!()

unless File.exists?(path = Path.dirname(file_name)), do: File.mkdir_p!(path)
File.write!(file_name, json)
Expand Down Expand Up @@ -48,7 +48,7 @@ defmodule ExVCR.JSON do
def read_json_file(file_name) do
file_name
|> File.read!()
|> JSX.decode!()
|> ExVCR.JsonAdapter.decode!()
|> Enum.map(&load_binary_data/1)
end

Expand Down
60 changes: 60 additions & 0 deletions lib/exvcr/json_adapter.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
defmodule ExVCR.JsonAdapter do
def encode!(input), do: ExVCR.Setting.get(:json_toolkit).encode!(input)
def decode!(input), do: ExVCR.Setting.get(:json_toolkit).decode!(input)

def prettify(input), do: ExVCR.Setting.get(:json_toolkit).prettify(input)

def prettify!(input), do: ExVCR.Setting.get(:json_toolkit).prettify!(input)

if Code.ensure_loaded?(Jason) do
defmodule JasonAdapter do
require Protocol

defimpl Jason.Encoder, for: ExVCR.Request do
def encode(value, opts) do
value
|> Map.update(:headers, %{}, fn headers -> Enum.into(headers, %{}) end)
|> Map.update(:options, %{}, fn headers -> Enum.into(headers, %{}) end)
|> Map.from_struct()
|> Jason.Encode.map(opts)
end
end

defimpl Jason.Encoder, for: ExVCR.Response do
def encode(value, opts) do
value
|> Map.update(:headers, %{}, fn headers -> Enum.into(headers, %{}) end)
|> Map.from_struct()
|> Jason.Encode.map(opts)
end
end

def decode!(input) do
Jason.decode!(input)
end

def encode!(input) do
Jason.encode!(input)
end

def prettify!(input) do
Jason.Formatter.pretty_print(input)
end

def prettify(input) do
Jason.Formatter.pretty_print(input)
end
end
end

if Code.ensure_loaded?(JSX) do
defmodule JSXAdapter do
def decode!(input), do: JSX.decode!(input)
def encode!(input), do: JSX.encode!(input)

def prettify(input), do: JSX.prettify(input)

def prettify!(input), do: JSX.prettify!(input)
end
end
end
6 changes: 3 additions & 3 deletions lib/exvcr/task/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule ExVCR.Task.Show do
IO.puts "\e[32mShowing #{file}\e[m"
IO.puts "\e[32m**************************************\e[m"
json = File.read!(file)
IO.puts json |> JSX.prettify! |> String.replace(~r/\\n/, "\n")
IO.puts json |> ExVCR.JsonAdapter.prettify! |> String.replace(~r/\\n/, "\n")
display_parsed_body(json)
IO.puts "\e[32m**************************************\e[m"
else
Expand All @@ -25,7 +25,7 @@ defmodule ExVCR.Task.Show do
end

defp display_parsed_body(json) do
case extract_body(json) |> JSX.prettify do
case extract_body(json) |> ExVCR.JsonAdapter.prettify do
{:ok, body_json } ->
IO.puts "\n\e[33m[Showing parsed JSON body]\e[m"
IO.puts body_json
Expand All @@ -35,7 +35,7 @@ defmodule ExVCR.Task.Show do

defp extract_body(json) do
json
|> JSX.decode!()
|> ExVCR.JsonAdapter.decode!()
|> List.first()
|> Enum.into(%{})
|> get_in(["responce", "body"])
Expand Down
14 changes: 9 additions & 5 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ defmodule ExVCR.Mixfile do
use Mix.Project

def project do
[ app: :exvcr,
[
app: :exvcr,
version: "0.11.1",
elixir: "~> 1.3",
deps: deps(),
Expand All @@ -15,7 +16,7 @@ defmodule ExVCR.Mixfile do

# Configuration for the OTP application
def application do
[applications: [:meck, :exactor, :exjsx]]
[applications: [:meck, :exactor | if(Code.ensure_loaded?(JSX), do: [:exjsx], else: [])]]
end

# Returns the list of dependencies in the format:
Expand All @@ -24,7 +25,8 @@ defmodule ExVCR.Mixfile do
[
{:meck, "~> 0.8"},
{:exactor, "~> 2.2"},
{:exjsx, "~> 4.0"},
{:exjsx, "~> 4.0", optional: true, test: true},
{:jason, "~> 1.1", optional: true, test: true},
{:ibrowse, "~> 4.4", optional: true},
{:httpotion, "~> 3.1", optional: true},
{:httpoison, "~> 1.0", optional: true},
Expand All @@ -41,8 +43,10 @@ defmodule ExVCR.Mixfile do
end

defp package do
[ maintainers: ["parroty"],
[
maintainers: ["parroty"],
licenses: ["MIT"],
links: %{"GitHub" => "https://github.com/parroty/exvcr"} ]
links: %{"GitHub" => "https://github.com/parroty/exvcr"}
]
end
end
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"httpotion": {:hex, :httpotion, "3.1.0", "14d20d9b0ce4e86e253eb91e4af79e469ad949f57a5d23c0a51b2f86559f6589", [:mix], [{:ibrowse, "~> 4.4", [hex: :ibrowse, repo: "hexpm", optional: false]}], "hexpm"},
"ibrowse": {:hex, :ibrowse, "4.4.0", "2d923325efe0d2cb09b9c6a047b2835a5eda69d8a47ed6ff8bc03628b764e991", [:rebar3], [], "hexpm"},
"idna": {:hex, :idna, "5.1.0", "d72b4effeb324ad5da3cab1767cb16b17939004e789d8c0ad5b70f3cea20c89a", [:rebar3], [{:unicode_util_compat, "0.3.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"},
"jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"},
"jsx": {:hex, :jsx, "2.8.3", "a05252d381885240744d955fbe3cf810504eb2567164824e19303ea59eef62cf", [:mix, :rebar3], [], "hexpm"},
"makeup": {:hex, :makeup, "1.0.0", "671df94cf5a594b739ce03b0d0316aa64312cee2574b6a44becb83cd90fb05dc", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"},
"makeup_elixir": {:hex, :makeup_elixir, "0.14.0", "cf8b7c66ad1cff4c14679698d532f0b5d45a3968ffbcbfd590339cb57742f1ae", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm"},
Expand Down