Skip to content

Commit

Permalink
Merge pull request #17 from igorprado/master
Browse files Browse the repository at this point in the history
Add possibility to generate URLs without protocol
  • Loading branch information
emerleite committed Apr 10, 2015
2 parents 8a6b7ca + b44f7fa commit 41e274b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 13 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
@@ -1,3 +1,5 @@
### 1.1.1
- Allow no protocol URL generation too. Closes #15
### 1.1.0
- Allow invalid email to prevent blows. Closes #8, #13
### 1.0.6
Expand Down
10 changes: 6 additions & 4 deletions Readme.md
Expand Up @@ -22,21 +22,23 @@ Instalation
Usage
------
var gravatar = require('gravatar');
gravatar.url(email, options, https=false);
gravatar.url(email, options, protocol);

## Where:
* email:
The gravatar email
* options:
Query string options. Ex: size or s, default or d, rating or r, forcedefault or f.
Should be passed as an object. Ex: {s: 200, f: 'y', d: '404'}
* https
Define if will use secure gravatar. Default is false.
* protocol
Define if will use no protocol, http or https gravatar URL. Default is 'undefined', which generates URLs without protocol. True to force https and false to force http.

### Examples
var gravatar = require('gravatar');
var url = gravatar.url('emerleite@gmail.com', {s: '200', r: 'pg', d: '404'});
//returns http://www.gravatar.com/avatar/93e9084aa289b7f1f5e4ab6716a56c3b?s=200&r=pg&d=404
//returns //www.gravatar.com/avatar/93e9084aa289b7f1f5e4ab6716a56c3b?s=200&r=pg&d=404
var unsecureUrl = gravatar.url('emerleite@gmail.com', {s: '100', r: 'x', d: 'retro'}, false);
//returns http://www.gravatar.com/avatar/93e9084aa289b7f1f5e4ab6716a56c3b?s=100&r=x&d=retro
var secureUrl = gravatar.url('emerleite@gmail.com', {s: '100', r: 'x', d: 'retro'}, true);
//returns https://secure.gravatar.com/avatar/93e9084aa289b7f1f5e4ab6716a56c3b?s=100&r=x&d=retro

Expand Down
10 changes: 8 additions & 2 deletions lib/gravatar.js
Expand Up @@ -2,10 +2,16 @@ var crypto = require('crypto')
, querystring = require('querystring');

var gravatar = module.exports = {
url: function (email, options, https) {
url: function (email, options, protocol) {
email = email || 'unspecified';
var baseURL;

if(typeof protocol === 'undefined'){
baseURL = "//www.gravatar.com/avatar/";
} else {
baseURL = protocol ? "https://secure.gravatar.com/avatar/" : 'http://www.gravatar.com/avatar/';
}

var baseURL = (https && "https://secure.gravatar.com/avatar/") || 'http://www.gravatar.com/avatar/';
var queryData = querystring.stringify(options);
var query = (queryData && "?" + queryData) || "";

Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -2,7 +2,7 @@
"name" : "gravatar",
"description" : "Gravatar Node.js library",
"keywords" : [ "gravatar", "avatar", "package.json" ],
"version" : "1.1.0",
"version" : "1.1.1",
"author" : "Emerson Macedo <emerleite@gmail.com>",
"repository" : { "type": "git",
"url": "git://github.com/emerleite/node-gravatar.git" },
Expand Down
18 changes: 12 additions & 6 deletions test/gravatar.test.js
Expand Up @@ -3,17 +3,18 @@ var should = require('should')
, gravatar = require('../lib/gravatar');

describe('gravatar', function() {
var baseURL = "http://www.gravatar.com/avatar/";
var baseNoProtocolURL = "//www.gravatar.com/avatar/";
var baseUnsecureURL = "http://www.gravatar.com/avatar/";
var baseSecureURL = "https://secure.gravatar.com/avatar/";

it('should gererate correct uri given an email', function() {
gravatar.url('emerleite@gmail.com').should.be.equal(baseURL + "93e9084aa289b7f1f5e4ab6716a56c3b");
gravatar.url('emerleite@yahoo.com.br').should.be.equal(baseURL + "6c47672b0d58bd6aae4fa70920cb3ee4");
gravatar.url('emerleite@gmail.com').should.be.equal(baseNoProtocolURL + "93e9084aa289b7f1f5e4ab6716a56c3b");
gravatar.url('emerleite@yahoo.com.br').should.be.equal(baseNoProtocolURL + "6c47672b0d58bd6aae4fa70920cb3ee4");
});

it('should generate same uri ignoring case', function() {
gravatar.url('EMERLEITE@gmAil.com').should.be.equal(baseURL + "93e9084aa289b7f1f5e4ab6716a56c3b");
gravatar.url('emerleite@YAHOO.com.BR').should.be.equal(baseURL + "6c47672b0d58bd6aae4fa70920cb3ee4");
gravatar.url('EMERLEITE@gmAil.com').should.be.equal(baseNoProtocolURL + "93e9084aa289b7f1f5e4ab6716a56c3b");
gravatar.url('emerleite@YAHOO.com.BR').should.be.equal(baseNoProtocolURL + "6c47672b0d58bd6aae4fa70920cb3ee4");
});

it('should generate uri with user passed parameters', function() {
Expand All @@ -25,7 +26,12 @@ describe('gravatar', function() {
queryString.d.should.equal('404');
});

it('should allow https gravatar uri generation', function() {
it('should force http protocol on gravatar uri generation', function() {
gravatar.url('emerleite@gmail.com', {}, false).should.be.equal(baseUnsecureURL + "93e9084aa289b7f1f5e4ab6716a56c3b");
gravatar.url('emerleite@yahoo.com.br', {}, false).should.be.equal(baseUnsecureURL + "6c47672b0d58bd6aae4fa70920cb3ee4");
});

it('should force https protocol on gravatar uri generation', function() {
var gravatarURL = gravatar.url('emerleite@gmail.com', {}, true);
gravatar.url('emerleite@gmail.com', {}, true).should.equal(baseSecureURL + "93e9084aa289b7f1f5e4ab6716a56c3b");
});
Expand Down

0 comments on commit 41e274b

Please sign in to comment.