Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

avatar-me

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

avatar-me - npm Package Compare versions

Comparing version 0.0.5 to 0.1.0

lib/Cache.js

50

lib/AvatarMe.js

@@ -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
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc