Skip to content

Instantly share code, notes, and snippets.

@jeregrine
Last active August 29, 2015 14:17
Show Gist options
  • Save jeregrine/b6ba711775807bcee218 to your computer and use it in GitHub Desktop.
Save jeregrine/b6ba711775807bcee218 to your computer and use it in GitHub Desktop.
Encoding with Poison.
defmodule MyApp.MyModelController do
use MyApp.Web, :controller
plug :action
def show(conn, params) do
json conn, Repo.get!(MyModel, params[:id])
end
end
defimpl Poison.Encoder, for: MyModel do
# This is a sigil that produces a list of atoms
@attributes ~W(id body title user comments inserted_at)
def encode(comment, _options) do
comment
|> Map.take(@attributes)
|> Map.update!(:title, &String.capitalize)
|> Poison.encode!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment