-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgravatar.js
95 lines (86 loc) · 3.37 KB
/
gravatar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
var md5 = require('md5'),
querystring = require('querystring');
exports.BASE_AVATAR_URL = 'www.gravatar.com/avatar/';
function isBrowser() {
return (typeof(window) !== 'undefined');
}
exports.trim = function (str) {
return str.replace(/^\s+/, '').replace(/\s+$/, '');
};
exports.hash = function (email) {
return md5.hex(exports.trim(email).toLowerCase());
};
/**
* Generates an avatar URL from an email address
*
* @param email {String}
* @param options {Object} - (optional)
*
* Options:
*
* - email {String} - The email address to get the avatar for.
* - hash {String} - The pre-hashed email address (alternative to using email)
*
* - size {Number} - Size in pixels of the desired avatar image.
* By default, images are presented at 80px by 80px if no size parameter
* is supplied
*
* - default_image {String} - a URL to a default image to use if the user has
* no avatar, can also be set to the following predefined options:
*
* - 404: do not load any image if none is associated with the email
* hash, instead return an HTTP 404 (File Not Found) response
* - mm: (mystery-man) a simple, cartoon-style silhouetted outline
* of a person (does not vary by email hash)
* - identicon: a geometric pattern based on an email hash
* - monsterid: a generated 'monster' with different colors, faces, etc
* - wavatar: generated faces with differing features and backgrounds
* - retro: awesome generated, 8-bit arcade-style pixelated faces
*
* - force_default {Boolean} - forces the use of the default image
*
* - rating {String} - Gravatar allows users to self-rate their images so that
* they can indicate if an image is appropriate for a certain audience. By
* default, only 'G' rated images are displayed unless you indicate that
* you would like to see higher ratings. Available options:
*
* - g: suitable for display on all websites with any audience type.
* - pg: may contain rude gestures, provocatively dressed individuals,
* the lesser swear words, or mild violence.
* - r: may contain such things as harsh profanity, intense violence,
* nudity, or hard drug use.
* - x: may contain hardcore sexual imagery or extremely disturbing
* violence.
*
* - secure {Boolean} - force https, if not set, the current protocol is used
* (if running in the browser), otherwise specifies no protocol or '//'.
*
* - extension {String} - optional image extension to use in the url (eg .jpg)
*/
exports.avatarURL = function (options) {
options = options || {};
var hash = options.hash || exports.hash(options.email);
var params = {};
if (options.size) {
params.size = options.size;
}
if (options.force_default) {
params.forcedefault = 'y';
}
if (options.default_image) {
params['default'] = options.default_image;
}
if (options.rating) {
params.rating = options.rating;
}
if (options.secure === undefined && isBrowser()) {
options.secure = location.protocol === 'https:';
}
var ext = '';
if (options.extension) {
ext = '.' + options.extension.replace(/^\./, '');
}
return (options.secure ? 'https://': '//') +
exports.BASE_AVATAR_URL + hash + ext + '?' +
querystring.stringify(params);
};