Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SessionsController < ApplicationController
- def index
- end
- def create
- user = User.from_omniauth(env["omniauth.auth"])
- session[:user_id] = user.id
- @client = $client
- current_user.provider == "twitter" ? twitter_info : facebook_info
- redirect_to root_url
- end
- def destroy
- session[:user_id] = nil
- redirect_to root_url
- end
- def show_facebook
- @profile = Facebookp.find_by_user_id(current_user.id)
- @messages = current_user.posts
- end
- def show_twitter
- @profile = Twitterp.find_by_user_id(current_user.id)
- @tweets = current_user.posts.includes(:user)
- @friends = TwitterFriend.includes(:user)
- end
- private
- def twitter_info
- @profile = @client.user(current_user.name).as_json
- @tweet = Twitterp.create_with(uid: @profile["id"], name: @profile["name"], screen_name: @profile["screen_name"],
- location: @profile["location"], image: @profile["profile_image_url"], link: @profile["profile_location"]["url"], lang: @profile["lang"], created: @profile["status"]["created_at"],
- friends_count: @profile["friends_count"], followers_count: @profile["followers_count"], statuses_count: @profile["statuses_count"]).find_or_create_by(user_id: session[:user_id])
- @friends = @client.friends.as_json
- @friends.each do |friend|
- unless TwitterFriend.includes(:user).where("uid = ?", friend["id"]).present?
- TwitterFriend.create(user_id: session[:user_id], twitter_id: @tweet.id, uid: friend["id"], name: friend["name"], screen_name: friend["screen_name"], location: friend["location"])
- end
- end
- @tweets = @client.home_timeline.as_json
- @tweets.each do |tweet|
- unless Post.includes(:user).where("uid = ?", tweet["id"]).present?
- Post.find_or_create_by(user_id: session[:user_id], uid: tweet["id"].to_s, twitter_id: @tweet.id, text: tweet["text"], author: tweet["user"]["name"], created: tweet["created_at"])
- end
- end
- end
- def facebook_info
- @graph = Koala::Facebook::API.new(User.find_by_provider("facebook").oauth_token)
- @feeds = @graph.get_object("me?fields=feed").flatten[1].flatten[1]
- @profile= @graph.get_object("me?fields=location, birthday,id,email,first_name,last_name,gender,link,locale,timezone,name,picture,friends")
- @tweet = Facebookp.create_with(uid: @profile["id"].to_s, email: @profile["email"], name: @profile["name"], first_name: @profile["first_name"], last_name: @profile["last_name"],
- birthday: @profile["birthday"], gender: @profile["gender"], location: @profile["location"]["name"], image: @profile["picture"]["data"]["url"],
- link: @profile["link"], lang: @profile["locale"], count_friends: @profile["friends"]["summary"]["total_count"], timezone: @profile["timezone"]).find_or_create_by(user_id: session[:user_id])
- @tweets = @graph.get_object("/me/statuses", "fields"=>"message, from, event")
- @tweets.each do |tweet|
- unless Post.includes(:twitterp).where("uid = ?", tweet["id"]).present?
- Post.find_or_create_by(user_id: session[:user_id], uid: tweet["id"].to_s, twitter_id: @tweet.id, text: tweet['message'], author: tweet['from']['name'], created: tweet['updated_time'])
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement