-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
53 lines (46 loc) · 1.46 KB
/
index.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
var querystring = require('querystring');
var _ = {
includes: require('lodash/includes'),
trim: require('lodash/trim')
};
var crypto = require('blueimp-md5');
var self = {},
defaultEmail = '';
var getQueryString = function (parameters) {
var result = '';
var convertedQueryString = querystring.stringify(parameters);
if (convertedQueryString !== '') {
result = '?' + convertedQueryString;
}
return result;
};
var getBaseUrl = function (secure) {
var baseUrl = 'http://www.gravatar.com/';
if (secure) {
baseUrl = 'https://secure.gravatar.com/';
}
return baseUrl;
};
var sanitize = function(options){
options.email = options.email || defaultEmail;
options.type = _.trim(options.type);
options.$emailHash = crypto(options.email.toLowerCase().trim());
return options;
};
self.imageUrl = function(options) {
options = sanitize(options);
var baseUrl = getBaseUrl(options.secure) + 'avatar/';
var result = getQueryString(options.parameters);
return baseUrl + options.$emailHash + result;
};
self.getProfileUrl = function(options){
options = sanitize(options);
var baseUrl = getBaseUrl(options.secure);
var validTypes = ['json', 'xml', 'qr', 'php', 'vcf'];
if(!_.includes(validTypes, options.type)){
options.type = 'json';
}
var result = getQueryString(options.parameters);
return baseUrl + options.$emailHash + '.'+ options.type + result;
};
module.exports = self;