Skip to content

Commit

Permalink
Merge pull request #145 from Zorbash/deserialization-options
Browse files Browse the repository at this point in the history
Implement configurable deserialization options
  • Loading branch information
edgurgel committed May 7, 2018
2 parents 174f1af + 85f0a07 commit 42b5d0d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion lib/tentacat.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand Down
17 changes: 16 additions & 1 deletion test/tentacat_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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=="}
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 42b5d0d

Please sign in to comment.