Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add output examples for the adapters #1375

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
223 changes: 223 additions & 0 deletions docs/general/adapters.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,147 @@ You can use one of the built-in adapters (```Attributes``` is the default one) o
It's the default adapter, it generates a json response without a root key.
Doesn't follow any specific convention.

### Output example

```json
[
{
id: 1,
title: "My new post",
body: "Testing my blog.",
authors: [
{
id: 1,
name: "Jon Snow",
email: "jon@thewall.com",
created_at: "2015-12-15T00:14:41.149Z",
updated_at: "2015-12-15T00:14:41.149Z"
}
],
comments: [
{
id: 1,
body: "Good post",
created_at: "2015-12-15T00:15:48.643Z",
updated_at: "2015-12-15T00:15:48.643Z",
post_id: 1
},
{
id: 2,
body: "Winter is Coming.",
created_at: "2015-12-15T00:16:21.578Z",
updated_at: "2015-12-15T00:16:21.578Z",
post_id: 1
}
]
},
{
id: 2,
title: "Another post",
body: "Just testing",
authors: [
{
id: 1,
name: "Jon Snow",
email: "jon@thewall.com",
created_at: "2015-12-15T00:14:41.149Z",
updated_at: "2015-12-15T00:14:41.149Z"
},
{
id: 2,
name: "Tyrion Lannister",
email: "theimp@lannister.com",
created_at: "2015-12-15T00:22:56.830Z",
updated_at: "2015-12-15T00:22:56.830Z"
}
],
comments: [
{
id: 3,
body: "Just a comment",
created_at: "2015-12-15T00:23:55.545Z",
updated_at: "2015-12-15T00:23:55.545Z",
post_id: 2
}
]
}
]
```

### JSON

It also generates a json response but always with a root key. The root key **can't be overridden**, and will be automatically defined accordingly to the objects being serialized.
Doesn't follow any specific convention.

### Output example

```json
{
posts: [
{
id: 1,
title: "My new post",
body: "Testing my blog.",
authors: [
{
id: 1,
name: "Jon Snow",
email: "jon@thewall.com",
created_at: "2015-12-15T00:14:41.149Z",
updated_at: "2015-12-15T00:14:41.149Z"
}
],
comments: [
{
id: 1,
body: "Good post",
created_at: "2015-12-15T00:15:48.643Z",
updated_at: "2015-12-15T00:15:48.643Z",
post_id: 1
},
{
id: 2,
body: "Winter is Coming.",
created_at: "2015-12-15T00:16:21.578Z",
updated_at: "2015-12-15T00:16:21.578Z",
post_id: 1
}
]
},
{
id: 2,
title: "Another post",
body: "Just testing",
authors: [
{
id: 1,
name: "Jon Snow",
email: "jon@thewall.com",
created_at: "2015-12-15T00:14:41.149Z",
updated_at: "2015-12-15T00:14:41.149Z"
},
{
id: 2,
name: "Tyrion Lannister",
email: "theimp@lannister.com",
created_at: "2015-12-15T00:22:56.830Z",
updated_at: "2015-12-15T00:22:56.830Z"
}
],
comments: [
{
id: 3,
body: "Just a comment",
created_at: "2015-12-15T00:23:55.545Z",
updated_at: "2015-12-15T00:23:55.545Z",
post_id: 2
}
]
}
]
}
```

### JSON API

This adapter follows **version 1.0** of the format specified in
Expand All @@ -36,6 +172,93 @@ render @posts, include: 'authors,comments'

The format of the `include` option can be either a String composed of a comma-separated list of [relationship paths](http://jsonapi.org/format/#fetching-includes), an Array of Symbols and Hashes, or a mix of both.

### Output example

```json
{
data: [
{
id: "1",
type: "posts",
attributes: {
title: "My new post",
body: "Testing my blog."
},
relationships: {
authors: {
data: [
{
id: 1,
name: "Jon Snow",
email: "jon@thewall.com",
created_at: "2015-12-15T00:14:41.149Z",
updated_at: "2015-12-15T00:14:41.149Z"
}
]
},
comments: {
data: [
{
id: 1,
body: "Good post",
created_at: "2015-12-15T00:15:48.643Z",
updated_at: "2015-12-15T00:15:48.643Z",
post_id: 1
},
{
id: 2,
body: "Winter is Coming.",
created_at: "2015-12-15T00:16:21.578Z",
updated_at: "2015-12-15T00:16:21.578Z",
post_id: 1
}
]
}
}
},
{
id: "2",
type: "posts",
attributes: {
title: "Another post",
body: "Just testing"
},
relationships: {
authors: {
data: [
{
id: 1,
name: "Jon Snow",
email: "jon@thewall.com",
created_at: "2015-12-15T00:14:41.149Z",
updated_at: "2015-12-15T00:14:41.149Z"
},
{
id: 2,
name: "Tyrion Lannister",
email: "theimp@lannister.com",
created_at: "2015-12-15T00:22:56.830Z",
updated_at: "2015-12-15T00:22:56.830Z"
}
]
},
comments: {
data: [
{
id: 3,
body: "Just a comment",
created_at: "2015-12-15T00:23:55.545Z",
updated_at: "2015-12-15T00:23:55.545Z",
post_id: 2
}
]
}
}
}
]
}
```

## Choosing an adapter

If you want to use a specify a default adapter, such as JsonApi, you can change this in an initializer:
Expand Down