Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit of refactoring.

  • Loading branch information...
commit a0d144cdd4b749899249becf40e0f5f8d7014409 1 parent a68f422
@clintandrewhall authored
View
3  .gitignore
@@ -1 +1,2 @@
-src/key.js
+test/config.js
+node_modules
View
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2011 Clint Andrew Hall
+Derived from: https://github.com/yikulju/Foursquare-on-node
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
View
3  index.js
@@ -0,0 +1,3 @@
+// This file is just added for convenience so this repository can be
+// directly checked out into a project's deps folder
+module.exports = require('./lib/node-foursquare');
View
269 lib/node-foursquare.js
@@ -0,0 +1,269 @@
+var QueryString = require('querystring'),
+ HTTPS = require('https'),
+ URL = require('url'),
+ defaultConfig = {
+ "accessTokenUrl" : "https://foursquare.com/oauth2/access_token",
+ "apiUrl" : "https://api.foursquare.com/v2"
+ };
+
+var Foursquare = function(config) {
+ config = config || defaultConfig;
+
+ var emptyCallback = function() { };
+
+ if(!config.accessTokenUrl || !config.apiUrl) {
+ throw new TypeError("Supplied configuration is invalid.");
+ }
+
+ function retrieve(url, callback) {
+ callback = callback || emptyCallback;
+
+ var parsedUrl = URL.parse(url, true), request, result = "";
+
+ if (parsedUrl.protocol == "https:" && !parsedUrl.port) {
+ parsedUrl.port = 443;
+ }
+
+ if (parsedUrl.query === undefined) {
+ parsedUrl.query = {};
+ }
+
+ request = HTTPS.request({
+ "host" : parsedUrl.hostname,
+ "port" : parsedUrl.port,
+ "path" : parsedUrl.pathname + "?" + QueryString.stringify(parsedUrl.query),
+ "method" : "POST",
+ "headers" : {
+ "Content-Length": 0
+ }
+ }, function(res) {
+ res.on("data", function(chunk) {
+ result += chunk;
+ });
+ res.on("error", function(error) {
+ callback(error);
+ });
+ res.on("end", function () {
+ callback(null, res.statusCode, result);
+ });
+ });
+
+ request.end();
+ }
+
+ function invokeAPI(url, accessToken, callback) {
+
+ var parsedUrl = URL.parse(url, true);
+
+ callback = callback || emptyCallback;
+
+ if(accessToken != null) {
+ parsedUrl.query["oauth_token"] = accessToken;
+ }
+
+ retrieve(url.format(parsedUrl),
+ function(error, status, result) {
+ if(error) {
+ callback(error);
+ }
+ else {
+ callback(null, status, result);
+ }
+ });
+ }
+
+ function extractData(status, result, field, callback) {
+ var json;
+ callback = callback || emptyCallback;
+
+ if (status !== undefined && result !== undefined) {
+ try {
+ json = JSON.parse(result);
+ }
+ catch(e) {
+ callback(e);
+ return;
+ }
+
+ // 200
+ if (json.meta.code === 200) {
+ if (json.response !== undefined && json.response[field] !== undefined) {
+ callback(null, json.response[field]);
+ }
+ else {
+ callback(null, {});
+ }
+ }
+
+ // 400
+ if (json.meta.code === 400) {
+ callback(new Error("Remote site unreachable."));
+ }
+ else {
+ callback(new Error("Code " + json.meta.code + " received."));
+ }
+ }
+ }
+
+ exports.getAccessToken = function (params, callback) {
+ params = params || [];
+ params["grant_type"] = "authorization_code";
+
+ retrieve(config.accessTokenUrl + "?" + QueryString.stringify(params),
+ function(error, status, result) {
+ if(error) {
+ callback(error);
+ }
+ else {
+ try {
+ var obj = JSON.parse(result);
+ callback(null, obj.access_token);
+ }
+ catch(e) {
+ callback(e);
+ }
+ }
+ });
+ };
+
+ exports.getUserBadges = function (user_id, accessToken, callback) {
+
+ user_id = user_id || "self";
+
+ var url = config.apiUrl + "/users/" + user_id + "/badges";
+
+ invokeAPI(url, accessToken, function (error, status, result) {
+ extractData(status, result, "badges", callback);
+ });
+ };
+
+ /**
+ * Returns a list of tips near the area specified.
+ *
+ * http://developer.foursquare.com/docs/tips/search.html
+ */
+ exports.searchTips = function (query, accessToken, callback) {
+
+ var url = config.apiUrl + "/tips/search";
+ url += "?" + QueryString.stringify(query);
+
+ invokeAPI(url, accessToken, function (error, status, result) {
+ extractData(status, result, "tips", callback);
+ });
+
+ };
+
+
+ exports.getRecentCheckins = function (query, accessToken, callback) {
+
+ var url = config.apiUrl + "/checkins/recent";
+ url += "?" + QueryString.stringify(query);
+
+ invokeAPI(url, accessToken, function (error, status, result) {
+ extractData(status, result, "recent", callback);
+ });
+
+ };
+
+ exports.searchVenues = function (query, accessToken, callback) {
+
+ var url = config.apiUrl + "/venues/search";
+ url += "?" + QueryString.stringify(query);
+
+ invokeAPI(url, accessToken, function (error, status, result) {
+ extractData(status, result, "groups", callback);
+ });
+ };
+
+ exports.searchUsers = function (query, accessToken, callback) {
+
+ var url = config.apiUrl + "/users/search";
+ url += "?" + QueryString.stringify(query);
+
+ invokeAPI(url, accessToken, function (error, status, result) {
+ extractData(status, result, "results", callback);
+ });
+ };
+
+
+ exports.getSettings = function (accessToken, callback) {
+
+ var url = config.apiUrl + "/settings/all";
+
+ invokeAPI(url, accessToken, function (status, result) {
+ extractData(status, result, "settings", callback);
+ });
+ };
+
+ exports.getPhoto = function (photo_id, accessToken, callback) {
+
+ var url = config.apiUrl + "/photos/" + photo_id;
+
+ invokeAPI(url, accessToken, function (error, status, result) {
+ extractData(status, result, "photo", callback);
+ });
+ };
+
+ exports.getTip = function (tip_id, accessToken, callback) {
+
+ var url = config.apiUrl + "/tips/" + tip_id;
+
+ invokeAPI(url, accessToken, function (error, status, result) {
+ extractData(status, result, "tip", callback);
+ });
+ };
+
+ exports.getCheckin = function (checkin_id, accessToken, callback) {
+
+ var url = config.apiUrl + "/checkins/" + checkin_id;
+
+ invokeAPI(url, accessToken, function (error, status, result) {
+ extractData(status, result, "checkin", callback);
+ });
+ };
+
+
+ exports.getVenue = function (venue_id, accessToken, callback) {
+
+ var url = config.apiUrl + "/venues/" + venue_id;
+
+ invokeAPI(url, accessToken, function (error, status, result) {
+ extractData(status, result, "venue", callback);
+ });
+ };
+
+ exports.getVenueAspect = function (venue_id, accessToken, aspect, params, callback) {
+
+ var url = config.apiUrl + "/venues/" + venue_id + '/' + aspect;
+ url += "?" + QueryString.stringify(params || {});
+
+ invokeAPI(url, accessToken, function (error, status, result) {
+ extractData(status, result, aspect, callback);
+ });
+ };
+
+ exports.getUser = function (user_id, accessToken, callback) {
+
+ var url = config.apiUrl + "/users/" + user_id;
+
+ invokeAPI(url, accessToken, function (error, status, result) {
+ extractData(status, result, "user", callback);
+ });
+ };
+
+ exports.getUserCheckins = function (user_id, accessToken, params, callback) {
+
+ var url = config.apiUrl + "/users/" + user_id + "/checkins",
+ optional = QueryString.stringify(params || {});
+
+ if (optional !== "") {
+ url += "?" + optional;
+ }
+
+ invokeAPI(url, accessToken, function (error, status, result) {
+ extractData(status, result, "checkins", callback);
+ });
+ }
+};
+
+exports.Foursquare = Foursquare;
View
19 package.json
@@ -0,0 +1,19 @@
+{
+ "name": "node-foursquare",
+ "description": "Fault-tolerant Foursquare API v2 wrapper for Node JS.",
+ "version": "0.0.1",
+ "main": "foursquare.js",
+ "keywords": ["node-foursquare", "foursquare", "4sq"],
+ "repository" : {
+ "type" : "git",
+ "url" : "https://github.com/clintandrewhall/node-foursquare"
+ },
+ "bugs" : {
+ "web" : "https://github.com/clintandrewhall/node-foursquare/issues"
+ },
+ "licenses" : [
+ { "type" : "MIT",
+ "url" : "http://github.com/clintandrewhall/node-foursquare/raw/master/LICENSE"
+ }
+ ]
+}
View
17 readme.md
@@ -1,13 +1,16 @@
-Foursquare on node
+node-foursquare
==================
-Foursquare API wrapper for Node JS.
-
-Install
--------
-
- `npm install foursquareonnode`
+Fault-tolerant Foursquare API wrapper for Node JS.
Use
---
+
+
+Notes
+-----
+
+This project is a refactoring and enhancement of:
+
+https://github.com/yikulju/Foursquare-on-node
View
1  run-tests.sh
@@ -0,0 +1 @@
+supervisor node ./test/node-foursquare-test.js
View
264 src/foursquare.js
@@ -1,264 +0,0 @@
-var QUERYSTRING = require('querystring'),
- HTTPS = require('https'),
- URL = require('url');
-
-
-BASESITE = "https://foursquare.com";
-ACCESS_TOKEN_URL = "/oauth2/access_token";
-API_URL = "https://api.foursquare.com/v2";
-
-
-function getRequest(url, access_token, callback) {
-
- var parsedUrl = URL.parse(url, true ),
- request, result = "";
-
- if (parsedUrl.protocol == "https:" && !parsedUrl.port) {
- parsedUrl.port= 443;
- }
-
- if( parsedUrl.query === undefined) {
- parsedUrl.query= {};
- }
-
- if (access_token != null) {
- parsedUrl.query["oauth_token"]= access_token;
- }
-
- request = HTTPS.request({
- host: parsedUrl.hostname,
- port: parsedUrl.port,
- path: parsedUrl.pathname + "?" + QUERYSTRING.stringify(parsedUrl.query),
- method: "GET"
- }, function(res) {
- res.on('data', function(chunk) {
- result+= chunk;
- });
-
- res.on("end", function () {
- callback(res.statusCode, result);
- });
- });
-
- request.end();
-}
-
-function extractData(status, result, field, success_handler, error_handler) {
-
- var json;
-
- if (status !== undefined && result !== undefined) {
-
- json = JSON.parse(result);
-
- // 200
- if (json.meta.code === 200) {
- if (json.response !== undefined && json.response[field] !== undefined) {
-
- if (success_handler !== undefined) {
- success_handler(json.response[field]);
- }
- }
- }
-
- // 400
- if (json.meta.code === 400) {
- if (error_handler !== undefined) {
- error_handler(json.meta);
- }
- }
- }
-}
-
-
-exports.getAccessToken = function (params, successHandler) {
-
- // adding extra params
- params["grant_type"] = "authorization_code";
-
- var url = BASESITE + ACCESS_TOKEN_URL + "?" + QUERYSTRING.stringify(params);
-
-
- var parsedUrl = URL.parse(url, true ),
- request, result = "";
-
- if (parsedUrl.protocol == "https:" && !parsedUrl.port) {
- parsedUrl.port= 443;
- }
-
- if( parsedUrl.query === undefined) {
- parsedUrl.query= {};
- }
-
- request = HTTPS.request({
- host: parsedUrl.hostname,
- port: parsedUrl.port,
- path: parsedUrl.pathname + "?" + QUERYSTRING.stringify(parsedUrl.query),
- method: "POST",
- headers: {
- 'Content-Length': 0
- }
- }, function(res) {
- res.on('data', function(chunk) {
- result+= chunk;
- });
-
- res.on("end", function () {
-
- var json;
-
- if (successHandler !== undefined && result !== undefined) {
-
- json = JSON.parse(result);
- successHandler(json.access_token);
- }
- });
- });
-
- request.end();
-};
-
-
-exports.getUserBadges = function (user_id, access_token, success_handler, error_handler) {
-
- user_id = user_id || "self";
-
- var url = API_URL + "/users/" + user_id + "/badges";
-
- getRequest(url, access_token, function (status, result) {
- extractData(status, result, "badges", success_handler, error_handler);
- });
-};
-
-/**
- * Returns a list of tips near the area specified.
- *
- * http://developer.foursquare.com/docs/tips/search.html
- */
-exports.searchTips = function (query, access_token, success_handler, error_handler) {
-
- var url = API_URL + "/tips/search";
-
- url += "?" + QUERYSTRING.stringify(query);
-
- getRequest(url, access_token, function (status, result) {
- extractData(status, result, "tips", success_handler, error_handler);
- });
-
-};
-
-
-exports.getRecentCheckins = function (query, access_token, success_handler, error_handler) {
-
- var url = API_URL + "/checkins/recent";
-
- url += "?" + QUERYSTRING.stringify(query);
-
- getRequest(url, access_token, function (status, result) {
- extractData(status, result, "recent", success_handler, error_handler);
- });
-
-};
-
-exports.searchVenues = function (query, access_token, success_handler, error_handler) {
-
- var url = API_URL + "/venues/search";
-
- url += "?" + QUERYSTRING.stringify(query);
-
- getRequest(url, access_token, function (status, result) {
- extractData(status, result, "groups", success_handler, error_handler);
- });
-};
-
-exports.searchUsers = function (query, access_token, success_handler, error_handler) {
-
- var url = API_URL + "/users/search";
-
- url += "?" + QUERYSTRING.stringify(query);
-
- getRequest(url, access_token, function (status, result) {
- extractData(status, result, "results", success_handler, error_handler);
- });
-};
-
-
-exports.getSettings = function (access_token, success_handler, error_handler) {
-
- var url = API_URL + "/settings/all";
-
- getRequest(url, access_token, function (status, result) {
- extractData(status, result, "settings", success_handler, error_handler);
- });
-};
-
-exports.getPhoto = function (photo_id, access_token, success_handler, error_handler) {
-
- var url = API_URL + "/photos/" + photo_id;
-
- getRequest(url, access_token, function (status, result) {
- extractData(status, result, "photo", success_handler, error_handler);
- });
-};
-
-exports.getTip = function (tip_id, access_token, success_handler, error_handler) {
-
- var url = API_URL + "/tips/" + tip_id;
-
- getRequest(url, access_token, function (status, result) {
- extractData(status, result, "tip", success_handler, error_handler);
- });
-};
-
-exports.getCheckin = function (checkin_id, access_token, success_handler, error_handler) {
-
- var url = API_URL + "/checkins/" + checkin_id;
-
- getRequest(url, access_token, function (status, result) {
- extractData(status, result, "checkin", success_handler, error_handler);
- });
-};
-
-
-exports.getVenue = function (venue_id, access_token, success_handler, error_handler) {
-
- var url = API_URL + "/venues/" + venue_id;
-
- getRequest(url, access_token, function (status, result) {
- extractData(status, result, "venue", success_handler, error_handler);
- });
-};
-
-exports.getVenueAspect = function (venue_id, access_token, success_handler, error_handler, aspect, params) {
-
- var url = API_URL + "/venues/" + venue_id + '/' + aspect;
-
- url += "?" + QUERYSTRING.stringify(params || {});
- getRequest(url, access_token, function (status, result) {
- extractData(status, result, aspect, success_handler, error_handler);
- });
-};
-
-exports.getUser = function (user_id, access_token, success_handler, error_handler) {
-
- var url = API_URL + "/users/" + user_id;
-
- getRequest(url, access_token, function (status, result) {
- extractData(status, result, "user", success_handler, error_handler);
- });
-};
-
-exports.getUserCheckins = function (user_id, access_token, success_handler, error_handler, params) {
-
- var url = API_URL + "/users/" + user_id + "/checkins";
-
- var optional = QUERYSTRING.stringify(params || {});
-
- if (optional !== "") {
- url += "?" + optional;
- }
-
- getRequest(url, access_token, function (status, result) {
- extractData(status, result, "checkins", success_handler, error_handler);
- });
-};
View
317 src/foursquare_test.js
@@ -1,317 +0,0 @@
-var sys = require('sys'),
- express = require('express'),
- assert = require('assert');
-
-var FOURSQ = require('./foursquare'),
- KEYS = require('./key');
-
-
-var CLIENT_ID = KEYS.CLIENT_ID;
-var CLIENT_SECRET = KEYS.CLIENT_SECRET;
-// var REDIRECT_URI = "http://localhost:30000/callback";
-var REDIRECT_URI = "http://distancebodza.com:30000/callback";
-
-
-// Test functions
-
-function testUserCheckins(access_token) {
-
- var params = {
- limit: 200
- };
-
- FOURSQ.getUserCheckins("self", access_token, function (data) {
-
- var result = data.count;
-
- try {
- assert.notStrictEqual(result, undefined);
- console.log("-> userCheckins OK");
- } catch (e) {
- console.log("-> userCheckins ERROR");
- }
-
- }, function (error) {
- console.log("-> userCheckins ERROR");
- }, params);
-}
-
-function testUserBadges(access_token) {
-
- FOURSQ.getUserBadges("self", access_token, function (data) {
-
- var result = data;
-
- try {
- assert.notStrictEqual(result, undefined);
- console.log("-> userBadges OK");
- } catch (e) {
- console.log("-> userBadges ERROR");
- }
-
- }, function (error) {
- console.log("-> userBadges ERROR");
- });
-}
-
-function testUserSearch(access_token) {
-
- var query = { twitter: "naveen" };
-
- FOURSQ.searchUsers(query, access_token, function (data) {
-
- var result = data[0].id;
-
- try {
- assert.equal(result, '33');
- console.log("-> searchUser OK");
- } catch (e) {
- console.log("-> searchUser ERROR");
- }
-
- }, function (error) {
- console.log("-> searchUser ERROR");
- });
-}
-
-function testVenueSearch(access_token) {
-
- var query = { ll: "40.7, -74" };
-
- FOURSQ.searchVenues(query, access_token, function (data) {
-
- var result = data[0].type;
-
- try {
- assert.equal(result, 'trending');
- console.log("-> searchVenue OK");
- } catch (e) {
- console.log("-> searchVenue ERROR");
- }
-
- }, function (error) {
- console.log(error);
- console.log("-> searchVenue ERROR");
- });
-}
-
-function testTipSearch(access_token) {
-
- var query = { ll: "40.7, -74" };
-
- FOURSQ.searchTips(query, access_token, function (data) {
-
- var result = data[0].text;
-
- try {
- assert.equal(result, 'It is time for espresso');
- console.log("-> searchTips OK");
- } catch (e) {
- console.log("-> searchTips ERROR");
- }
-
- }, function (error) {
- console.log(error);
- console.log("-> searchTips ERROR");
- });
-}
-
-
-function testGetRecentCheckins(access_token) {
-
- FOURSQ.getRecentCheckins( { limit: "20" }, access_token, function (data) {
-
- var result = JSON.stringify(data);
-
- try {
- assert.ok(result);
- console.log("-> getRecentCheckins OK");
- } catch (e) {
- console.log("-> getRecentCheckins ERROR");
- }
-
- }, function (error) {
- console.log("-> getRecentCheckins ERROR");
- });
-}
-
-function testGetSettings(access_token) {
-
- FOURSQ.getSettings( access_token, function (data) {
-
- var result = JSON.stringify(data);
-
- try {
- assert.ok(result);
- console.log("-> getSettings OK");
- } catch (e) {
- console.log("-> getSettings ERROR");
- }
-
- }, function (error) {
- console.log("-> getSettings ERROR");
- });
-}
-
-
-function testGetPhoto(access_token) {
-
- FOURSQ.getPhoto("4d0fb8162d39a340637dc42b", access_token, function (data) {
- var result = data.id;
-
- try {
- assert.equal(result, "4d0fb8162d39a340637dc42b");
- console.log("-> getPhoto OK");
- } catch (e) {
- console.log("-> getPhoto ERROR");
- }
-
- }, function (error) {
- console.log("-> getPhoto ERROR");
- });
-}
-
-function testGetUser(access_token) {
-
- FOURSQ.getUser("self", access_token, function (data) {
- var result = data;
-
- try {
- assert.ok(result);
- console.log("-> getUser OK");
- } catch (e) {
- console.log("-> getUser ERROR");
- }
-
- }, function (error) {
- console.log("-> getUser ERROR");
- });
-}
-
-function testGetVenue(access_token) {
-
- FOURSQ.getVenue(5104, access_token, function (data) {
- var result = data.id;
-
- try {
- assert.equal(result, "40a55d80f964a52020f31ee3");
- console.log("-> getVenue OK");
- } catch (e) {
- console.log("-> getVenue ERROR");
- }
-
- }, function (error) {
- console.log("-> getVenue ERROR");
- });
-}
-
-function testGetVenueAspect(access_token) {
-
- var venueId = '3fd66200f964a5206be61ee3';
-
- FOURSQ.getVenueAspect(venueId, access_token, function (data) {
- var result = data.items[0];
-
- try {
- assert.ok(result);
- console.log("-> getVenueAspect OK");
- } catch (e) {
- console.log("-> getVenueAspect ERROR");
- }
-
- }, function (error) {
- console.log(error);
- console.log("-> getVenueAspect ERROR");
- }, 'tips');
-}
-
-function testGetCheckin(access_token) {
-
- FOURSQ.getCheckin("IHR8THISVNU", access_token, function (data) {
- var result = data;
-
- try {
- assert.ok(result);
- console.log("-> getCheckin OK");
- } catch (e) {
- console.log("-> getCheckin ERROR");
- }
-
- }, function (error) {
- console.log("-> getCheckin ERROR");
- });
-}
-
-function testGetTip(access_token) {
-
- FOURSQ.getTip("4b5e662a70c603bba7d790b4", access_token, function (data) {
- var result = data.id;
-
- try {
- assert.equal(result, "4b5e662a70c603bba7d790b4");
- console.log("-> getTip OK");
- } catch (e) {
- console.log("-> getTip ERROR");
- }
-
- }, function (error) {
- console.log("-> getTip ERROR");
- });
-}
-
-
-
-var app = express.createServer();
-
-app.get('/login', function(req, res) {
-
- var loc = "https://foursquare.com/oauth2/authenticate?client_id=" + CLIENT_ID + "&response_type=code&redirect_uri=" + REDIRECT_URI;
- res.writeHead(303, { 'location': loc });
- res.end();
-});
-
-app.get('/callback', function (req, res) {
-
- var code = req.query.code;
-
- FOURSQ.getAccessToken({
- code: code,
- redirect_uri: REDIRECT_URI,
- client_id: CLIENT_ID,
- client_secret: CLIENT_SECRET
- }, function (access_token) {
-
- if (access_token !== undefined) {
-
- testUserCheckins(access_token);
- testUserBadges(access_token);
- testTipSearch(access_token);
- testUserSearch(access_token);
- testVenueSearch(access_token);
-
- testGetRecentCheckins(access_token);
- testGetSettings(access_token);
- testGetPhoto(access_token);
-
- testGetUser(access_token);
- testGetVenue(access_token);
- testGetVenueAspect(access_token);
-
- testGetCheckin(access_token);
- testGetTip(access_token);
-
- res.send('Please check the console.');
-
- } else {
- console.log("access_token is undefined.");
- }
-
- });
-});
-
-app.get('/', function(req, res){
- res.send('Foursquare on node, please <a href="/login">login</a> to run the tests');
-});
-
-
-app.listen(30000);
View
15 src/package.json
@@ -1,15 +0,0 @@
-{
- "name": "foursquareonnode",
- "version": "0.0.2",
- "description": "Foursquare on Node, Foursquare API v2 wrapper for Node JS.",
- "main": "foursquare.js",
- "keywords": [
- "foursquare",
- "4sq"
- ],
- "repository" : {
- "type" : "git",
- "url" : "https://yikulju@github.com/yikulju/Foursquare-on-node.git"
- }
-
-}
View
292 test/node-foursquare-test.js
@@ -0,0 +1,292 @@
+var sys = require('sys'),
+ express = require('express'),
+ assert = require('assert');
+
+var Foursquare = require('./../lib/node-foursquare').Foursquare(),
+ config = require('./config').config;
+
+// Test functions
+function testUserCheckins(accessToken) {
+
+ var params = {
+ limit: 200
+ };
+
+ Foursquare.getUserCheckins("self", accessToken, params, function (error, data) {
+ if(error) {
+ console.log("-> userCheckins ERROR: " + sys.inspect(error));
+ }
+ else {
+ try {
+ assert.notStrictEqual(data.count, undefined);
+ console.log("-> userCheckins OK");
+ } catch (e) {
+ console.log("-> userCheckins ERROR: " + sys.inspect(e));
+ }
+ }
+ });
+}
+
+function testUserBadges(accessToken) {
+ Foursquare.getUserBadges("self", accessToken, function (error, data) {
+ if(error) {
+ console.log("-> userBadges ERROR: " + sys.inspect(error));
+ }
+ else {
+ try {
+ assert.notStrictEqual(data, undefined);
+ console.log("-> userBadges OK");
+ } catch (e) {
+ console.log("-> userBadges ERROR: " + sys.inspect(e));
+ }
+ }
+ });
+}
+
+function testUserSearch(accessToken) {
+
+ var query = { twitter: "naveen" };
+
+ Foursquare.searchUsers(query, accessToken, function (error, data) {
+ if(error) {
+ console.log("-> searchUser ERROR: " + sys.inspect(error));
+ }
+ else {
+ try {
+ assert.equal(data[0].id, '33');
+ console.log("-> searchUser OK");
+ } catch (e) {
+ console.log("-> searchUser ERROR: " + sys.inspect(e));
+ }
+ }
+ });
+}
+
+function testVenueSearch(accessToken) {
+
+ var query = { ll: "40.7, -74" };
+
+ Foursquare.searchVenues(query, accessToken, function (error, data) {
+ if(error) {
+ console.log("-> searchVenue ERROR: " + sys.inspect(error));
+ }
+ else {
+ try {
+ assert.equal(data[0].type, 'trending');
+ console.log("-> searchVenue OK");
+ } catch (e) {
+ console.log("-> searchVenue ERROR: " + sys.inspect(e));
+ }
+ }
+ });
+}
+
+function testTipSearch(accessToken) {
+
+ var query = { ll: "40.7, -74" };
+
+ Foursquare.searchTips(query, accessToken, function (error, data) {
+ if(error) {
+ console.log("-> searchTips ERROR: " + sys.inspect(error));
+ }
+ else {
+ try {
+ assert.equal(data[0].text, 'It is time for espresso');
+ console.log("-> searchTips OK");
+ } catch (e) {
+ console.log("-> searchTips ERROR: " + sys.inspect(e));
+ }
+ }
+ });
+}
+
+
+function testGetRecentCheckins(accessToken) {
+
+ Foursquare.getRecentCheckins( { limit: "20" }, accessToken, function (error, data) {
+ if(error) {
+ console.log("-> getRecentCheckins ERROR: " + sys.inspect(error));
+ }
+ else {
+ try {
+ var result = JSON.stringify(data);
+ assert.ok(result);
+ console.log("-> getRecentCheckins OK");
+ } catch (e) {
+ console.log("-> getRecentCheckins ERROR: " + sys.inspect(e));
+ }
+ }
+ });
+}
+
+function testGetSettings(accessToken) {
+
+ Foursquare.getSettings(accessToken, function (error, data) {
+ if(error) {
+ console.log("-> getSettings ERROR: " + sys.inspect(error));
+ }
+ else {
+ try {
+ var result = JSON.stringify(data);
+ assert.ok(result);
+ console.log("-> getSettings OK");
+ } catch (e) {
+ console.log("-> getSettings ERROR: " + sys.inspect(e));
+ }
+ }
+ });
+}
+
+
+function testGetPhoto(accessToken) {
+
+ Foursquare.getPhoto("4d0fb8162d39a340637dc42b", accessToken, function (error, data) {
+ if(error) {
+ console.log("-> getPhoto ERROR: " + sys.inspect(error));
+ }
+ else {
+ try {
+ var result = data.id;
+ assert.equal(result, "4d0fb8162d39a340637dc42b");
+ console.log("-> getPhoto OK");
+ } catch (e) {
+ console.log("-> getPhoto ERROR: " + sys.inspect(e));
+ }
+ }
+ });
+}
+
+function testGetUser(accessToken) {
+
+ Foursquare.getUser("self", accessToken, function (error, data) {
+ if(error) {
+ console.log("-> getUser ERROR: " + sys.inspect(error));
+ }
+ else {
+ try {
+ assert.ok(data);
+ console.log("-> getUser OK");
+ } catch (e) {
+ console.log("-> getUser ERROR: " + sys.inspect(e));
+ }
+ }
+ });
+}
+
+function testGetVenue(accessToken) {
+
+ Foursquare.getVenue(5104, accessToken, function(error, data) {
+ if(error) {
+ console.log("-> getVenue ERROR: " + sys.inspect(error));
+ }
+ else {
+ try {
+ assert.equal(data.id, "40a55d80f964a52020f31ee3");
+ console.log("-> getVenue OK");
+ } catch (e) {
+ console.log("-> getVenue ERROR: " + sys.inspect(e));
+ }
+ }
+ });
+}
+
+function testGetVenueAspect(accessToken) {
+ Foursquare.getVenueAspect("3fd66200f964a5206be61ee3", accessToken, "tips", null, function(error, data) {
+ if(error) {
+ console.log("-> getVenueAspect ERROR: " + sys.inspect(error));
+ }
+ else {
+ try {
+ assert.ok(data.items[0]);
+ console.log("-> getVenueAspect OK");
+ } catch (e) {
+ console.log("-> getVenueAspect ERROR: " + sys.inspect(e));
+ }
+ }
+ });
+}
+
+function testGetCheckin(accessToken) {
+
+ Foursquare.getCheckin("IHR8THISVNU", accessToken, function (error, data) {
+ if(error) {
+ console.log("-> getCheckin ERROR: " + sys.inspect(error));
+ }
+ else {
+ try {
+ assert.ok(data);
+ console.log("-> getCheckin OK");
+ } catch (e) {
+ console.log("-> getCheckin ERROR: " + sys.inspect(e));
+ }
+ }
+ });
+}
+
+function testGetTip(accessToken) {
+
+ Foursquare.getTip("4b5e662a70c603bba7d790b4", accessToken, function (data) {
+ if(error) {
+ console.log("-> getTip ERROR: " + sys.inspect(error));
+ }
+ else {
+ try {
+ assert.equal(data.id, "4b5e662a70c603bba7d790b4");
+ console.log("-> getTip OK");
+ } catch (e) {
+ console.log("-> getTip ERROR: " + sys.inspect(error));
+ }
+ }
+ });
+}
+
+var app = express.createServer();
+
+app.get('/login', function(req, res) {
+ var loc = "https://foursquare.com/oauth2/authenticate?client_id=" + config.clientId + "&response_type=code&redirect_uri=" + config.redirectUri;
+ res.writeHead(303, { 'location': loc });
+ res.end();
+});
+
+app.get('/callback', function (req, res) {
+
+ var code = req.query.code;
+
+ Foursquare.getAccessToken({
+ code: code,
+ redirect_uri: config.redirectUri,
+ client_id: config.clientId,
+ client_secret: config.clientSecret
+ }, function (error, accessToken) {
+
+ if(error) {
+ res.send("An error was thrown: " + sys.inspect(error));
+ }
+ else if (accessToken !== undefined) {
+ testUserCheckins(accessToken);
+ testUserBadges(accessToken);
+ testTipSearch(accessToken);
+ testUserSearch(accessToken);
+ testVenueSearch(accessToken);
+ testGetRecentCheckins(accessToken);
+ testGetSettings(accessToken);
+ testGetPhoto(accessToken);
+ testGetUser(accessToken);
+ testGetVenue(accessToken);
+ testGetVenueAspect(accessToken);
+ testGetCheckin(accessToken);
+ testGetTip(accessToken);
+ res.send('Please check the console.');
+ } else {
+ console.log("accessToken is undefined.");
+ }
+
+ });
+});
+
+app.get('/', function(req, res){
+ res.send("Please <a href=\"/login\">login</a> to run the tests");
+});
+
+
+app.listen(30000);
View
2  tools/start_server.sh
@@ -1,2 +0,0 @@
-cd ../src
-supervisor node foursquare_test.js
Please sign in to comment.
Something went wrong with that request. Please try again.