A nice short post today. I'm building an API Blueprint for a project I'm working on. If you don't know what API blueprint is I would suggest you checkout the API Blueprint website. I would also recommend checking out a post by Phil Sturgeon on API  Blueprint, as it's what got me interested in using it and was helpful in helping me get to grips with it, see the post HTTP Documentation with API Blueprint.

In the past I've been writing my own API documents, hard coded with all the styling. They look great and are easy to understand, but from a building point of view they can be time consuming and can be a pain when you're scrolling through lines and lines of HTML to find what you're looking for. Step in API Blueprint, it's a document type which allows you describe your API. Using a tool such as Aglio you can quickly and easily convert your document into a pretty HTML document.

Anyway, with the introduction out of the way, let's get down to the problem. Adding an array of attributes to an existing attribute. Here's how you do it; Notice the 'following' attribute has two child objects.

+ Attributes
        - full_name: John Doe (string, optional) - The users updated full name
        - email: test@test.com (string, optional) - The users updated email address
        - password: mypass123 (string, optional) - The users updated password
        - facebook_id: 11111 (string, optional) - The users facebook id
        - facebook_token: 11111 (string, optional) - The users facebook token
        - twitter_id: 22222 (string, optional) - The users twitter id
        - twitter_token: 22222 (string, optional) - The users twitter token
        - latitude: 0.87 (string, optional) - The users updated latitude
        - longitude: 1.33 (string, optional) - The users updated longitude
        - following (array)
            - (object)
                - artist_id: 58 (number, required) - The artist id
                - following: true (boolean, required) - Is the user following this artist?
            - (object)
                - artist_id: 58 (number, required) - The artist id
                - following: false (boolean, required) - Is the user following this artist?

In my API we're using JSON, so the resulting JSON from above would look like this;

{
  "full_name": "John Doe",
  "email": "test@test.com",
  "password": "mypass123",
  "facebook_id": "11111",
  "facebook_token": "11111",
  "twitter_id": "22222",
  "twitter_token": "22222",
  "latitude": "0.87",
  "longitude": "1.33",
  "following": [
    {
      "artist_id": 58,
      "following": true
    },
    {
      "artist_id": 58,
      "following": false
    }
  ]
}

Don't forget, if you're working with duplicate objects to take a look at Data Structures.

I may when I get time put together a tutorial on using API Blueprint, but in the meantime I hope this would helpful.

Comments

There are no comments, why not be the first?

Submit a reply

Your e-mail address will not be published, all fields are required.

SIGNUP TO THE NEWSLETTER

Get useful tips on web application development and software engineering.