Transformation: Convert Response

We’ll need a JSON library to convert the response into a data structure. Searching hex.pm, I found the poison library[23] (no relation to HTTPoison), so let’s add its dependency to our mix.exs file.

 defp​ deps ​do
  [
 httpoison:​ ​"​​~> 0.8"​,
 poison:​ ​"​​~> 1.5"
  ]
 end

Run mix deps.get, and you’ll end up with poison installed.

To convert the body from a string, we call the Poison.Parser.parse! function when we return the message from the GitHub API:

 def​ handle_response({​:ok​, %{​status_code:​ 200, ​body:​ body}}) ​do
» { ​:ok​, Poison.Parser.parse!(body) }
 end
 
 def​ handle_response({_, %{​status_code:​ _, ​body:​ body}}) ...

Get Programming Elixir 1.2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.