diff --git a/lib/gravatar.js b/lib/gravatar.js index 0029ab3..13614d1 100644 --- a/lib/gravatar.js +++ b/lib/gravatar.js @@ -1,15 +1,11 @@ var crypto = require('crypto') , querystring = require('querystring'); -var MD5_REGEX = /[0-9a-f]{32}/; +var MD5_REGEX = /^[0-9a-f]{32}$/; var gravatar = module.exports = { url: function (email, options, protocol) { - email = email || 'unspecified'; - email = email.trim().toLowerCase(); - var hash = email.match(MD5_REGEX) ? email : crypto.createHash('md5').update(email).digest('hex'); var baseURL; - if(typeof protocol === 'undefined'){ baseURL = "//www.gravatar.com/avatar/"; } else { @@ -17,20 +13,31 @@ var gravatar = module.exports = { } var queryData = querystring.stringify(options); - var query = (queryData && "?" + queryData) || ""; + var query = queryData ? ("?" + queryData) : ""; - return baseURL + hash + query; + return baseURL + getHash(email) + query; }, profile_url: function (email, options, https) { - email = email.trim().toLowerCase(); - var hash = email.match(MD5_REGEX) ? email : crypto.createHash('md5').update(email).digest('hex'); - var format = options != undefined && options.format != undefined ? String(options.format) : 'json' - //delete options.format - if (options != undefined && options.format != undefined) delete options.format + var baseUrl = (typeof secure === 'undefined') ? "//www.gravatar.com/" : (secure ? "https://s.gravatar.com/" : "http://www.gravatar.com/"); + + //Default format is json, replace + var format = "json"; + if(options != undefined && options.format != undefined){ + format = options.format; + delete options.format; //Delete so that it is not included in the querystring + } + var baseURL = (https && "https://secure.gravatar.com/") || 'http://www.gravatar.com/'; + var queryData = querystring.stringify(options); var query = (queryData && "?" + queryData) || ""; - return baseURL + hash + '.' + format + query; + return baseURL + getHash(email) + '.' + format + query; } }; + +function getHash(email){ + email = email || 'unspecified'; + email = email.trim().toLowerCase(); + return email.match(MD5_REGEX) ? email : crypto.createHash('md5').update(email).digest('hex'); +}