Skip to content

Minor bugfix in getAccessToken and added 2 new endpoints: addcomment, deletecomment #9

Merged
merged 4 commits into from Jul 1, 2012
View
50 lib/checkins.js
@@ -49,8 +49,56 @@ module.exports = function(config) {
core.callApi("/checkins/recent", accessToken, params || {}, callback);
}
+ /**
+ * Comment on a checkin-in
+ * @memberof module:node-foursquare/Checkins
+ * @param {String} checkinId The id of the check-in.
+ * @param {Object} [params] An object containing additional parameters. Refer to Foursquare documentation for details
+ * on currently supported parameters.
+ * @param {String} text The text of the comment, up to 200 characters.
+ * @param {String} accessToken The access token provided by Foursquare for the current user.
+ * @param {Function} callback The function to call with results, function({Error} error, {Object} results).
+ * @see https://developer.foursquare.com/docs/checkins/addcomment
+ */
+ function addCommentToCheckin(checkinId, params, accessToken, callback) {
+ logger.debug("ENTERING: Checkins.addCommentToCheckin");
+
+ if(!checkinId) {
+ logger.error("addCommentToCheckin: checkinId is required.");
+ callback(new Error("Checkins.addCommentToCheckin: checkinId is required."));
+ return;
+ }
+
+ core.callApi(path.join("/checkins", checkinId, "addcomment"), accessToken, params || {}, callback, 'POST');
+ }
+
+ /**
+ * Remove a comment from a checkin, if the acting user is the author or the owner of the checkin.
+ * @memberof module:node-foursquare/Checkins
+ * @param {String} checkinId The id of the check-in.
+ * @param {Object} [params] An object containing additional parameters. Refer to Foursquare documentation for details
+ * on currently supported parameters.
+ * @param {String} commentId The id of the comment to remove.
+ * @param {String} accessToken The access token provided by Foursquare for the current user.
+ * @param {Function} callback The function to call with results, function({Error} error, {Object} results).
+ * @see https://developer.foursquare.com/docs/checkins/deletecomment
+ */
+ function deleteCommentFromCheckin(checkinId, params, accessToken, callback) {
+ logger.debug("ENTERING: Checkins.deleteCommentFromCheckin");
+
+ if(!checkinId) {
+ logger.error("deleteCommentFromCheckin: checkinId is required.");
+ callback(new Error("Checkins.deleteCommentFromCheckin: checkinId is required."));
+ return;
+ }
+
+ core.callApi(path.join("/checkins", checkinId, "deletecomment"), accessToken, params || {}, callback, 'POST');
+ }
+
return {
"getCheckin" : getCheckin,
- "getRecentCheckins" : getRecentCheckins
+ "getRecentCheckins" : getRecentCheckins,
+ "addCommentToCheckin": addCommentToCheckin,
+ "deleteCommentFromCheckin": deleteCommentFromCheckin
}
};
View
15 lib/core.js
@@ -16,8 +16,9 @@ module.exports = function(config) {
log4js.configure(config.log4js);
var logger = log4js.getLogger("node-foursquare.core");
- function retrieve(url, callback) {
+ function retrieve(url, callback, method) {
callback = callback || emptyCallback;
+ method = 'POST' === method ? 'POST' : 'GET';
var parsedUrl = urlParser.parse(url, true), request, result = "";
@@ -29,12 +30,13 @@ module.exports = function(config) {
parsedUrl.query = {};
}
var path = parsedUrl.pathname + "?" + qs.stringify(parsedUrl.query);
+
logger.debug("Requesting: " + path);
request = https.request({
"host" : parsedUrl.hostname,
"port" : parsedUrl.port,
"path" : path,
- "method" : "GET",
+ "method" : method,
"headers" : {
"Content-Length": 0
}
@@ -54,9 +56,10 @@ module.exports = function(config) {
request.end();
}
- function invokeApi(url, accessToken, callback) {
+ function invokeApi(url, accessToken, callback, method) {
callback = callback || emptyCallback;
+ method = 'POST' === method ? 'POST' : 'GET';
var parsedUrl = urlParser.parse(url, true);
@@ -84,7 +87,7 @@ module.exports = function(config) {
logger.trace(sys.inspect(result));
callback(null, status, result);
}
- });
+ }, method);
}
function extractData(url, status, result, callback) {
@@ -136,7 +139,7 @@ module.exports = function(config) {
}
}
- function callApi(path, accessToken, params, callback) {
+ function callApi(path, accessToken, params, callback, method) {
var url = config.foursquare.apiUrl + path;
@@ -157,7 +160,7 @@ module.exports = function(config) {
logger.trace("URL: " + url);
invokeApi(url, accessToken, function(error, status, result) {
extractData(url, status, result, callback);
- });
+ }, method);
}
return {
View
11 lib/node-foursquare.js
@@ -89,7 +89,16 @@ module.exports = function(config) {
}
else {
try {
- callback(null, JSON.parse(result).access_token);
+ var resultObj = JSON.parse(result);
+ if(resultObj.error) {
+ callback({message: esultObj.error});
+ }
+ else if(!resultObj.access_token) {
+ callback({message: 'access_token not present, got ' + result});
+ }
+ else {
+ callback(null, resultObj.access_token);
+ }
}
catch(e) {
callback(e);
View
49 test/node-foursquare-test.js
@@ -148,6 +148,7 @@ function TestSuite(accessToken) {
});
};
+
Tests.Users.getFriends = function() {
var test = "Foursquare.Users.getFriends(self)";
Foursquare.Users.getFriends(null, null, accessToken, function (error, data) {
@@ -475,6 +476,54 @@ function TestSuite(accessToken) {
}
});
};
+
+ Tests.Checkins.addCommentToCheckin = function(commentId) {
+ var self = this;
+ var test = "Foursquare.Checkins.addCommentToCheckin(4fc1f24ee4b06f6d23627a50, {text: 'Hello world!'})";
+ Foursquare.Checkins.addCommentToCheckin("4fc1f24ee4b06f6d23627a50", {text: "Hello world!"}, accessToken, function (error, data) {
+ if(error) {
+ reportError(test, error.message);
+ }
+ else {
+ try {
+ logger.trace(sys.inspect(data));
+ assert.ok(data.comment);
+ assert.ok(data.comment.id);
+ assert.equal(data.comment.text, "Hello world!");
+ ok(test);
+
+ // Executes the deleteCommentFromCheckin test by removing the just-added comment
+ self.Tests.Checkins.deleteCommentFromCheckin("4fc1f24ee4b06f6d23627a50", data.comment.id);
+ } catch (error) {
+ reportError(test, error);
+ }
+ }
+ });
+ };
+
+ Tests.Checkins.deleteCommentFromCheckin = function(checkinId, commentId) {
+ // Executes only when called from addCommentToCheckin
+ if(!checkinId || !commentId) return;
+
+ var test = "Foursquare.Checkins.deleteCommentFromCheckin(" + checkinId + ", {commentId: '" + commentId +"'})";
+ logger.debug("Running: " + test);
+ Foursquare.Checkins.deleteCommentFromCheckin(checkinId, {commentId: commentId}, accessToken, function (error, data) {
+ if(error) {
+ reportError(test, error.message);
+ }
+ else {
+ try {
+ logger.trace(sys.inspect(data));
+ assert.ok(data.checkin);
+ assert.equal(data.checkin.id, checkinId);
+ assert.equal(data.checkin.type, "checkin");
+ ok(test);
+ } catch (error) {
+ reportError(test, error);
+ }
+ }
+ });
+ };
Tests.Tips.getTip = function() {
var test = "Foursquare.Tips.getTip(4b5e662a70c603bba7d790b4)";
Something went wrong with that request. Please try again.