Aucune description

twitter.rb 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. module Oauth
  2. class Twitter
  3. attr_reader :access_token, :formatted_account_info, :twitter_account
  4. def initialize params
  5. @client = TwitterOAuth::Client.new( consumer_key: ENV['TWITTER_KEY'], consumer_secret: ENV['TWITTER_SECRET'])
  6. @params = params
  7. if params[:oauth_token] && params[:oauth_verifier]
  8. get_access_token
  9. elsif params[:twitter_token]
  10. get_account
  11. format_account_info
  12. end
  13. end
  14. def request_token
  15. if @request_token.blank?
  16. @token = @client.request_token(oauth_callback: "#{ENV['ANGULAR_SITE_URL']}")
  17. $redis.set(@token.token, @token.secret)
  18. @request_token = @token.token
  19. else
  20. @request_token
  21. end
  22. end
  23. def get_access_token
  24. @access_token = @client.authorize(
  25. @params[:oauth_token], $redis.get(@params[:oauth_token]),
  26. oauth_verifier: @params[:oauth_verifier]
  27. )
  28. $redis.set(@access_token.token, @access_token.secret)
  29. @access_token = @access_token.token
  30. end
  31. def get_account
  32. @twitter_account = TwitterOAuth::Client.new(
  33. consumer_key: ENV['TWITTER_KEY'],
  34. consumer_secret: ENV['TWITTER_SECRET'],
  35. token: @params[:twitter_token],
  36. secret: $redis.get(@params[:twitter_token])
  37. )
  38. end
  39. def authorized?
  40. @authorized ||= @twitter_account.authorized?
  41. end
  42. def format_account_info
  43. twitter_profile = @twitter_account.info
  44. begin
  45. website = twitter_profile["entities"]["url"]["urls"][0]["expanded_url"]
  46. rescue
  47. end
  48. @formatted_account_info = {
  49. provider: 'twitter',
  50. uid: twitter_profile["id"],
  51. name: twitter_profile["name"],
  52. description: twitter_profile["description"],
  53. website: website,
  54. token: @params[:twitter_token],
  55. secret: $redis.get(@params[:twitter_token]),
  56. image_url: twitter_profile["profile_background_image_url"],
  57. twitter_profile: "http://www.twitter.com/#{twitter_profile['screen_name']}"
  58. }
  59. end
  60. end
  61. end