Skip to content

Instantly share code, notes, and snippets.

@bhalash
Created November 27, 2015 11:42
Show Gist options
  • Select an option

  • Save bhalash/e34ab38bdfaa53b3badd to your computer and use it in GitHub Desktop.

Select an option

Save bhalash/e34ab38bdfaa53b3badd to your computer and use it in GitHub Desktop.
Avatars

Avatars

Hey!

I have a headache and this is easier than talking. :) I have not added baked-in avatar support to my application. While there are gems like Paperclip, I haven't added any becuase it is more work.

Instead I use Adorable Avatars:

  1. http://avatars.adorable.io/#demo
  2. https://github.com/adorableio/avatars-api

Symbols

Adorable avatars do something along the lines of a Ruby symbol. In Ruby, each string has a different ID. It's a different physical bit of memory:

For example:

irb(main):009:0> 'hello'.object_id
=> 70254151744540
irb(main):010:0> 'hello'.object_id
=> 70254151712680
irb(main):011:0> 'hello'.object_id
=> 70254151690460
irb(main):012:0> 'hello'.object_id
=> 70254151674740

Each new string is potentially different, so each new string is given a new bit of memory. A symbol is only declared once:

irb(main):016:0> :hello.object_id
=> 1088668
irb(main):017:0> :hello.object_id
=> 1088668
irb(main):018:0> :hello.object_id
=> 1088668

Adorable Avatars

Each same symbol will have the same ID. Adorable Avatars works along these lines: If you send the same string (it can be any arbitrary string), you will get the same avatar back, no matter what the string might be.

The simplest way to grab an avatar is to append string and size to their API URL:

http://api.adorable.io/avatars/<integer size>/<arbitrary string>.png

Examples

Examples include:

http://api.adorable.io/avatars/300/1234567.png
http://api.adorable.io/avatars/100/a1dbao2d20d221d12d12d12d12dk.png
http://api.adorable.io/avatars/150/GIANT_ELEPHANTS_LOL.png

I use Adorable Avatar with Rails' image_tag method:

<%= image_tag "http://api.adorable.io/avatars/#{size}/#{user.login_md5}.png", alt: "#{user.nicename}'s nice avatar." %>

The service is a little slow since there is no way to cache requests from my side, and avatars are generated "live" on each request on theirs, but it works! Unique, dependable avatars.

Login MD5

I don't want to send my user's data, however made-up it is, a third-party service. Instead I send an MD5 hash of the user's username/login. The method is at the bottom of my users controller.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment