Skip to content

Commit 0162dca

Browse files
committed
Use Jason Instead of JSX
Given that the broader Elixir community is circling their wagons around Jason, and issue parroty#153, it seems that moving to Jason would be a wise choice.
1 parent ae8e8f7 commit 0162dca

File tree

4 files changed

+42
-8
lines changed

4 files changed

+42
-8
lines changed

lib/exvcr/json.ex

+6-5
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ defmodule ExVCR.JSON do
77
Save responses into the json file.
88
"""
99
def save(file_name, recordings) do
10-
json = recordings
11-
|> Enum.map(&encode_binary_data/1)
12-
|> Enum.reverse()
13-
|> JSX.encode!()
14-
|> JSX.prettify!()
10+
json =
11+
recordings
12+
|> Enum.map(&encode_binary_data/1)
13+
|> Enum.reverse()
14+
|> Jason.encode_to_iodata!()
15+
|> Jason.Formatter.pretty_print_to_iodata()
1516

1617
unless File.exists?(path = Path.dirname(file_name)), do: File.mkdir_p!(path)
1718
File.write!(file_name, json)

lib/exvcr/records.ex

+34
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,51 @@ defmodule ExVCR.Record do
22
defstruct options: nil, responses: nil
33
end
44

5+
defmodule ExVCR.Utils do
6+
@moduledoc false
7+
8+
def keyword_to_map(kw) do
9+
Enum.reduce(kw, %{}, fn {k, v}, acc -> Map.put(acc, k, v) end)
10+
end
11+
end
12+
513
defmodule ExVCR.Request do
614
defstruct url: nil, headers: [], method: nil, body: nil, options: [], request_body: ""
15+
16+
defimpl Jason.Encoder, for: ExVCR.Request do
17+
def encode(value, opts) do
18+
%{headers: headers, options: options} = Map.take(value, [:headers, :options])
19+
20+
map = value
21+
|> Map.take([:url, :method, :body, :request_body])
22+
|> Map.put(:headers, ExVCR.Utils.keyword_to_map(headers))
23+
|> Map.put(:options, ExVCR.Utils.keyword_to_map(options))
24+
25+
Jason.Encode.map(map, opts)
26+
end
27+
end
728
end
829

930
defmodule ExVCR.Response do
1031
defstruct type: "ok", status_code: nil, headers: [], body: nil, binary: false
32+
33+
defimpl Jason.Encoder, for: ExVCR.Response do
34+
def encode(value, opts) do
35+
headers = Map.get(value, :headers)
36+
37+
map = value
38+
|> Map.take([:type, :status_code, :body, :binary])
39+
|> Map.put(:headers, ExVCR.Utils.keyword_to_map(headers))
40+
41+
Jason.Encode.map(map, opts)
42+
end
43+
end
1144
end
1245

1346
defmodule ExVCR.Checker.Results do
1447
defstruct dirs: nil, files: []
1548
end
49+
1650
defmodule ExVCR.Checker.Counts do
1751
defstruct server: 0, cache: 0
1852
end

mix.exs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ defmodule ExVCR.Mixfile do
2020
end
2121

2222
def application do
23-
[applications: [:meck, :exactor, :exjsx], mod: {ExVCR.Application, []}]
23+
[applications: [:meck, :exactor], mod: {ExVCR.Application, []}]
2424
end
2525

2626
def deps do
2727
[
2828
{:meck, "~> 0.8"},
2929
{:exactor, "~> 2.2"},
30-
{:exjsx, "~> 4.0"},
30+
{:jason, "~> 1.0"},
3131
{:ibrowse, "4.4.0", optional: true},
3232
{:httpotion, "~> 3.1", optional: true},
3333
{:httpoison, "~> 1.0", optional: true},

mix.lock

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"ibrowse": {:hex, :ibrowse, "4.4.0", "2d923325efe0d2cb09b9c6a047b2835a5eda69d8a47ed6ff8bc03628b764e991", [:rebar3], [], "hexpm", "6a8e5988872086f0506bef68311493551ac5beae7c06ba2a00d5e9f97a60f1c2"},
1717
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
1818
"jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"},
19-
"jsx": {:hex, :jsx, "2.8.3", "a05252d381885240744d955fbe3cf810504eb2567164824e19303ea59eef62cf", [:mix, :rebar3], [], "hexpm", "fc3499fed7a726995aa659143a248534adc754ebd16ccd437cd93b649a95091f"},
2019
"makeup": {:hex, :makeup, "1.0.0", "671df94cf5a594b739ce03b0d0316aa64312cee2574b6a44becb83cd90fb05dc", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "a10c6eb62cca416019663129699769f0c2ccf39428b3bb3c0cb38c718a0c186d"},
2120
"makeup_elixir": {:hex, :makeup_elixir, "0.14.0", "cf8b7c66ad1cff4c14679698d532f0b5d45a3968ffbcbfd590339cb57742f1ae", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "d4b316c7222a85bbaa2fd7c6e90e37e953257ad196dc229505137c5e505e9eff"},
2221
"meck": {:hex, :meck, "0.9.2", "85ccbab053f1db86c7ca240e9fc718170ee5bda03810a6292b5306bf31bae5f5", [:rebar3], [], "hexpm", "81344f561357dc40a8344afa53767c32669153355b626ea9fcbc8da6b3045826"},

0 commit comments

Comments
 (0)