diff --git a/README.md b/README.md index e8014cf..087ec2a 100644 --- a/README.md +++ b/README.md @@ -193,13 +193,26 @@ iex> Tentacat.Users.me(client) ``` ## Misc + Having that Github Reviews API is still in a pre-release state you need to set an additional header in your config. ```elixir -config :tentacat, :extra_headers, [{"Accept", "application/vnd.github.black-cat-preview+json"}]) +config :tentacat, :extra_headers, [{"Accept", "application/vnd.github.black-cat-preview+json"}] ``` +### Deserialization Options + +You can pass deserialization options to the library used to decode JSON +using: + +```elixir +# To have Atom keys +config :tentacat, :deserialization_options, [labels: :atoms] +``` + +See: https://github.com/talentdeficit/exjsx#decodejson-opts for available options. + ## Contributing Start by forking this repo diff --git a/lib/tentacat.ex b/lib/tentacat.ex index a89793e..f529fa4 100644 --- a/lib/tentacat.ex +++ b/lib/tentacat.ex @@ -9,7 +9,7 @@ defmodule Tentacat do @spec process_response_body(binary) :: term def process_response_body(""), do: nil - def process_response_body(body), do: JSX.decode!(body) + def process_response_body(body), do: JSX.decode!(body, deserialization_options()) @spec process_response(HTTPoison.Response.t()) :: response def process_response(%HTTPoison.Response{status_code: status_code, body: body} = resp), @@ -78,6 +78,10 @@ defmodule Tentacat do Application.get_env(:tentacat, :extra_headers, []) end + defp deserialization_options do + Application.get_env(:tentacat, :deserialization_options, [labels: :binary]) + end + defp pagination(options) do Keyword.get(options, :pagination, Application.get_env(:tentacat, :pagination, nil)) end diff --git a/test/tentacat_test.exs b/test/tentacat_test.exs index b57688e..c8596b0 100644 --- a/test/tentacat_test.exs +++ b/test/tentacat_test.exs @@ -12,6 +12,12 @@ defmodule TentacatTest do end) end + setup do + on_exit(fn -> + Application.delete_env(:tentacat, :deserialization_options) + end) + end + test "authorization_header using user and password" do assert authorization_header(%{user: "user", password: "password"}, []) == [ {"Authorization", "Basic dXNlcjpwYXNzd29yZA=="} @@ -50,7 +56,16 @@ defmodule TentacatTest do end test "process_response_body with content" do - :meck.expect(JSX, :decode!, 1, :decoded_json) + :meck.expect(JSX, :decode!, 2, :decoded_json) + + assert process_response_body("json") == :decoded_json + end + + test "process_response_body with serialization options" do + Application.put_env :tentacat, :deserialization_options, [keys: :atoms] + + :meck.expect(JSX, :decode!, fn _, [keys: :atoms] -> :decoded_json end) + assert process_response_body("json") == :decoded_json end