Last active
August 29, 2015 14:17
-
-
Save jeregrine/b6ba711775807bcee218 to your computer and use it in GitHub Desktop.
Encoding with Poison.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule MyApp.MyModelController do | |
| use MyApp.Web, :controller | |
| plug :action | |
| def show(conn, params) do | |
| json conn, Repo.get!(MyModel, params[:id]) | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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