Created
September 30, 2010 21:53
-
-
Save rmanalan/605393 to your computer and use it in GitHub Desktop.
wc-rest.next.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* A more dynamic API for WebCenter | |
* Rich Manalang / @rmanalan | |
* | |
* This is an attempt to make a better Javascript wrapper for the WebCenter REST API. | |
* Goals: | |
* - Dynamic object creation | |
* - Callbacks receive proper objects from prior call | |
* - Concurrent request support | |
* - Beautiful API | |
* | |
* Patterns I'm hoping to support: | |
* | |
* // fetches the resourceIndex and constructs all of the available services as objects | |
* wc.init({options},callback) | |
* | |
* // fetches the current user, then gets that user's message board | |
* wc.people.get(function(currUser){ | |
* currUser.messageBoard.get(function(msgBoard){ | |
* console.log(msgBoard) | |
* }); | |
* }) | |
* | |
* // once an item is fetched the first time, the data is cached in the original object in a nested fashion | |
* wc.people.messageBoard.items | |
* wc.people.addresses | |
* wc.people.displayName | |
* ...etc | |
* | |
* // we'll also support other verbs | |
* wc.people.messageBoard.create({msg},callback) | |
* wc.people.status.update({msg},callback) | |
* wc.forums.items[0].create({forumtopic},callback) | |
* wc.feedback.items[0].delete(callback) | |
*/ | |
var wc = function() { | |
function extractParams(p) { | |
var params = {}; | |
var re = /\{([^\}]*)\}/g; | |
while ((match = re.exec(p)) !== null) { | |
params[match[1]] = ''; | |
} | |
return params; | |
} | |
function makeObjects(d, obj) { | |
// if obj not provided, the root wc obj will be used | |
if (!obj) var obj = wc; | |
for (var j in d.links) { | |
var tmpObj = {}; | |
var svcName; | |
for (var i in d.links[j]) { | |
var val = d.links[j][i]; | |
val = /^urn/.test(val) ? val.split(':').splice( - 1)[0] : val; | |
if (i === 'resourceType') { | |
svcName = val; | |
obj[svcName] = {}; | |
} else { | |
if (i === 'template') { | |
tmpObj[i] = { | |
url: val, | |
params: extractParams(val) | |
} | |
} else { | |
tmpObj[i] = val; | |
} | |
tmpObj.get = getService; | |
} | |
} | |
obj[svcName] = tmpObj; | |
} | |
return obj; | |
} | |
function getService(opts, callback) { | |
if (typeof opts === 'function' && typeof callback === 'undefined') { | |
callback = opts; | |
} | |
var self = this; | |
if (self.template && typeof opts === 'object') { | |
$.extend(self.template.params, opts); | |
} | |
var url = self.template.url.replace(/\{[^\}]*\}/g, function(key) { | |
return self.template.params[key.replace(/[\{\}]/g, '')]; | |
}); | |
$.getJSON(url, function(d) { | |
// cache results in original object | |
$.extend(self, d); | |
makeObjects(self, self); | |
if (callback) callback(self); | |
}); | |
} | |
return { | |
init: function(options,callback) { | |
$.getJSON(options.resourceIndexURL, function(d) { | |
callback(makeObjects(d)); | |
}); | |
} | |
} | |
} (); | |
/* | |
* Sample usage | |
*/ | |
var options = { | |
resourceIndexURL: 'http://webcenter.us.oracle.com/rest/api/resourceIndex', | |
}; | |
var loadCurrUser = function(){ | |
wc.people.get(function(currUser){ | |
currUser.messageBoard.get() | |
}) | |
}; | |
var loadSpaces = function(){ wc.spaces.get() }; | |
var preLoadData = function(){ | |
// the following will load concurrently | |
loadCurrUser(); | |
loadSpaces(); | |
} | |
wc.init(options,preLoadData); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* A more dynamic API for WebCenter | |
* Rich Manalang / @rmanalan | |
* | |
* This is an attempt to make a better Javascript wrapper for the WebCenter REST API. | |
* Goals: | |
* - Dynamic object creation | |
* - Callbacks receive proper objects from prior call | |
* - Concurrent request support | |
* - Beautiful API | |
* | |
* Patterns I'm hoping to support: | |
* | |
* // loads resourceIndex then creates objects with the links in the resource index | |
* $w() | |
* | |
* // metadata cache/store and helper methods | |
* $w.resourceIndex | |
* $w.<global helper methods for webCenter> | |
* | |
* // loads current user (people service) | |
* $w().people() | |
* | |
* // callbacks to handle dependent calls | |
* $w().people(opts,function(currUser){ | |
* currUser.messageBoard(opts,function(msgBoard){ | |
* console.log(msgBoard) | |
* }) | |
* }) | |
* | |
* // properties of currUser available after people() | |
* $w().people.emails | |
* | |
* // handle basic crud opts | |
* $w().spaces(opts) // read/find | |
* $w().people(function(currUser){ // read/find | |
* currUser.status.update({msg}); update | |
* currUser.messageBoard.create({msg}); //create | |
* }) | |
* $w().feedback.delete(opts) // delete | |
* | |
*/ | |
var $w = function() { | |
var $w = function() { | |
return new $w.init; | |
} | |
$w.prototype = { | |
// add all of the methods/props you want accessible as $w().method() or $w().prop here: | |
init: function() { | |
console.log('init called'); | |
}, | |
length: 0 | |
} | |
$.extend($w, { | |
// add all of the methods/props you want accessible as $w.method() or $w.prop here: | |
sayHello: function() { | |
console.log('hello'); | |
} | |
}) | |
return $w; | |
} (); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment