Created
December 29, 2012 23:29
-
-
Save joshtwist/4409988 to your computer and use it in GitHub Desktop.
Simple function to generate a basic profile for users that log into your Mobile Service via Twitter and Facebook. This is great for pre-completing a default profile and/or getting profile images for your users. Includes simple mocha tests.
This file contains hidden or 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
| function fetchBasicProfile(user, callback) { | |
| var req = require('request'); | |
| var util = require('util'); | |
| var sep = user.userId.indexOf(':'); | |
| var profile = {}; | |
| profile.source = user.userId.substring(0, sep).toLowerCase(); | |
| profile.id = user.userId.substring(sep + 1); | |
| var createError = function(e, r) { | |
| var newError = {}; | |
| newError.message = e ? e.toString() : r.body; | |
| newError.statusCode = r.statusCode; | |
| newError.responseText = r.body; | |
| return newError; | |
| } | |
| switch (profile.source) { | |
| case 'facebook': | |
| var url = util.format('https://graph.facebook.com/%s', profile.id); | |
| req.get(url, function(error, response, body) { | |
| if (error || response.statusCode != 200) callback(createError(error, response)); | |
| else { | |
| var rmt = JSON.parse(body); | |
| profile.name = rmt.name; | |
| profile.firstName = rmt.first_name; | |
| profile.lastName = rmt.last_name; | |
| profile.userName = rmt.username; | |
| profile.smallImageUrl = url + '/picture?type=small'; | |
| profile.largeImageUrl = url + '/picture?type=large'; | |
| callback(null, profile); | |
| return; | |
| } | |
| }); | |
| break; | |
| case 'twitter': | |
| var url = util.format('https://api.twitter.com/1/users/show.json?user_id=%s', profile.id); | |
| req.get(url, function(error, response, body) { | |
| if (error || response.statusCode != 200) callback(createError(error, response)); | |
| else { | |
| var rmt = JSON.parse(body); | |
| profile.name = rmt.name; | |
| profile.userName = rmt.screen_name; | |
| profile.smallImageUrl = util.format('http://api.twitter.com/1/users/profile_image/%s.normal', profile.userName); | |
| profile.largeImageUrl = util.format('http://api.twitter.com/1/users/profile_image/%s.bigger', profile.userName); | |
| callback(null, profile); | |
| } | |
| }); | |
| break; | |
| default: | |
| callback(null, profile); | |
| } | |
| } |
This file contains hidden or 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
| var u1 = { userId: "Facebook:753472068" }; | |
| var u2 = { userId: "Twitter:23967168" }; | |
| var assert = require('assert'); | |
| var req = require('request'); | |
| function loadAndVerifyImage(url, callback) { | |
| req.get(url, function(error, response, body) { | |
| assert.equal(200, response.statusCode); | |
| assert.equal(0, response.headers['content-type'].indexOf('image/')); | |
| callback(); | |
| }) | |
| } | |
| suite('fetchBasicProfile', function() { | |
| test('source and id OK', function(done) { | |
| fetchBasicProfile(u1, function(error, profile) { | |
| assert.equal('facebook', profile.source); | |
| assert.equal('753472068', profile.id); | |
| done(); | |
| }); | |
| }); | |
| test('facebook basics', function(done) { | |
| fetchBasicProfile(u1, function(error, profile) { | |
| assert.equal('Josh Twist', profile.name); | |
| assert.equal('Josh', profile.firstName); | |
| assert.equal('Twist', profile.lastName); | |
| assert.equal('joshtwist', profile.userName); | |
| done(); | |
| }); | |
| }); | |
| test('facebook small image', function(done) { | |
| fetchBasicProfile(u1, function(error, profile) { | |
| loadAndVerifyImage(profile.smallImageUrl, function() { | |
| done(); | |
| }); | |
| }); | |
| }); | |
| test('facebook large image', function(done) { | |
| fetchBasicProfile(u1, function(error, profile) { | |
| loadAndVerifyImage(profile.largeImageUrl, function() { | |
| done(); | |
| }); | |
| }); | |
| }); | |
| test('twitter basics', function(done) { | |
| fetchBasicProfile(u2, function(error, profile) { | |
| assert.equal('Josh Twist', profile.name); | |
| assert.equal('joshtwist', profile.userName); | |
| done(); | |
| }); | |
| }); | |
| test('twitter small image', function(done) { | |
| fetchBasicProfile(u2, function(error, profile) { | |
| loadAndVerifyImage(profile.smallImageUrl, function() { | |
| done(); | |
| }); | |
| }); | |
| }); | |
| test('twitter large image', function(done) { | |
| fetchBasicProfile(u2, function(error, profile) { | |
| loadAndVerifyImage(profile.largeImageUrl, function() { | |
| done(); | |
| }); | |
| }); | |
| }); | |
| test('test bad user', function(done) { | |
| fetchBasicProfile({ userId: 'Facebook:1nons2ense3' }, function(err, profile) { | |
| assert.equal(null, profile); | |
| assert.notEqual(null, err); | |
| done(); | |
| }); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment