How can I display Gravtar using Middleman?

241 Views Asked by At

I'm attempting to build my first ruby gem that uses Middleman Frontmatter to display Gravatar images.

My goal is to use frontmatter like this as an input:

---
email: [email protected]
---

...and output the associated Gravatar image in a .erb file like this:

<%= gravatar_for(article.data.email) %>

Here is what I have come up with based on the instructions provided by Gravatar for displaying images via ruby:

require "gravatar/version"
require 'digest/md5'

module Gravatar

  def gravatar_for(email)
    hash = Digest::MD5.hexdigest(email.chomp.downcase)
    "http://www.gravatar.com/avatar/#{hash}"
  end

end

The error I receive when the code is run is

NoMethodError at/
undefined method `gravatar_for'...

The gem and repo can be found here: https://rubygems.org/gems/snagagrav

3

There are 3 best solutions below

0
On

I assume you've successfully tried adding something like this to your config.rb:

require 'digest/md5'
class Gravatar < Middleman::Extension
  def initialize(app, options_hash={}, &block)
    super
    puts "#{options_hash.inspect}"
  end

  helpers do
    def make_a_link(url, text)
      "<a href='#{url}'>#{text}</a>"
    end

    def gravatar_for(email)
      hash = Digest::MD5.hexdigest(email.chomp.downcase)
      "http://www.gravatar.com/avatar/#{hash}"
    end
  end
end

::Middleman::Extensions.register(:gravatar, Gravatar)

set :css_dir, 'stylesheets'

set :js_dir, 'javascripts'

set :images_dir, 'images'

# Build-specific configuration
configure :build do
  activate :gravatar
end

Then in your template:

---
title: Welcome to Middleman
email: [email protected]
---

<div class="welcome">
  <h1>Middleman is Watching</h1>
  <p class="doc">
    <%= gravatar_for(current_page.data.email) %>
  </p><!-- .doc -->
</div><!-- .welcome -->

That works for me. Seems like you need to turn your gem into a MiddleMan extension and register it.

0
On

Figured it out! Here is what I did:

Created a gem called middleman-gravatar.rb in directory middleman-gravatar/lib/

class MiddlemanGravatar < ::Middleman::Extension

  helpers do
    def gravatar_for(email, options_hash={})
      if email
        # Creates md5 hash for email address
        hash = Digest::MD5.hexdigest(email.chomp.downcase)
        # Returns Gravatar image for email (.jpg)
        "<img src='http://www.gravatar.com/avatar/#{hash}.jpg' />"
      else
        # Returns default mysterman image if no Gravatar exists
        "<img src='http://www.gravatar.com/avatar/?d=mm' />"
      end
    end
  end

end

::Middleman::Extensions.register(:middleman_gravatar, MiddlemanGravatar)

added this to the config.rb file

# Gravatar
require('middleman-gravatar/lib/middleman-gravatar')
activate :middleman_gravatar

added this to the index.html.erb to show the Gravatar

<%= gravatar_for(article.data.gravatar) %>

and added this to the frontmatter of the article file

---
gravatar: [email protected]
---
0
On

The easiest way to do this doesn't involve making your own Middleman extension:

Just put this in config.rb:

require 'digest/md5'
helpers do
  def gravatar_for(email)
    hash = Digest::MD5.hexdigest(email.chomp.downcase)
    "http://www.gravatar.com/avatar/#{hash}"
  end
end

And now in templates you can do:

<%= gravatar_for(article.data.email) %>