Skip to content

API Blueprint

Kyle Fuller edited this page Feb 3, 2015 · 2 revisions

This document describes how to add and use the API Blueprint adapter in the Representor.

Installation

Installation with CocoaPods is recommended using CocoaPods 0.36.

pod 'Representor'

Sub-projects

Alternatively, you can clone Representor via git or as a submodule and include Representor.xcodeproj inside your project and add Representor.framework as a target dependency.

Blueprint

The next step is to include your blueprint into your project, for this you will need to grab a JSON representation of the blueprint.

We're going to use a command line tool called Snowcrash to do this.

NOTE: This depends on the Homebrew package manager.

$ brew install --HEAD https://raw.github.com/apiaryio/snowcrash/master/tools/homebrew/snowcrash.rb

Now we have Snowcrash installed, we can generate the JSON representation of our blueprint (blueprint.md):

$ snowcrash -o blueprint.ast.json -f json blueprint.md

OK.

This will output a blueprint.ast.json which we will need to add to our Xcode project.

Using the Representor

Now we're ready to access our blueprint at runtime. Given an action called "Create" in a resource named "Polls", which looks something like this in our blueprint:

## Polls [/polls]
### Create [POST]

We can now pull that out at run-time using the following:

let blueprint = Blueprint(named: "blueprint.ast.json", bundle: nil)

if let transition = blueprint?.transition("Polls", action: "Create") {
  println("We can perform this action via the HTTP method: \(transition.method) with the URI: \(transition.uri).")
}

We can use Alamofire to perform this action as follows:

let baseURL = NSURL(string: "https://pollsapi.herokuapp.com/")
request(baseURL, transition, parameters: nil, attributes: nil).responseJSON {
  (_, _, json, _) in

  println(json)
}
Clone this wiki locally