SocialAvatarProxy

This gem acts as a proxy for avatars on Twitter & Facebook.

Build Status Code Climate

Installation

Add this line to your application's Gemfile:

gem "social-avatar-proxy"

And then execute:

$ bundle

Or install it yourself as:

$ gem install social-avatar-proxy

Usage

Rails

In your config/routes.rb file:

mount SocialAvatarProxy::Engine, at: "/avatars"

The engine is basically a proxy for the Rack app, it also adds the path helpers below:

In your views:

# for a Twitter user, by username:
image_tag(twitter_avatar_path("username"))
# by ID:
image_tag(twitter_avatar_path(12345))

# for a Facebook user, by username:
image_tag(facebook_avatar_path("username"))
# by ID:
image_tag(facebook_avatar_path(12345))

You might wish to configure a default image which will be rendered if an avatar could not be found, or if the remote service has an error (e.g. times out or has a redirect loop), this is simple with an initializer:

# choose our image
image_path = Rails.root.join(*%W(app assets images default_avatar.jpg))
# set the config
SocialAvatarProxy.configure do
  default_image(image_path)
end

HTTP caching can be configured as below:

SocialAvatarProxy.configure do
  http_cache do
    expires 10.minutes
    cache_control({
      max_age: 5.days,
      max_stale: 1.day,
      public: true
    })
  end
end

Files can be cached in either or both of Memcached and the file system, here is the configuration options:

SocialAvatarProxy.configure do
  file_cache do
    directory "/path/to/folder"
  end

  memcache do
    enable # only required if you don't set either of the following options
    host "192.168.1.2:11211" # defaults to "localhost:11211"
    namespace "something:" # defaults to "sap/"
  end
end

Rack

The Rack app is available at: SocialAvatarProxy::App.

Both the class and an instance respond to call so either can be mounted.

use SocialAvatarProxy::App
# or
use SocialAvatarProxy::App.new

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request