I need to reshape my ecto models when responding to API requests, and I also need to create request objects that I want to bind to when I get a API request.
So my API responses in Ruby would look like:
class ApiResponse
attr_accessor :success, :errors
def initialize(success, errors) @success = success @errors = errors
end
And then say for my /api/users response I would have a users collection also so:
The other way of looking at it is as a “Content-Template” - the term “view” is heavily influenced by the pre-dominance of a Content-Type of text/html in many HTTP requests/responses.
When the required Content-Type is application/json you simply select a JSON template to render your Elixir data into the appropriate media-type representation.
The underlying idea being is that the same data (“resource state”) can have representations across a whole range of different media-types.