Skip to content

Instantly share code, notes, and snippets.

@therabidbanana
Created May 15, 2012 20:09
Show Gist options
  • Save therabidbanana/2704727 to your computer and use it in GitHub Desktop.
Save therabidbanana/2704727 to your computer and use it in GitHub Desktop.
Example request to dtime api
{
"_embedded" : {},
"_links" : {
"dtime:root" : {
"rel" : "dtime:root",
"href" : "http://dev-api.dtime.com/"
},
"self" : {
"rel" : "self",
"href" : "http://dev-api.dtime.com/"
},
"curie" : {
"rel" : "curie",
"href-template" : "http://dev-api.dtime.com/docs/rels/{curie}"
},
"dtime:current_user" : {
"rel" : "dtime:current_user",
"href" : "http://dev-api.dtime.com/user"
},
"dtime:user" : {
"rel" : "dtime:user",
"href-template" : "http://dev-api.dtime.com/users/{user_id}"
},
// ...snip...
"dtime:developers:documentation" : {
"rel" : "dtime:developers:documentation",
"href" : "http://dev-api.dtime.com/docs"
}
}
}
// Relies on jquery
var endpoint = "http://dev-api.dtime.com/";
var home = null;
function load_dtime(rel){
if(home != null){
return $.when(home)
}
else{
home = $.ajax({
url: endpoint
})
return home;
}
}
function find_link(rel, data){
for(link in data._links){
if(link == rel){
return data._links[rel]
}
}
}
function find_href(rel, data, tmpl){
var link, template;
link = find_link(rel, data);
template = uritemplate(link["href"]);
}
// Use jquery promise to make sure api is only fetched once,
// and all calls relying on api are delayed until ready.
load_dtime().done(function(data){
// One without a template
$('body').append("<div> Link is: " +JSON.stringify(find_link("dtime:current_user", data))+"</div>");
$('body').append("<div> Href is: " +find_href("dtime:current_user", data, {user_id: 'abc'})+"</div>");
// One with a template
$('body').append("<div> Link is: " +JSON.stringify(find_link("dtime:user", data))+"</div>");
$('body').append("<div> Link is is: " +find_href("dtime:user", data, {user_id: 'abc'})+"</div>");
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment