Monthly Archives: March 2014

Ruby Koala Gem to Interact with Facebook

Today I started playing around with the Koala gem:

Koala is a Facebook library for Ruby, supporting the Graph API (including the batch requests and photo uploads), the REST API, realtime updates, test users, and OAuth validation.

To install the gem just:

gem install koala
Or put it in the Gemfile and use the bundle command ‘bundle install’:
gem "koala", "~> 1.8.0rc1"
Before we can connect to Facebook, we need to create an access token for the authentication, go on the following link:
And create your key with user_groups and user_photos privileges.
Here the code to post to specific groups, substitute the key with the one you got in the previous step:
#!/usr/bin/env ruby
require 'koala'
oauth_access_token = 'xxxxxxxxxxxxxxxxxx'
group_filtering_words = ['gangofruby', 'gangofrubyII']
image_path = 'ruby.jpg' #change to your image path
message = 'Ruby is the most beautiful gem.' # your message
graph =
# getting groups of interest
groups = graph.get_connections("me", "groups").select do |group|
  group_filtering_words.any? do |word|
    group["name"].downcase.include? word
groups.each_with_index { |group,i|
  puts "[#{i+1}/#{groups.size}] Posting to group #{group["name"]}."
  graph.put_picture( image_path, {message: message}, group['id'])
If it is returning an SSL error like:
/lib/ruby/2.0.0/net/http.rb:918:in `connect': SSL_connect returned=1 errno=0 state=SSLv3 
read server certificate B: certificate verify failed (Faraday::SSLError)
Then you need to download the cacert.pem file from 
Save the file to C:\RailsInstaller\cacert.pem and add it to your system as system variable:

Tagged , , ,

Ruby Gravatar Link generator

Just a simple example of Ruby code to generate a gravatar link, substitute the with the desired address and you will get the something like:

In the second use case you can specify the size of the gravatar, the default image in case no gravatar is found and the rating:


#!/usr/bin/env ruby
require 'digest/md5'
require 'uri'
# this is based on gravatar image API
# options :
# size : <integer> size of image
# default: <string> url of image if email not found or:
# * 404
# * mm
# * identicon
# * monsterid
# * wavatar
# * retro
# * blank
# forcedefault: "y" force default image to load
# rating: <string> one of the values : g, pg, r, x
def gravatar email, options={}
  email_md5 = Digest::MD5.hexdigest email
  params_query = URI.encode_www_form(options) unless options.empty?
puts gravatar('')
puts gravatar('',
              size: 200,
              default: '404',
              rating: 'pg'
Tagged , ,