HTTPS SSH

HyperBek

An Objective-C library for working with the JSON variant of Hypertext Application Language (HAL).

Getting started

HyperBek simply accepts an NSDictionary, so you can use any HTTP client/JSON decoder you like. It supports embedded resources and links (absolute/relative, templated and CURIE).

Say you have an NSDictionary decoded from the following JSON:

{
    "_links": {
        "self": {
            "href": "/users/mike"
        },
        "curies": [
            {
                "name": "ht",
                "href": "http://haltalk.herokuapp.com/rels/{rel}",
                "templated": true
            }
        ],
        "ht:posts": {
            "href": "/users/mike/posts"
        }
    },
    "username": "mike",
    "bio": null,
    "real_name": "Mike Kelly"
}

You can extract state and links from it like so:

NSURL *baseURL = [NSURL URLWithString:@"http://haltalk.herokuapp.com"];
YBHALResource *resource = [jsonDictionary HALResourceWithBaseURL:baseURL];
NSLog(@"Name: %@", resource[@"real_name"]);
YBHALLink *posts = [resource linkForRelation:@"http://haltalk.herokuapp.com/rels/posts"];
NSLog(@"Posts at: %@", posts.URL);

HyperBek will automatically resolve CURIE links and relative URLs against the supplied base URL (which should be the URL from which the resource was downloaded).

You can also use templated links. For example:

{
    "_links": {
        "document": {
            "href": "/documents/{id}",
            "templated": true
        }
    }
}

Extract the link, then resolve the URL against a dictionary of values:

YBHALResource *resource = ...
YBHALLink *link = [resource linkForRelation:@"document"];
NSURL *documentURL = [link URLWithVariables:@{@"id": @"99"}];

Finally, HyperBek lets you extract embedded resources:

{
    "_embedded": {
        "project": {
            "name": "HyperBek"
        }
    }
}

YBHALResource *resource = ...
YBHALResource *project = [resource resourceForRelation:@"project"];
NSLog(@"Project name: %@", project[@"name"]);

Installing

CocoaPods is the easiest way to use HyperBek. Note that you may need to specify the iOS version you're targeting.

platform :ios, '6.0'
pod 'HyperBek'

If you'd rather not use CocoaPods, just copy the directory HyperBek/HyperBek into your project.

Import HyperBek in any places you'd like to use it:

#import "HyperBek.h"