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