Skip to main content

Kola Gem Implementaion In Ruby On Rails

Facebook omniauth with kola gem Implementation on Rails 3
1)Gem file add these lines
gem 'koala'

2)Facebook User Details getting

 token = request.env['omniauth.auth']["credentials"]["token"]
   # omniauth = request.env["omniauth.auth"]
   # token = omniauth['credentials']['token']
 @graph =
 #Get The User Details of user
 fb_profile = @graph.get_object("me")
 #Get The Fb user freinds list
 friends = @graph.get_connections("me", "friends")
 firend_ids ={ |friend| friend["id"]}
  image_url = @graph.get_picture(@fb_id)
  image = open(image_url)
      full_name = fb_profile["name"]
      sub_name = fb_profile["first_name"]
      if fb.include?("location")
        location = fb_profile["location"]["name"]
      elsif fb.include?("hometown")
        location = fb_profile["hometown"]["name"]
      city,country = location.split(',').map(&:strip)
       if fb.include?("bio")
        fb_user_content = fb["bio"]
       unless !fb_profile.include?("work")
            employers = fb_profile["work"]
            employers.each do |emp|
            location = "-"
            company_name = emp["employer"].include?("name") ? emp["employer"]["name"].titleize : "-"
            designation = emp.include?("position") ? emp["position"]["name"].titleize : "-"
            start_date  =  emp.include?("start_date") ? emp["start_date"] + "-01" : "-"
            end_date    = emp.include?("end_date") ? emp["end_date"] + "-01" : "-"
            currently_working = emp.include?("end_date") ? false : true
            industry = work["employer"].include?("name") ? work["employer"]["name"].titleize
            design = work.include?("position") ? work["position"]["name"].titleize

      unless !fb_profile.include?("education")
        schools = fb_profile["education"]
        schools.each do |school|
          institute_name = school["school"].include?("name") ? school["school"]["name"].titleize : "-"
          course = school.include?("type") ? school["type"].titleize : "-"
          location = "-"
          start_date = school.include?("year") ? school["year"]["name"] + "-01" + "-01" : "-"
          end_date = "-"
 @graph.put_connections("me", "feed", :message => "I am writing on my wall!")
 @graph.get_connections("me", "mutualfriends/#{friend_id}")
 @graph.put_connections("me", "namespace:action", :object => object_url)
 @graph =, app_secret)

Koala.config.api_version = "v2.0"
# or on a per-request basis
@graph.get_object("me", {}, api_version: "v2.0")

The response of most requests is the JSON data returned from the Facebook servers as a Hash.

When retrieving data that returns an array of results (for example, when calling API#get_connections or API#search) a GraphCollection object will be returned, which makes it easy to page through the results:
feed = @graph.get_connections("me", "feed")
feed.each {|f| do_something_with_item(f) } # it's a subclass of Array
next_feed = feed.next_page

next_page_params = feed.next_page_params
page = @graph.get_page(next_page_params)

You can also make multiple calls at once using Facebooks batch API:

Returns an array of results as if they were called non-batch
@graph.batch do |batch_api|
  batch_api.put_wall_post('Making a post in a batch.')

#Facebook pages To current user
    user_graph = Koala::Face['credentials']['token'])
    pages = user_graph.get_connections('me', 'accounts')
#Facebook page Posts
       token =['credentials']['token'])
       @graph =
        pages = user_graph.get_connections('me', 'accounts')
        pages.each do |x|
        fql_sql = "select actor_id,message,created_time,attachment  from stream where source_id='#{x['id']}' and actor_id != '#{x['id']}' LIMIT 1000"
        page_posts =  @graph.fql_query(fql_sql)

#Facebook page Reviews
token =['credentials']['token'])
page_graph =
rating = page_graph.get_connection('me', 'ratings')
ratings ={ |r| r["reviewer"]["id"]}
rating_fb = rating.collect{|r|[r["reviewer"]["id"],r["rating"],r["review_text"]]}

#Facebook omniauth Implementation
gem 'omniauth'
gem 'omniauth-facebook'
#config/initializers/omniauth.rb(add these lines)
fbapp_key = 'xxxxxxx'
fbsecret_key = 'yyyyyyyyyyyyyyyyy'
config.omniauth :facebook, fbapp_key, fbsecret_key,{:scope => "user_about_me,email",:iframe => true}
config.omniauth :facebook_page_posts, fbapp_key, fbsecret_key,{:scope =>'publish_stream,offline_access,manage_pages' }
config.omniauth :facebook_full_page, fbapp_key, fbsecret_key,{:scope => 'publish_stream,offline_access, manage_pages'}


  1. i am unable to get images in feed using koala gem, Any suggestion?

    1. This comment has been removed by the author.

    2. Hi rakesh
      I will give some piece of code can you check it any issues are there let me know

      image = User.picture("{u["id"]}/picture?type=large")
      user.rb model
      def self.picture(url)
      image_url = url.gsub("=square","=large")
      path = File.join('public/tmp_images', "#{}.jpg")
      r=open(image_url), "wb") { |f| f.write( }

  2. Thank you.Well it was nice post and very helpful information onRuby on Rails Online Course Hyderabad


Post a Comment

Popular posts from this blog

Gem::LoadError (Specified 'mysql2' for database adapter, but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).?

Gem::LoadError (Specified 'mysql2' for database adapter, but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).) In rails?

1)change the gemfile in mysql version
 gem 'mysql2'

2)Change to mysql2 version
 gem 'mysql2','0.3.20'

Active Scaffold : Add radio buttons to your form ruby on rails ?

Active Scaffold : Add radio buttons to your form ruby on rails ?

The way to add radio buttons to your active scaffold create/update from is as shown below


class UsersController < ApplicationController
active_scaffold :user do |config|
config.label = “Users”
config.columns = [:name, :gender]
config.columns[:gender].form_ui = :radio
config.columns[:gender].options[:options] = [['Male', '1'], ['Female','2']]

Get user accessing ip address in rails?

Get user accessing ip address rails?

request.ip ->returns the ip, whether is is a local proxy ip (localhost address) or not.
*request.remote_ip -> is smarter and gets the ip address of the client outside of local proxies.
3)If you are using apache in front of a mongrel, then remote_ip will return the source address of the request, which in this case will be local host because the Apache web server is making the request, so instead put this in your controller:

@remote_ip = request.env["HTTP_X_FORWARDED_FOR"]