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 = Koala::Facebook::API.new(token)
 #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 = friends.map{ |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"]
      end
      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
            work=employers.last
            industry = work["employer"].include?("name") ? work["employer"]["name"].titleize
            design = work.include?("position") ? work["position"]["name"].titleize
                                   
           end
        end
   end

      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 = "-"
        end
      end
      
 @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 = Koala::Facebook::API.new(oauth_access_token, 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.get_object('me')
  batch_api.put_wall_post('Making a post in a batch.')
end


#Facebook pages To current user
    user_graph = Koala::Face book::API.new(omniauth['credentials']['token'])
    pages = user_graph.get_connections('me', 'accounts')
#Facebook page Posts
       token = Koala::Facebook::API.new(omniauth['credentials']['token'])
       @graph = Koala::Facebook::API.new(token)
        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)
       end

#Facebook page Reviews
token = Koala::Facebook::API.new(omniauth['credentials']['token'])
page_graph = Koala::Facebook::API.new(token)
rating = page_graph.get_connection('me', 'ratings')
ratings = rating.map{ |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'}

Comments

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

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

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

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

      Delete

Post a Comment

Popular posts from this blog

Rails Migration Difference between Text and String

Rails Migration Difference between Text and String ? While working with Rails Migration Difference between Text and String is important to be known to every developer. Columns and their data types are finalized while deciding Table structure. This tutorial will help understand difference between String and Text column type and illustrate how to write Rails Migration implementing the same. You might want to read about database.yml files for specifying database configuration for Rails Application. 1. Concepts When String or Text data type is required?     Whenever you require your column to store information which is lengthy in size (Many characters), you need to consider String or Text data type for the column.     Both of them let you store Many(How Many - will see later) characters Difference between String and Text Considering MySQL database Feature     String     Text Length     1 to 255     ...

Error malloc(): memory corruption nginx with passenger?

Error malloc(): memory corruption nginx with passenger Passenger issue resolving steps :  sudo gem uninstall passenger(uninstall all passenger) sudo gem install passenger sudo passenger-install-nginx-module --auto --auto-download --prefix=/opt/nginx --extra-configure-flags=none Update nginx config file with new passenger version and restart the nginx

rake db migrate with down, up, redo, rollback options in rails

rake db migrate with down, up, redo, rollback options ? rake db migrate - This can be used to migrate your production/test database using various options like up, down, step, redo, version etc. In this tutorial we will learn how all these options can be used with rake tool to migrate the database. What is rake? rake is basically ruby make. i.e. make tool for ruby It has similar functionality to the make tool that you may have used on unix based systems for comopiling running some kind of script. rake allows you to ruby particular task in the environment that you specify. How to Install rake? You can install rake by installing gem 'rake' as, gem install rake Above command will install the latest version of rake tool avaialable. Various rake db migrate commands Operation     Command     Description General     rake db:migrate     This will migrate your database by running migrations that are not run yet Running specific Migra...