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

Create dynamic sitemap on ruby on rails

Sitemaps are an easy way for webmasters to inform search engines about pages on their sites that are available for crawling. In its simplest form, a Sitemap is an XML file that lists URLs for a site along with additional metadata about each URL (when it was last updated, how often it usually changes, and how important it is, relative to other URLs in the site) so that search engines can more intelligently crawl the site. It’s basically a XML file describing all URLs in your page: The following example shows a Sitemap that contains just one URL and uses all optional tags. The optional tags are in italics. <?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">    <url>       <loc>http://www.example.com/</loc>       <lastmod>2005-01-01</lastmod>       <changefreq>monthly</changefreq>     ...

Omniauth Linked in Ruby On Rails

def get_linkedin_user_data      omniauth = request.env["omniauth.auth"]      dat=omniauth.extra.raw_info      linked_app_key = "xxxxxxx"      linkedin_secret_key = "yyyyyyy"      client = LinkedIn::Client.new(linked_app_key,linkedin_secret_key)      client.authorize_from_access(omniauth['credentials']['token'],omniauth['credentials']['secret'])      connections=client.connections(:fields => ["id", "first-name", "last-name","picture-url"])      uid=omniauth['uid']      token=omniauth["credentials"]["token"]      secret=omniauth["credentials"]["secret"]   #linked user data     omniauth = request.env["omniauth.auth"]      data             = omniauth.info      user_name...

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