Skip to content

Instantly share code, notes, and snippets.

@ericelliott
Last active December 10, 2015 14:18
Show Gist options
  • Save ericelliott/4446253 to your computer and use it in GitHub Desktop.
Save ericelliott/4446253 to your computer and use it in GitHub Desktop.

Siren Samples

Siren is a hypermedia type designed for entity representation in JSON. These are some ideas on how you might represent tout collections using Siren.

window.example.pendingToutsSiren = {
  "class": [ "collection" ],
  "properties": {
    "pagination": {
      "offset": 0,
      "limit": 10
    }
  },
  "entities": [
    {
      "class": ["item", "tout", "pending"],
      "rel": ["item"],
      "properties": {
        "uid": "fk9abg",
        "text": "It's safe to say that this was the best moment today. LOL #KellyandMichael",
        "parsed_text": {
          "hashtags": [
            {
              "text": "#KellyandMichael",
              "name": "KellyandMichael",
              "indices": [
                58,
                74
              ],
              "uid": "kellyandmichael",
              "type": "hashtags"
            }
          ],
          "mentions": [],
          "urls": []
        },
        "views_count": 1492,
        "likes_count": 22,
        "total_likes_count": 22,
        "replies_count": 0,
        "retouts_count": 6,
        "recorded_at": "2012-12-20T22:14:13Z",
        "created_at": "2012-12-20T22:14:13Z",
        "privacy": "public",
        "user": {
          "username": "KellyandMichael",
          "uid": "kellyandmichael",
          "fullname": "LIVE! with Kelly and Michael",
          "verified": true,
          "location": "New York",
          "bio": "The official “LIVE! with Kelly and Michael” Tout page! Be the first to know the latest from the show. Watch “LIVE! with Kelly and Michael” weekday mornings with Kelly Ripa and Michael Strahan.\r\n\r\n",
          "friendly_name": "LIVE! with Kelly and Michael",
          "touts_count": 544,
          "followers_count": 33113,
          "friends_count": 5,
          "last_tout_recorded_at": "2012-12-25T16:18:24Z",
          "created_at": "2012-03-27T16:32:02Z",
          "avatar": {
            "profile": {
              "http_url": "http://avatars.tout.com/u/20b3d997063c8b8ad60b4aa63c8ffbee/profile/LIVE_Kelly_and_Michael_best_graphic_psd_copy.jpg"
            },
            "large": {
              "http_url": "http://avatars.tout.com/u/20b3d997063c8b8ad60b4aa63c8ffbee/large/LIVE_Kelly_and_Michael_best_graphic_psd_copy.jpg"
            },
            "medium": {
              "http_url": "http://avatars.tout.com/u/20b3d997063c8b8ad60b4aa63c8ffbee/medium/LIVE_Kelly_and_Michael_best_graphic_psd_copy.jpg"
            },
            "small": {
              "http_url": "http://avatars.tout.com/u/20b3d997063c8b8ad60b4aa63c8ffbee/small/LIVE_Kelly_and_Michael_best_graphic_psd_copy.jpg"
            }
          },
          "following": false,
          "followed_by": false
        },
        "image": {
          "poster": {
            "width": 640,
            "height": 360,
            "http_url": "http://thumbnails.tout.com/dry/db6ca28223acc087/poster-13.jpg"
          },
          "thumbnail": {
            "width": 150,
            "height": 84,
            "http_url": "http://thumbnails.tout.com/dry/db6ca28223acc087/thumbs-13.jpg"
          }
        },
        "video": {
          "type": "native",
          "duration": 16,
          "mp4": {
            "width": 640,
            "height": 360,
            "http_url": "http://videos.tout.com/dry/mp4/db6ca28223acc087.mp4"
          }
        },
        "liked": false
      },
      "links": [
        { "rel": "self", "href": "http://api.tout.com/api/v1/touts/fk9abg" }
      ]
    },    
    {
      "class": ["item", "tout", "pending"],
      "rel": ["item"],
      "properties": {
        "uid": "nim3x1",
        "text": "LOL everyone is soooooo excited #Backstage today! VIDEO:",
        "parsed_text": {
          "hashtags": [
            {
              "text": "#Backstage",
              "name": "Backstage",
              "indices": [
                32,
                42
              ],
              "uid": "backstage",
              "type": "hashtags"
            }
          ],
          "mentions": [
            
          ],
          "urls": [
            
          ]
        },
        "views_count": 442,
        "likes_count": 5,
        "total_likes_count": 5,
        "replies_count": 0,
        "retouts_count": 0,
        "recorded_at": "2012-12-20T14:01:11Z",
        "created_at": "2012-12-20T14:03:22Z",
        "privacy": "public",
        "user": {
          "username": "KellyandMichael",
          "uid": "kellyandmichael",
          "fullname": "LIVE! with Kelly and Michael",
          "verified": true,
          "location": "New York",
          "bio": "The official \u201cLIVE! with Kelly and Michael\u201d Tout page! Be the first to know the latest from the show. Watch \u201cLIVE! with Kelly and Michael\u201d weekday mornings with Kelly Ripa and Michael Strahan.\r\n\r\n",
          "friendly_name": "LIVE! with Kelly and Michael",
          "touts_count": 542,
          "followers_count": 32603,
          "friends_count": 5,
          "last_tout_recorded_at": "2012-12-20T22:14:13Z",
          "created_at": "2012-03-27T16:32:02Z",
          "avatar": {
            "profile": {
              "http_url": "http:\/\/avatars.tout.com\/u\/20b3d997063c8b8ad60b4aa63c8ffbee\/profile\/LIVE_Kelly_and_Michael_best_graphic_psd_copy.jpg"
            },
            "large": {
              "http_url": "http:\/\/avatars.tout.com\/u\/20b3d997063c8b8ad60b4aa63c8ffbee\/large\/LIVE_Kelly_and_Michael_best_graphic_psd_copy.jpg"
            },
            "medium": {
              "http_url": "http:\/\/avatars.tout.com\/u\/20b3d997063c8b8ad60b4aa63c8ffbee\/medium\/LIVE_Kelly_and_Michael_best_graphic_psd_copy.jpg"
            },
            "small": {
              "http_url": "http:\/\/avatars.tout.com\/u\/20b3d997063c8b8ad60b4aa63c8ffbee\/small\/LIVE_Kelly_and_Michael_best_graphic_psd_copy.jpg"
            }
          },
          "following": false,
          "followed_by": false
        },
        "image": {
          "poster": {
            "width": 568,
            "height": 320,
            "http_url": "http:\/\/thumbnails.tout.com\/dry\/ab6f39afb719e143\/poster-3.jpg"
          },
          "thumbnail": {
            "width": 150,
            "height": 84,
            "http_url": "http:\/\/thumbnails.tout.com\/dry\/ab6f39afb719e143\/thumbs-3.jpg"
          }
        },
        "video": {
          "type": "native",
          "duration": 16,
          "mp4": {
            "width": 568,
            "height": 320,
            "http_url": "http:\/\/videos.tout.com\/dry\/mp4\/ab6f39afb719e143.mp4"
          }
        },
        "liked": false
      },
      "links": [
        { "rel": "self", "href": "http://api.tout.com/api/v1/touts/fk9abg" }
      ]        
    }
  ],
  "actions": [
    {
      "name": "add-tout",
      "title": "Add Tout",
      "method": "POST",
      "href": "https://api.tout.com/api/v1/pendingtouts/",
      "type": "application/x-www-form-urlencoded",
      "fields": [
        { "name": "uid", "type": "string" }
      ]
    }
  ],
  "links": [
    { "rel": [ "self" ], "href": "https://api.tout.com/api/v1/pendingtouts/" },
    { "rel": [ "next" ], "href": "https://api.tout.com/api/v1/pendingtouts/?offset=10&limit=10" }
  ]
};
window.example.sirenError = {
  "class": "error",
  "properties": {
    "message":"User not found",
    "status":"not_found",
    "code": 404
  },
  "links": [
    { "rel": [ "self" ], "href": "https://api.tout.com/api/v1/errors/chbhb4jhy00005jj6khlkwhhn" }
  ]
};

Siren questions:

  • Internal linking by ID (to avoid repeating embedded objects)?
  • Representing multiple errors (validation)?
  • Existing API examples?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment