Monthly Archives: March 2014

Ruby Koala Gem to Interact with Facebook

Today I started playing around with the Koala gem:

https://github.com/arsduo/koala

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:
https://developers.facebook.com/tools/explorer
And create your key with user_groups and user_photos privileges.
image
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 = Koala::Facebook::API.new(oauth_access_token)
 
# getting groups of interest
groups = graph.get_connections("me", "groups").select do |group|
  group_filtering_words.any? do |word|
    group["name"].downcase.include? word
  end
end
 
 
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 http://curl.haxx.se/ca/cacert.pem. 
Save the file to C:\RailsInstaller\cacert.pem and add it to your system as system variable:
SSL_CERT_FILE=C:\RailsInstaller\cacert.pem 



		                		
Tagged , , ,

Ruby Gravatar Link generator

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

http://www.gravatar.com/avatar/d02770a3574d9ed0ee364c49d5fb14d3?

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:

http://www.gravatar.com/avatar/d02770a3574d9ed0ee364c49d5fb14d3?size=200&default=https%3A%2F%2Fimage-not-found-link.jpg&rating=pg

 

#!/usr/bin/env ruby
require 'digest/md5'
require 'uri'
 
# this is based on gravatar image API
# https://en.gravatar.com/site/implement/images/
# 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?
  "http://www.gravatar.com/avatar/#{email_md5}?#{params_query}"
end
 
 
puts gravatar('xxxx@gmail.com')
puts gravatar('xxxx@gmail.com',
              size: 200,
              default: '404',
              rating: 'pg'
     )
Tagged , ,