Skip to content

Commit

Permalink
Use Jason Instead of JSX
Browse files Browse the repository at this point in the history
Given that the broader Elixir community is circling their wagons around
Jason, and issue #153, it seems that moving to Jason would be a wise
choice.
  • Loading branch information
jherdman committed Aug 29, 2021
1 parent ae8e8f7 commit 2eeff29
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 20 deletions.
8 changes: 5 additions & 3 deletions lib/exvcr/iex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ defmodule ExVCR.IEx do
unquote(test)
after
ExVCR.MockLock.release_lock()
Recorder.get(recorder)
|> JSX.encode!
|> JSX.prettify!

recorder
|> Recorder.get()
|> Jason.encode_to_iodata!()
|> Jason.Formatter.pretty_print_to_iodata()
|> IO.puts
end
:ok
Expand Down
13 changes: 7 additions & 6 deletions lib/exvcr/json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ defmodule ExVCR.JSON do
Save responses into the json file.
"""
def save(file_name, recordings) do
json = recordings
|> Enum.map(&encode_binary_data/1)
|> Enum.reverse()
|> JSX.encode!()
|> JSX.prettify!()
json =
recordings
|> Enum.map(&encode_binary_data/1)
|> Enum.reverse()
|> Jason.encode_to_iodata!()
|> Jason.Formatter.pretty_print_to_iodata()

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

Expand Down
34 changes: 34 additions & 0 deletions lib/exvcr/records.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,51 @@ defmodule ExVCR.Record do
defstruct options: nil, responses: nil
end

defmodule ExVCR.Utils do
@moduledoc false

def keyword_to_map(kw) do
Enum.reduce(kw, %{}, fn {k, v}, acc -> Map.put(acc, k, v) end)
end
end

defmodule ExVCR.Request do
defstruct url: nil, headers: [], method: nil, body: nil, options: [], request_body: ""

defimpl Jason.Encoder, for: ExVCR.Request do
def encode(value, opts) do
%{headers: headers, options: options} = Map.take(value, [:headers, :options])

map = value
|> Map.take([:url, :method, :body, :request_body])
|> Map.put(:headers, ExVCR.Utils.keyword_to_map(headers))
|> Map.put(:options, ExVCR.Utils.keyword_to_map(options))

Jason.Encode.map(map, opts)
end
end
end

defmodule ExVCR.Response do
defstruct type: "ok", status_code: nil, headers: [], body: nil, binary: false

defimpl Jason.Encoder, for: ExVCR.Response do
def encode(value, opts) do
headers = Map.get(value, :headers)

map = value
|> Map.take([:type, :status_code, :body, :binary])
|> Map.put(:headers, ExVCR.Utils.keyword_to_map(headers))

Jason.Encode.map(map, opts)
end
end
end

defmodule ExVCR.Checker.Results do
defstruct dirs: nil, files: []
end

defmodule ExVCR.Checker.Counts do
defstruct server: 0, cache: 0
end
16 changes: 9 additions & 7 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 |> Jason.Formatter.pretty_print() |> String.replace(~r/\\n/, "\n")
display_parsed_body(json)
IO.puts "\e[32m**************************************\e[m"
else
Expand All @@ -25,17 +25,19 @@ defmodule ExVCR.Task.Show do
end

defp display_parsed_body(json) do
case extract_body(json) |> JSX.prettify do
{:ok, body_json } ->
IO.puts "\n\e[33m[Showing parsed JSON body]\e[m"
IO.puts body_json
_ -> nil
try do
json
|> extract_body()
|> Jason.Formatter.pretty_print()
rescue
_ ->
nil
end
end

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

def application do
[applications: [:meck, :exactor, :exjsx], mod: {ExVCR.Application, []}]
[applications: [:meck, :exactor], mod: {ExVCR.Application, []}]
end

def deps do
[
{:meck, "~> 0.8"},
{:exactor, "~> 2.2"},
{:exjsx, "~> 4.0"},
{:jason, "~> 1.0"},
{:ibrowse, "4.4.0", optional: true},
{:httpotion, "~> 3.1", optional: true},
{:httpoison, "~> 1.0", optional: true},
Expand Down
2 changes: 0 additions & 2 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"ex_doc": {:hex, :ex_doc, "0.21.3", "857ec876b35a587c5d9148a2512e952e24c24345552259464b98bfbb883c7b42", [:mix], [{:earmark, "~> 1.4", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm", "0db1ee8d1547ab4877c5b5dffc6604ef9454e189928d5ba8967d4a58a801f161"},
"exactor": {:hex, :exactor, "2.2.4", "5efb4ddeb2c48d9a1d7c9b465a6fffdd82300eb9618ece5d34c3334d5d7245b1", [:mix], [], "hexpm", "1222419f706e01bfa1095aec9acf6421367dcfab798a6f67c54cf784733cd6b5"},
"excoveralls": {:hex, :excoveralls, "0.14.2", "f9f5fd0004d7bbeaa28ea9606251bb643c313c3d60710bad1f5809c845b748f0", [:mix], [{:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "ca6fd358621cb4d29311b29d4732c4d47dac70e622850979bc54ed9a3e50f3e1"},
"exjsx": {:hex, :exjsx, "4.0.0", "60548841e0212df401e38e63c0078ec57b33e7ea49b032c796ccad8cde794b5c", [:mix], [{:jsx, "~> 2.8.0", [hex: :jsx, repo: "hexpm", optional: false]}], "hexpm", "32e95820a97cffea67830e91514a2ad53b888850442d6d395f53a1ac60c82e07"},
"finch": {:hex, :finch, "0.8.0", "9fe1b7b1613f4f0f43ac4be94462a0f3eb13264e5e9a624005da5670e452a1d1", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: false]}, {:mint, "~> 1.3", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.3.5", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 0.2", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "07650fd74da3fb51984b9f62bf4db3ef720f3c06d914d716cceeb13a2bc2542d"},
"hackney": {:hex, :hackney, "1.17.4", "99da4674592504d3fb0cfef0db84c3ba02b4508bae2dff8c0108baa0d6e0977c", [:rebar3], [{:certifi, "~>2.6.1", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~>6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~>1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~>1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "de16ff4996556c8548d512f4dbe22dd58a587bf3332e7fd362430a7ef3986b16"},
"http_server": {:git, "https://github.com/parroty/http_server.git", "922d10420836a51289ed04f0bb5022bf695da1ab", []},
Expand All @@ -16,7 +15,6 @@
"ibrowse": {:hex, :ibrowse, "4.4.0", "2d923325efe0d2cb09b9c6a047b2835a5eda69d8a47ed6ff8bc03628b764e991", [:rebar3], [], "hexpm", "6a8e5988872086f0506bef68311493551ac5beae7c06ba2a00d5e9f97a60f1c2"},
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
"jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"},
"jsx": {:hex, :jsx, "2.8.3", "a05252d381885240744d955fbe3cf810504eb2567164824e19303ea59eef62cf", [:mix, :rebar3], [], "hexpm", "fc3499fed7a726995aa659143a248534adc754ebd16ccd437cd93b649a95091f"},
"makeup": {:hex, :makeup, "1.0.0", "671df94cf5a594b739ce03b0d0316aa64312cee2574b6a44becb83cd90fb05dc", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "a10c6eb62cca416019663129699769f0c2ccf39428b3bb3c0cb38c718a0c186d"},
"makeup_elixir": {:hex, :makeup_elixir, "0.14.0", "cf8b7c66ad1cff4c14679698d532f0b5d45a3968ffbcbfd590339cb57742f1ae", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "d4b316c7222a85bbaa2fd7c6e90e37e953257ad196dc229505137c5e505e9eff"},
"meck": {:hex, :meck, "0.9.2", "85ccbab053f1db86c7ca240e9fc718170ee5bda03810a6292b5306bf31bae5f5", [:rebar3], [], "hexpm", "81344f561357dc40a8344afa53767c32669153355b626ea9fcbc8da6b3045826"},
Expand Down

0 comments on commit 2eeff29

Please sign in to comment.