Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. class SessionsController < ApplicationController
  2.  
  3. def index
  4. end
  5.  
  6. def create
  7. user = User.from_omniauth(env["omniauth.auth"])
  8. session[:user_id] = user.id
  9. @client = $client
  10. current_user.provider == "twitter" ? twitter_info : facebook_info
  11. redirect_to root_url
  12. end
  13.  
  14. def destroy
  15. session[:user_id] = nil
  16. redirect_to root_url
  17. end
  18.  
  19. def show_facebook
  20. @profile = Facebookp.find_by_user_id(current_user.id)
  21. @messages = current_user.posts
  22. end
  23.  
  24. def show_twitter
  25. @profile = Twitterp.find_by_user_id(current_user.id)
  26. @tweets = current_user.posts.includes(:user)
  27. @friends = TwitterFriend.includes(:user)
  28. end
  29.  
  30. private
  31.  
  32. def twitter_info
  33. @profile = @client.user(current_user.name).as_json
  34. @tweet = Twitterp.create_with(uid: @profile["id"], name: @profile["name"], screen_name: @profile["screen_name"],
  35. location: @profile["location"], image: @profile["profile_image_url"], link: @profile["profile_location"]["url"], lang: @profile["lang"], created: @profile["status"]["created_at"],
  36. friends_count: @profile["friends_count"], followers_count: @profile["followers_count"], statuses_count: @profile["statuses_count"]).find_or_create_by(user_id: session[:user_id])
  37.  
  38. @friends = @client.friends.as_json
  39. @friends.each do |friend|
  40. unless TwitterFriend.includes(:user).where("uid = ?", friend["id"]).present?
  41. 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"])
  42. end
  43. end
  44.  
  45. @tweets = @client.home_timeline.as_json
  46. @tweets.each do |tweet|
  47. unless Post.includes(:user).where("uid = ?", tweet["id"]).present?
  48. 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"])
  49. end
  50. end
  51. end
  52.  
  53. def facebook_info
  54. @graph = Koala::Facebook::API.new(User.find_by_provider("facebook").oauth_token)
  55. @feeds = @graph.get_object("me?fields=feed").flatten[1].flatten[1]
  56.  
  57. @profile= @graph.get_object("me?fields=location, birthday,id,email,first_name,last_name,gender,link,locale,timezone,name,picture,friends")
  58. @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"],
  59. birthday: @profile["birthday"], gender: @profile["gender"], location: @profile["location"]["name"], image: @profile["picture"]["data"]["url"],
  60. 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])
  61.  
  62. @tweets = @graph.get_object("/me/statuses", "fields"=>"message, from, event")
  63. @tweets.each do |tweet|
  64. unless Post.includes(:twitterp).where("uid = ?", tweet["id"]).present?
  65. 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'])
  66. end
  67. end
  68. end
  69.  
  70. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement