Tag Archives: koala

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 , , ,