Comparing version 0.0.5 to 0.1.0
@@ -6,2 +6,4 @@ const async = require('async') | ||
const Cache = require('./Cache') | ||
class AvatarMe { | ||
@@ -11,2 +13,3 @@ constructor () { | ||
this.defaultAvatarPath = '/static/images/avatar_tiles/v1/' | ||
this.cache = null | ||
} | ||
@@ -17,14 +20,34 @@ | ||
this.defaultAvatarPath = config.defaultAvatarPath ? config.defaultAvatarPath : this.defaultAvatarPath | ||
this.cache = config.cache ? new Cache(config.cache) : null | ||
} | ||
fetchAvatar (email, username, callback) { | ||
var that = this | ||
if (!email) return callback(new Error('No email provided'), null) | ||
if (!username) return callback(new Error('No username provided'), null) | ||
if (!this.isValidEmail(email)) { | ||
let defaultImage = this.fetchDefaultImage(username) | ||
return callback(null, defaultImage) | ||
if (!this.isValidEmail(email)) return callback(new Error('No valid email'), null) | ||
if (this.cache) { | ||
return this.cache.getCachedValue(email).then(avatar => { | ||
return callback(null, avatar) | ||
}) | ||
.catch((error) => { // Cache fails. Try to fetch avatar from the APIS | ||
return this.fetchAvatarFromApis(email, username, (err, avatar) => { | ||
if (err) return callback(err, null) | ||
this.cache.setCachedValue(email, avatar) // CACHE RESULT! | ||
return callback(null, avatar) | ||
}) | ||
}) | ||
} | ||
this.fetchAvatarFromApis(email, username, (err, avatar) => { | ||
if (err) return callback(err, null) | ||
return callback(null, avatar) | ||
}) | ||
} | ||
/** | ||
* Fetch an avatar in multiple websites. | ||
* Returns a Promise | ||
*/ | ||
fetchAvatarFromApis (email, username, callback) { | ||
async.parallel({ | ||
@@ -44,14 +67,7 @@ gmail: (nextCb) => { | ||
} | ||
}, function(err, results) { | ||
let url = '' | ||
if (results.gmail) { | ||
url = results.gmail | ||
} else if (results.gravatar) { | ||
url = results.gravatar | ||
} else { | ||
url = results.default | ||
} | ||
return callback(err, url) | ||
}, (err, results) => { | ||
if (err) return callback(err, null) | ||
if (results.gmail) return callback(null, results.gmail) | ||
if (results.gravatar) return callback(null, results.gravatar) | ||
return callback(null, results.default) | ||
}) | ||
@@ -58,0 +74,0 @@ } |
{ | ||
"name": "avatar-me", | ||
"version": "0.0.5", | ||
"version": "0.1.0", | ||
"description": "Simple node module to retrieves a user avatar given an email or a user name Edit", | ||
@@ -33,2 +33,3 @@ "main": "index.js", | ||
"gravatar": "^1.6.0", | ||
"redis": "^2.7.1", | ||
"request": "^2.81.0", | ||
@@ -35,0 +36,0 @@ "validator": "^7.0.0" |
@@ -5,3 +5,3 @@ # avatar-me | ||
# How to use it? | ||
## How to use it? | ||
@@ -17,3 +17,3 @@ ```javascript | ||
# Configuration? Yes, please! | ||
## Configuration? Yes, please! | ||
@@ -28,2 +28,3 @@ | ||
}) | ||
avatarMe.fetchAvatar('jorge@ferreiro.me', 'jorge', (err, avatar) => { | ||
@@ -35,5 +36,28 @@ if (err) console.log(err) | ||
# Contribute! | ||
## Add redis cache. No more extra api calls! | ||
In 0.0.6 we have introduced support to cache results with Redis! | ||
Just add redis to the avatar me config and it will create a new redis client. | ||
```javascript | ||
var avatarMe = require('./index.js') | ||
avatarMe.configure({ | ||
defaultAvatar: 'mySuperAwesomeDefaultAvatar.png', | ||
defaultAvatarPath: 'http://my/super/awesome/path/to/default/images/', | ||
cache: { | ||
host: '127.0.0.1', | ||
port: '6379' | ||
} | ||
}) | ||
avatarMe.fetchAvatar('jorge@ferreiro.me', 'jorge', (err, avatar) => { | ||
console.log(err) | ||
console.log(avatar) | ||
}) | ||
``` | ||
## Contribute! | ||
* Bugs, Pull Requests or feature requests? Go here! [avatar-me Github repository](https://github.com/ferreiro/avatar-me/issues) | ||
* Or... Send me an email jorge [AT] ferreiro [DOT] me |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7876
7
146
60
5
+ Addedredis@^2.7.1
+ Addeddouble-ended-queue@2.1.0-0(transitive)
+ Addedredis@2.8.0(transitive)
+ Addedredis-commands@1.7.0(transitive)
+ Addedredis-parser@2.6.0(transitive)