Permalink
Browse files

comments

  • Loading branch information...
1 parent 192bb0f commit 481479dbd01fb8023a9c5f564ef087b1c0e057d9 @tmarshall committed Apr 24, 2012
Showing with 20 additions and 0 deletions.
  1. +20 −0 main.js
View
@@ -2,6 +2,7 @@ var
util = require('util'),
request = require('request'),
querystring = require('querystring'),
+ // types of requests which shouldn't use 'id' as keys, in the user data object
nonIdBasedConnection = [ 'permissions', 'picture', 'user' ],
connections,
preQueryString = /.*\?/,
@@ -23,6 +24,9 @@ function prepOptions(options) {
}
function connection(kind) {
+ // can be:
+ // (id, accessToken, callback)
+ // (id, accessToken, options, callback)
return function connfunc(id, accessToken, a, b) {
var
lex = this,
@@ -31,33 +35,42 @@ function connection(kind) {
options, callback,
pagingFunc
+ // figuring out the positions of 'a' and 'b'
+ // 'a' and 'b' are either options (a) and callback (b) or just callback (a)
a = isUser ? arguments[0] : a,
b = isUser ? arguments[1] : b
+ // figuring out the actual options and callback to be used
options = a && toString.call(a) === '[object Object]' ? prepOptions(a) : false,
callback = typeof a === 'function' ? a : b
+ // if this is a call from a User instance then we already have the id and acces token, which shouldn't be passed in
if (this instanceof User) id = this.id, accessToken = this.accessToken, this.data[kind] = this.data[kind] || (isNonIdBased ? null : {})
+ // break everything if what should be a function is, in fact, not quite a function
if (typeof callback != 'function') throw 'No callback given for #' + kind + '()'
request('https://graph.facebook.com/' + id + (kind === 'user' ? '' : '/' + kind) + '?access_token=' + accessToken + (options === false ? '' : '&' + querystring.stringify(options)), function(err, res, body) {
var
i, l, key,
paging = {}
+ // not sure if this try catch is needed
try {
body = JSON.parse(body)
} catch(e) {}
+ // if in a User instance then append the data
if (isUser) {
if (isNonIdBased) lex.data[kind] = typeof body == 'string' ? body : Array.isArray(body.data) ? body.data[0] : body.data ? body.data : body
else if (Array.isArray(body.data)) for (i = 0, l = body.data.length; i < l; i++) lex.data[kind][body.data[i].id] = body.data[i]
}
+ // facebook may have given us 'next' and 'previous' paging paths
if (body.paging) {
pagingFunc = function(callback) {
var pagingOptions = querystring.parse(body.paging[this].replace(preQueryString, ''))
+ // they gave us the access token, which we have in the User instance
delete pagingOptions.access_token
connfunc.apply(lex, (isUser ? [ ] : [ id, accessToken ]).concat([ pagingOptions, callback ]))
}
@@ -72,6 +85,8 @@ function connection(kind) {
}
}
+// not all of these have been tested yet
+// also may be missing some
;(connections = [
'accounts', // nada
'achievements', // developers.facebook.com/docs/achievements
@@ -133,6 +148,7 @@ function User(id, accessToken) {
}
util.inherits(User, graph)
+// function to get multiple 'connections' asynchronously
User.prototype.get = function(toGet, a, b) {
var
lex = this,
@@ -151,13 +167,17 @@ User.prototype.get = function(toGet, a, b) {
if (typeof callback != 'function') throw 'No callback given for #get()'
for (; i < l; i++) {
+ // although we throw if trying to 'get' something that we haven't defined,
+ // the previous requests will have still been sent out (but callback will not be reached)
if (!~connections.indexOf(toGet[i])) throw toGet[i] + ' is not a valid \'connection\' type'
this[toGet[i]].apply(lex, (options ? [ options ] : [ ]).concat([ getCallback ]))
}
}
+// can access User two ways
graph.prototype.User = User
graph.User = User
+// boom
module.exports = graph

0 comments on commit 481479d

Please sign in to comment.