Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 5 commits
  • 5 files changed
  • 2 commit comments
  • 1 contributor
View
3  Changelog.md
@@ -1,3 +1,6 @@
+### 2.0.0
+- Gravatar is now an object
+- fetch the picture
### 1.0.4
- Test script to avoid global nodeunit - Fix #4
### 1.0.3
View
27 Readme.md
@@ -7,8 +7,7 @@ Dependencies
------------
### Runtime
-* Node 0.2.X+
-* Works with Node.js 0.4.X too.
+* Works with Node.js 0.6.X.
### Development/Tests
* nodeunit
@@ -23,7 +22,7 @@ Usage
gravatar.url(email, options, https=false);
## Where:
-* email:
+* email:
The gravatar email
* options:
Query string options. Ex: size or s, default or d, rating or r, forcedefault or f.
@@ -32,17 +31,24 @@ Usage
Define if will use secure gravatar. Default is false.
### Examples
- var gravatar = require('gravatar');
- var url = gravatar.url('emerleite@gmail.com', {s: '200', r: 'pg', d: '404'});
+ var Gravatar = require('gravatar').Gravatar;
+ var gravatar = new Gravatar('emerleite@gmail.com');
+ var url = gravatar.url({s: '200', r: 'pg', d: '404'});
//returns http://www.gravatar.com/avatar/93e9084aa289b7f1f5e4ab6716a56c3b?s=200&r=pg&d=404
- var secureUrl = gravatar.url('emerleite@gmail.com', {s: '100', r: 'x', d: 'retro'}, true);
+ var secureUrl = gravatar.url({s: '100', r: 'x', d: 'retro'}, true);
//returns https://secure.gravatar.com/avatar/93e9084aa289b7f1f5e4ab6716a56c3b?s=100&r=x&d=retro
-
+ gravatar.fetch(function(picture) {
+ //picture.data contains a Buffer with the picture.
+ });
+
Running tests (3 ways)
----------------------
-$ npm test
-$ ./run-tests.js
-$ nodeunit test
+
+ $ npm test
+
+ $ ./run-tests.js
+
+ $ nodeunit test
To-Do
-----
@@ -52,6 +58,7 @@ Author
------
* Emerson Macedo (<http://codificando.com/>)
+* Mathieu Lecarme
License:
--------
View
78 lib/gravatar.js
@@ -1,12 +1,72 @@
-var crypto = require('crypto')
- , querystring = require('querystring');
+var crypto = require('crypto'),
+ querystring = require('querystring'),
+ url = require('url'),
+ http = require('http'),
+ https = require('https');
-var gravatar = module.exports = {
- url: function (email, options, https) {
- var baseURL = (https && "https://secure.gravatar.com/avatar/") || 'http://www.gravatar.com/avatar/';
- var queryData = querystring.stringify(options);
- var query = (queryData && "?" + queryData) || "";
+/**
+ * Picture object
+ *
+ * Picture.length data size.
+ * Picture.data is Buffer, you can write it or handle it a string.
+ * Picture.mime mime type.
+ * Picture.gravatar the gravatar object.
+ */
+function Picture(gravatar) {
+ this.gravatar = gravatar;
+}
- return baseURL + crypto.createHash('md5').update(email.toLowerCase().trim()).digest('hex') + query;
- }
+function Gravatar(email) {
+ this.email = email.trim().toLowerCase();
+}
+
+/**
+ * Compute gravatar signature
+ */
+Gravatar.prototype.hash = function() {
+ return crypto.createHash('md5').update(this.email).digest('hex');
+};
+
+/**
+ * Build url
+ */
+Gravatar.prototype.url = function(options, secure) {
+ options = options ? options : {};
+ secure = secure ? secure : false;
+ var baseURL = (secure && "https://secure.gravatar.com/avatar/") || 'http://www.gravatar.com/avatar/';
+ var queryData = querystring.stringify(options);
+ var query = (queryData && "?" + queryData) || "";
+
+ return baseURL + this.hash() + query;
};
+
+/**
+ * Fetch a picture
+ */
+Gravatar.prototype.fetch = function(cb, options, secure){
+ var that = this;
+ var u = url.parse(this.url(options, secure));
+ var protocol = (u.protocol === 'http:') ? http : https;
+ protocol.get(u, function(res) {
+ var p = new Picture(that);
+ p.length = parseInt(res.headers['content-length'], 10);
+ p.mime = res.headers['content-type'];
+ p.last = res.headers['last-modified'];
+ p.data = new Buffer(p.length);
+ var copied = 0;
+ res.on('data', function(chunk){
+ chunk.copy(p.data, copied);
+ copied += chunk.length;
+ });
+ res.on('end', function() {
+ cb(p)
+ });
+ });
+};
+
+exports.Gravatar = Gravatar;
+
+exports.url = function(email, options, secure) {
+ var g = new Gravatar(email);
+ return g.url(options, secure)
+}
View
4 package.json
@@ -1,7 +1,7 @@
{ "name" : "gravatar"
, "description" : "Gravatar Node.js library"
-, "keywords" : [ "gravatar", "avatar", "package.json" ]
-, "version" : "1.0.4"
+, "keywords" : [ "gravatar", "avatar" ]
+, "version" : "2.0.0"
, "author" : "Emerson Macedo <emerleite@gmail.com>"
, "repository" : { "type": "git"
, "url": "git://github.com/emerleite/node-gravatar.git" }
View
18 test/gravatar.test.js
@@ -1,8 +1,8 @@
-var testCase = require('nodeunit').testCase
- , url = require('url')
- , gravatar = require('../lib/gravatar')
- , baseURL = "http://www.gravatar.com/avatar/"
- , baseSecureURL = "https://secure.gravatar.com/avatar/";
+var testCase = require('nodeunit').testCase,
+ url = require('url'),
+ gravatar = require('../lib/gravatar'),
+ baseURL = "http://www.gravatar.com/avatar/",
+ baseSecureURL = "https://secure.gravatar.com/avatar/";
module.exports = testCase({
'it should gererate correct uri given an email': function(test) {
@@ -28,5 +28,13 @@ module.exports = testCase({
var gravatarURL = gravatar.url('emerleite@gmail.com', {}, true);
test.equal(gravatar.url('emerleite@gmail.com', {}, true), baseSecureURL + "93e9084aa289b7f1f5e4ab6716a56c3b");
test.done();
+ },
+ 'it should fetch a picture': function(test) {
+ var g = new gravatar.Gravatar('emerleite@gmail.com');
+ g.fetch(function(picture) {
+ //It 's a JPEG aka JFIF file
+ test.equal(picture.data.toString('binary', 6, 10), 'JFIF');
+ test.done();
+ })
}
});

Showing you all comments on commits in this comparison.

@emerleite
Owner

Hi,

fist I have to say that I love your changes. Just need few fixes, IMO.

1 - In fetch a picture test, need do ensure the picture returned.
2 - In package.json, do not change repository url and author, because its not the right way. Fell free to put your information in the readme contributors section.
3 - Since you've updated the package.json version and the readme, it's good if you update the Changelog too.
4 - Seems that you not changed the uri test and it will broke the test suite. Could you take a look?

With this changes I'll test it and integrate.

It's ok for you?

Cheers,

@athoune

It's ok for me. I send you a second pull request.

Something went wrong with that request. Please try again.