UNPKG

2 kBJavaScriptView Raw
1var md5 = require('blueimp-md5'),
2 querystring = require('querystring'),
3 MD5_REGEX = /^[0-9a-f]{32}$/;
4
5function params(options) {
6 var params = {}, removing = {protocol:1, format:1};
7 for (var key in options) {
8 if (!removing[key]) params[key] = options[key];
9 }
10 return params;
11}
12function proto(options, protocol) {
13 if (!options) return;
14 if(typeof options.protocol === 'boolean') return options.protocol;
15 return options.protocol === "http" ? false
16 : options.protocol === "https" ? true
17 : undefined;
18}
19function getHash(email){
20 email = (typeof email === 'string') ? email.trim().toLowerCase() : 'unspecified';
21 return email.match(MD5_REGEX) ? email : md5(email);
22}
23
24function getQueryString(options){
25 var queryData = querystring.stringify(params(options));
26 return (queryData && "?" + queryData) || "";
27}
28
29var gravatar = module.exports = {
30
31 url: function (email, options, protocol) {
32 var baseURL = "//www.gravatar.com/avatar/";
33 if (options && options.cdn) {
34 baseURL = options.cdn + '/avatar/';
35 delete options.cdn;
36 } else {
37 if (options && options.protocol) protocol = proto(options);
38 if(typeof protocol !== 'undefined') {
39 baseURL = protocol ? "https://s.gravatar.com/avatar/" : 'http://www.gravatar.com/avatar/';
40 }
41 }
42 var query = getQueryString(options);
43 return baseURL + getHash(email) + query;
44 },
45
46 profile_url: function (email, options, https) {
47 var format = options != undefined && options.format != undefined ? String(options.format) : 'json'
48 var baseURL
49 if (options && options.cdn) {
50 baseURL = options.cdn + '/';
51 delete options.cdn;
52 } else {
53 if (options && options.protocol) https = proto(options);
54 var baseURL = (https && "https://secure.gravatar.com/") || 'http://www.gravatar.com/';
55 }
56 var query = getQueryString(options);
57 return baseURL + getHash(email) + '.' + format + query;
58 }
59};