Skip to content

Instantly share code, notes, and snippets.

@midned
Created September 25, 2012 21:37
Show Gist options
  • Save midned/3784611 to your computer and use it in GitHub Desktop.
Save midned/3784611 to your computer and use it in GitHub Desktop.
Gravatar in Coffeescript
Gravy =
"HTTP_URL": "http://www.gravatar.com/avatar/"
"HTTPS_URL": "https://secure.gravatar.com/avatar/"
valid: (email) ->
/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i.test email
to: (email, size, https) ->
# MD5 (Message-Digest Algorithm) by WebToolkit
# You must include the function to use it
gravatar_id = md5 email
size = if size then "?s=#{size}" else ""
url = if https then @HTTPS_URL else @HTTP_URL
url + gravatar_id + size
to_secure: (email, size) ->
@to email, size, true
img: (email, size) ->
url = @to email, size
"<img src='#{url}' />"
img_secure: (email, size) ->
url = @to email, size, true
"<img src='#{url}' />"
email = "[email protected]"
if Gravy.valid email
console.log "Gravy.to(email) " + Gravy.to email
console.log "Gravy.to(email, 50) " + Gravy.to email, 50
console.log "Gravy.to(email, undefined, true) " + Gravy.to email, undefined, true
console.log "Gravy.to(email, 50, true) " + Gravy.to email, 50, true
console.log "Gravy.img(email)" + Gravy.img email
console.log "Gravy.img_secure(email)" + Gravy.img_secure email
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment