Gravatar

Displays Gravatar image for any email address

Explanation

No custom JavaScript was written for this demo. It uses the data-reflex attribute to produce the behavior.

Code (12 LOC)

ERB (4 LOC)
app/views/gravatars/_demo.html.erb (4 LOC)
<input id="email" type="text" placeholder="Enter an email address..."
class="form-control form-control-lg d-inline-block col-4"
data-reflex="input->GravatarReflex#perform">
<span class="ml-3"><%= image_tag @gravatar_image_url || "" %></span>
Ruby (8 LOC)
app/reflexes/gravatar_reflex.rb (8 LOC)
class GravatarReflex < ApplicationReflex
def perform
email = element[:value]
return unless %r{[^@]+@[^\.]+\..+}.match?(email)
email_md5 = Digest::MD5.hexdigest(email.downcase.strip)
@gravatar_image_url = "https://www.gravatar.com/avatar/#{email_md5}"
end
end