content v1.3.0 Content

Content handles Content Negotiation in any Elixir/Phoenix App. Please see: github.com/dwyl/content for detail.

Link to this section Summary

Functions

call/2 is invoked to handle each HTTP request which Content inspects. If the accept header is "html", execute the html_plugs for that request. If the accept header contains "json" return the conn unmodified. If the url terminate with .json return the conn unmodified.

get_accept_header/1 gets the "accept" header from req_headers. Defaults to "text/html" if no header is set.

init/1 initialises the options passed in and makes them available in the lifecycle of the call/2 invocation (below). When invoking the Content plug in a Phoenix router pipeline, we pass in a Map containing a key html_plugs with a list of plugs that need to be run when the accept header is "html". See implimentation docs for more detail/clarity.

redirect_path/1 returns the conn.request_path with the .json removed.

reply/5 gets the "accept" header from req_headers. Verify if url finishes with .json. Defaults to "text/html" if no header is set. The Content.reply/5 accepts the following 5 argument

url_json?/1 check if the current url endpoint terminate with .json

wildcard_redirect/3 redirects a "/route.json" request to "/route" such that a request to "/admin/profile.json" invokes "admin/profile" router is the Phoenix Router for your app, e.g: MyApp.Router see: https://github.com/dwyl/content#4-wildcard-routing

Link to this section Functions

Link to this function

call(conn, options)

call/2 is invoked to handle each HTTP request which Content inspects. If the accept header is "html", execute the html_plugs for that request. If the accept header contains "json" return the conn unmodified. If the url terminate with .json return the conn unmodified.

Link to this function

get_accept_header(conn)

get_accept_header/1 gets the "accept" header from req_headers. Defaults to "text/html" if no header is set.

init/1 initialises the options passed in and makes them available in the lifecycle of the call/2 invocation (below). When invoking the Content plug in a Phoenix router pipeline, we pass in a Map containing a key html_plugs with a list of plugs that need to be run when the accept header is "html". See implimentation docs for more detail/clarity.

Link to this function

redirect_path(conn)

redirect_path/1 returns the conn.request_path with the .json removed.

Link to this function

reply(conn, render, template, json, data)

reply/5 gets the "accept" header from req_headers. Verify if url finishes with .json. Defaults to "text/html" if no header is set. The Content.reply/5 accepts the following 5 argument:

  1. conn - the Plug.Conn where we get the req_headers from.
  2. render/3 - the Phoenix.Controller.render/3 function, or your own implementation of a render function that takes conn, template and data as it's 3 params.
  3. template - the .html template to be rendered if the accept header matches "html"; e.g: "index.html"
  4. json/2 - the Phoenix.Controller.json/2 function that renders json data. Or your own implementation that accepts the two params: conn and data corresponding to the Plug.Conn and the json data you want to return.
  5. data - the data we want to render as HTML or JSON.
Link to this function

url_json?(conn)

url_json?/1 check if the current url endpoint terminate with .json

Link to this function

wildcard_redirect(conn, params, router)

wildcard_redirect/3 redirects a "/route.json" request to "/route" such that a request to "/admin/profile.json" invokes "admin/profile" router is the Phoenix Router for your app, e.g: MyApp.Router see: https://github.com/dwyl/content#4-wildcard-routing