Code Monkey home page Code Monkey logo

gravatar's Introduction

The Ultimate Gravatar Gem!

This gem is used to interface with the entire Gravatar API: it’s not just for generating image URLs, but for connecting to and communicating with the XML-RPC API too! Additionally, it can be used to download the Gravatar image data itself, rather than just a URL to that data. This saves you the extra step of having to do so.

Installation

Only Ruby 2.0 is officially supported. Rails itself is optional, but if used, only versions 3.1, 3.2, and 4.0+ are supported. For older versions of Ruby and/or Rails, see v1.x of this gem.

In your Gemfile:

gem 'gravatar-ultimate'

Usage

Using the gem is actually pretty simple. Let’s say you want the Gravatar image URL for “[email protected]”:

url = Gravatar.new("[email protected]").image_url

Cool, huh? Let’s take it a step further and grab the actual image data so that we can render it on the screen:

data = Gravatar.new("[email protected]").image_data

Fine, but how about the rest of the API as advertised at en.gravatar.com/site/implement/xmlrpc? Well, for that you need either the user’s Gravatar password, or their API key:

api = Gravatar.new("[email protected]", :password => "helloworld")
api = Gravatar.new("[email protected]", :api_key => "AbCdEfG1234")

After you have that, things get a lot easier:

api.exists?                                   #=> true or false, depending on whether this user has an account.
api.addresses                                 #=> a list of email addresses and their corresponding images
api.save_data!(rating, image_data)            #=> saves an image to this user's account and returns a handle to it
api.use_user_image!(handle, an_email_address) #=> uses the specified image handle for the specified email address(es)
api.exists?("[email protected]")            #=> true or false, depending on whether the specified email exists.

Caching

As you can see this is quite powerful. But it gets better. Gravatar Ultimate even manages caching of API responses for you! That way, if an error occurs, (such as the Gravatar site being offline), your code won’t break. It’ll instead gracefully fall back to the cached copy! By default, if you are using Rails, it’ll use the Rails cache. Otherwise, it’ll use whatever cache you’re using with Gravatar (by default an instance of ActiveSupport::Cache::FileStore).

This has obvious benefits when used for the API calls that do not result in changing the user’s profile, but what you might not have thought of yet is that it also caches #image_data, so you can hook your application up to that method without fear of what might happen to all those Gravatar images if the Gravatar server should be unavailable!

To customize exactly which cache is used, see the next section…

Configuration

To see settings and options you can give for a particular Gravatar instance, check out the Gravatar class documentation. There are a few things you can set for Gravatar on a system-wide basis, and that’s what we’ll go over next.

For a non-Rails project, simply set these options before you start using Gravatar. For a Rails project, you should set them within an Initializer in config/initializers/any_filename.rb in order to ensure that the settings are applied (A) after Gravatar has been included into the project, and (B) before it is actually used by Rails.

# You can set the default cache for Gravatar to use:
Gravatar.cache = ActiveSupport::Cache::SynchronizedMemoryStore.new

# You can also set the length of time an item in the Gravatar cache is valid. Default is 24.hours
Gravatar.duration = 20.minutes

# You can also change the logger used by default. It's worth mentioning that, once again, Gravatar will use
# the Rails logger if it's available. Otherwise, the default is $stdout.
grav_log = ""
Gravatar.logger = StringIO.new(grav_log) # logs Gravatar output to a String

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Don’t update the version number. (If you want to have your own version, that’s fine but bump version in a commit by itself I can ignore when I pull.)

  • Send me a pull request. Bonus points for topic branches.

Copyright © 2010-2012 Colin MacKenzie IV. See LICENSE for details.

Contributors

github.com/sinisterchipmunk/gravatar/contributors

Also:

abitdodgy (Mohamad El-Husseini) for github.com/sinisterchipmunk/gravatar/pull/7

gravatar's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gravatar's Issues

Errno::ENAMETOOLONG: File name too lon

in rails console

url = Gravatar.new("[email protected]").image_data
Errno::ENAMETOOLONG: File name too long @ rb_sysopen - /home/tomi/rubyrubyruby/github/company/app_name/tmp/cache/gravatar-f6efe2ad4860e6774104c88114ad9dec-none%2Fhttp%3A%2F%2Fwww.gravatar.com%2Favatar%2Ff6efe2ad4860e6774104c88114ad9dec20140801-14666-1868pl1

ruby 2.1.1
rails 4.0.2 (I know I should upgrade :( )
gravatar-ultimate (2.0.0)

Set default image if user doesn't have Gravatar

I did not see anywhere in your docs how to override the ugly blue "G" that gravatar serves if the user email is not in the system. Can I check if the default image is going to come back and if so, serve a local image as a default instead of the blue "G"?

Dependency not specificed

gravatar-ultimate requires rack to run. If you don't have it installed, it will produce an error:

LoadError: cannot load such file -- rack/utils
    from /Users/joe/.rbenv/versions/1.9.3-p448/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /Users/joe/.rbenv/versions/1.9.3-p448/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /Users/joe/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/cache/file_store.rb:4:in `<top (required)>'
    from /Users/joe/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/gravatar-ultimate-1.0.3/lib/gravatar/cache.rb:101:in `default_cache_instance'
    from /Users/joe/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/gravatar-ultimate-1.0.3/lib/gravatar/cache.rb:109:in `cache'
    from /Users/joe/.rbenv/versions/1.9.3-p448/

Best solution would probably be to include it as a dependency in the gemfile

Readme gem install typo!

Hey there,

I was just getting going with it, and copied and pasted from the readme for my Gemfile, but it couldn't find the gem. I realized it's just spelled wrong –

gem install gravitar-ultimate

should be

gem install gravatar-ultimate

gravatar broken on ruby 1.9 rails 2.3.14

this line in dependencies.rb is incorrect:

  Rails.configuration.gem "sc-core-ext", ">= 1.2.0"

this should be (I think):

Rails.configuration.gem "sc-core-ext", :version => ">= 1.2.0"

the bad code ends up triggering "sc-core-ext"[:requirement], which harmlessly returns nil in ruby 1.8, but throws in ruby 1.9.

https?

More of a feature request - can the gem bring avatars in via https ?

You should mention the options somewhere

I had to take a look at the source to find out I could pass :secure as an option to image_url. Maybe you could mention that somewhere in the Wiki? It might make life a little bit easier for users. Not a big deal at all, of course.

XMLRPC requirement

Howdy,

I believe the newer versions of Ruby have removed xmlrpc support. It's available as a gem though. Could the xmlrpc gem be added as a dependency of this gem?

Thanks!

undefined method 'cache' for Rails::Module when using Sinatra

This is the erorr i get when i try to do this using Sinatra framework

 Gravatar.new(current_user.email).image_url(:ssl => true)

NoMethodError - undefined method cache' for Rails:Module: gems/gravatar-ultimate-2.0.0/lib/gravatar/cache.rb:101:indefault_cache_instance'
gems/gravatar-ultimate-2.0.0/lib/gravatar/cache.rb:109:in cache' gems/gravatar-ultimate-2.0.0/lib/gravatar.rb:44:ininitialize'

Can this be fixed? or is there any work-around? Please help.

Add the forcedefault param

Adding 'forcedefault' or 'f' allows you to ignore the user's gravatar and force it into one of the options (monsterid, identicon, etc...).

I forked the repo and made what I think are the required changes, but I was not able to install the forked gem, and hence I can't test it to see if it works. I've only been doing Ruby and RoR for three days...

More of the f option can be found here: http://en.gravatar.com/site/implement/images/ under "Force Default"

API doesn't need to be passed hashed emails

I think the api has changed, and no longer needs a hashed array of emails. For example, the following code works now:

def use_user_image!(image_hash, emails)
emails = [emails] unless emails.is_a?(Array)
hash = call('grav.useUserimage', :userimage => image_hash, :addresses => emails)
expire_cache!
return hash
end

I would have submitted a pull request, but I didn't understand your 'dehashify_emails' method. So I've just hacked it up for now.

How to: check if gravatar exists for email in Ruby

The Story

I have a mailer that will include a gravatar. I return to this awesome gem once again. I want a custom default icon to appear inline and embedded in an html email. Reason for being embedded is because I don't want the user to have to wait to load the resource from heroku if the app is "sleeping". If the device doesn't cache it, it will then be making multiple requests. Having it embedded would also make development easier. When specifying a default image with gravatar and this gem, gravatar makes an http request for your image and forwards it as their own. This is obviously not going to work for me.

The issue

There is no way for me to use this gem to check if a user has a gravatar image or not. Silly. However, I can specify gravatar to return a status header of 404 if there is no gravatar image for the user (or if they are not a user). Hmm...

Searching google

Searching google, I found this snippet. It seemed to work, though I didn't test it thoroughly. Credit goes to Henrik Nyh. http://henrik.nyh.se/2008/11/gravatar-check/

require 'net/http'
require 'digest/md5'
# Is there a Gravatar for this email? Optionally specify :rating and :timeout.
def gravatar?(email, options = {})
  hash = Digest::MD5.hexdigest(email.to_s.downcase)
  options = { :rating => 'x', :timeout => 2 }.merge(options)
  http = Net::HTTP.new('www.gravatar.com', 80)
  http.read_timeout = options[:timeout]
  response = http.request_head("/avatar/#{hash}?rating=#{options[:rating]}&default=http://gravatar.com/avatar")
  response.code != '302'
rescue StandardError, Timeout::Error
  true  # Don't show "no gravatar" if the service is down or slow
end

My solution

This inspired me to come up with my own solution. I choose to use httparty, though I'm sure you can do it without if you choose. https://github.com/jnunemaker/httparty

if HTTParty.get(Gravatar.new(@user.email).image_url :default => 404).code == 404

It's as simple as that. Rework the if statement however you like to fit your situation. If there is no gravatar image, it will return true. Then do whatever you wish.

PS Adding this feature to the gem would be wonderful. ;)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.