Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

implemented gravatar, added test cases and npm package.json

  • Loading branch information...
commit a50cb032f70a6b499e7f97f29d60cba77ed00300 1 parent 65331f7
@arnabc authored
Showing with 143 additions and 0 deletions.
  1. +104 −0 lib/gravatar.js
  2. +18 −0 package.json
  3. +21 −0 test/gravatar_test.js
View
104 lib/gravatar.js
@@ -0,0 +1,104 @@
+// we need crypto module for making md5 hash
+var crypto = require('crypto');
+
+var Gravatar = module.exports = function () {
+ // stores the generated email id and corresponding md5 hash
+ // so that we don't need to generate the same hash again and again
+ var md5 = {};
+
+ // list of allowed ratings
+ var RATINGS = [ 'G', 'PG', 'R', 'X' ];
+
+ // default URL for gravatar
+ var G_URL = 'http://www.gravatar.com/avatar/';
+
+ // default size
+ var DEFAULT_ICON_SIZE = 96;
+
+ // default rating
+ var DEFAULT_RATING = 'G';
+
+ // default location for users who has no Gravatar avatar
+ var DEFAULT_ICON_PATH = undefined;
+
+
+ // method converts the specified object to Gravatar compatible URL
+ // with querystring params
+ function getURL ( ob ) {
+ // extract the email and then remove the key from
+ // the object itself
+ var email = ob.email;
+ delete ob.email;
+
+ var params = [];
+ for ( var k in ob ) {
+ // exclude undefined/null properties but not empty => '' or '0'
+ if( typeof ob[k] !== 'undefined' && ob[k] !== null )
+ params[ params.length ] = k + '=' + encodeURIComponent( ob[k] );
+ }
+
+ return G_URL + email + '?' + params.join('&');
+ }
+
+ return {
+ // method to generate the gravatar image URL
+ // @param {String} email - required
+ // @param {String} rating - optional
+ // @param {Number} size - optional
+ // @param {String} defaultForMissing - optional image for non-existent email account in gravatar.com
+ get: function ( email, rating, size, defaultForMissing ) {
+ var qs = {};
+
+ if( !email )
+ throw Error('[Error] Invalid email, email is :' + email );
+
+ // remove all whitespace and lowercase all letters in the email before md5 hashing
+ email = email.toLowerCase().trim();
+
+ if( !md5[ email ] ) {
+ // md5 hash
+ qs.email = crypto.createHash( 'md5' ).update( email ).digest( 'hex' );
+
+ // store it in the hash to avoid rehashing everytime
+ md5[ email ] = qs.email;
+ }
+ else
+ qs.email = md5[ email ];
+
+ // if specified rating does not match with any of these
+ // then it defaults to G
+ qs.r = rating && RATINGS.indexOf( rating ) >= 0 ? rating : DEFAULT_RATING;
+
+ // size can be 1 to 512, anything other than defaults to 96
+ qs.s = size && size >= 1 && size <= 512 ? size : DEFAULT_ICON_SIZE;
+
+ // if default image URL specified then use that
+ qs.d = defaultForMissing || DEFAULT_ICON_PATH;
+
+
+ return getURL( qs );
+ },
+
+ // method to set the default values of the optional items once
+ // so that there is no need to pass optional values everytime
+ // for customization.
+ //
+ // Remember that arguments passed during "get()" method call will always
+ // get preference over default values.
+ //
+ // @param {String} rating
+ // @param {Number} size
+ // @param {String} defaultForMissing
+ setDefaultOptions: function ( rating, size, defaultForMissing ) {
+ if( rating )
+ DEFAULT_RATING = rating;
+
+ if( size )
+ DEFAULT_ICON_SIZE = size;
+
+ if( defaultForMissing )
+ DEFAULT_ICON_PATH = defaultForMissing;
+ }
+ };
+
+}();
View
18 package.json
@@ -0,0 +1,18 @@
+{
+ 'name': 'node-gravatar',
+ 'version': '1.0.0',
+ 'description': 'Node.js Gravatar URL generator, useful for using with Node.js blogging frameworks',
+ 'author': 'Arnab Chakraborty',
+ 'email': 'arnabc@webgyani.com',
+ 'directories': {
+ 'lib': './lib/'
+ },
+ 'main': './lib/gravatar',
+ 'repositories': {
+ 'type': 'git',
+ 'url': 'git://github.com/arnabc/node-gravatar.git'
+ },
+ 'engines': {
+ 'node': '>=0.1.103'
+ }
+}
View
21 test/gravatar_test.js
@@ -0,0 +1,21 @@
+var assert = require('assert'),
+crypto = require('crypto');
+
+var gravatar = require('./../lib/gravatar');
+
+var url = gravatar.get('arnabc@webgyani.com');
+// test default rating
+assert.ok( url.indexOf( 'r=G' ) > -1, 'Default Rating test failed' );
+// test size
+assert.ok( url.indexOf('s=96') > -1, 'Default size test failed' );
+
+url = gravatar.get( 'arnabc@webgyani.com', 'PG', 120 );
+assert.ok( url.indexOf('r=PG') > -1, 'Specified size test failed');
+assert.ok( url.indexOf('s=120') > -1, 'Specified rating test failed' );
+
+var md5 = crypto.createHash('md5').update('arnabc@webgyani.com').digest('hex');
+assert.ok( url.indexOf( md5 ) > -1, 'MD5 digest mismatch' );
+
+assert.throws( function () {
+ var u = gravatar.get();
+ } );
Please sign in to comment.
Something went wrong with that request. Please try again.