/app/models/identity/twitter.rb

https://github.com/bountyhill/bountyhill · Ruby · 126 lines · 72 code · 26 blank · 28 comment · 7 complexity · 8d8566a5d5798d8fdca03b3f54c3cea6 MD5 · raw file

  1. # encoding: UTF-8
  2. require "twitter"
  3. class Identity::Twitter < Identity
  4. include Identity::PolymorphicRouting
  5. include Identity::Provider
  6. with_metrics! "accounts.twitter"
  7. attr_accessible :followed_at
  8. # -- Pseudo attributes ----------------------------------------------
  9. attr :follow_bountyhermes, true
  10. attr_accessible :follow_bountyhermes
  11. def handle
  12. nickname
  13. end
  14. alias_method :screen_name, :handle
  15. #
  16. # consider Twitter API as accessible as long as an oauth token and an oauth secret
  17. # is present since these tokens are needed to access the API and do never expire
  18. def api_accessible?
  19. oauth_token.present? && oauth_secret.present?
  20. end
  21. # -- Twitter actions ------------------------------------------------
  22. # This method makes the user follow @bountyhermes. If the user followed
  23. # @bountyhermes before, this method is a no-op, and the method returns
  24. # false.
  25. def follow
  26. return false if followed_at?
  27. follow!
  28. return true
  29. end
  30. def follow!
  31. followee = Bountybase.config.twitter_notifications["user"]
  32. expect! followee => /^[^@]/
  33. Deferred.twitter(:follow, followee, oauth_hash)
  34. update_attributes :followed_at => Time.now
  35. true
  36. end
  37. #
  38. # post a tweet in user's twitter feed
  39. def post(text, options={})
  40. expect! text => String
  41. expect! options => { :object => [nil, Quest] }
  42. Deferred.twitter(:update, self.class.message(text, options[:object]), oauth_hash)
  43. end
  44. #
  45. # post a tweet in bountyhill's twitter feed
  46. def self.post(text, options={})
  47. expect! text => String
  48. expect! options => { :object => [nil, Quest] }
  49. Deferred.twitter(:update, message(text, options[:object]), oauth_hash)
  50. end
  51. #
  52. # Send a direct message *to this user*.
  53. def direct_message(msg)
  54. hermes = User.hermes.identity(:twitter)
  55. Deferred.twitter(:direct_message_create, handle, msg, hermes.send(:oauth_hash))
  56. end
  57. #
  58. # checks if a valid email address was given
  59. def processable?
  60. return true if self.user && !self.user.new_record?
  61. # clean errors on email
  62. self.errors.delete(:email)
  63. # check if email is given and if so if email is valid
  64. if self.email
  65. self.errors.add(:email, :invalid) unless self.email =~ Identity::Email::EMAIL_ADDRESS_REGEX
  66. else
  67. self.errors.add(:email, :blank)
  68. end
  69. super
  70. end
  71. private
  72. #
  73. # sets up a message text for a tweet
  74. def self.message(text, object=nil)
  75. object.present? ? "#{text} #{object.url}" : text
  76. end
  77. #
  78. # Returns the twitter auth as a Hash. While one might be tempted to
  79. # just use the Identity::Twitter object to pass this around, we'll
  80. # need it in an extra object, because it will be passed into background
  81. # and should not be bound to any ActiveRecord-related objects.
  82. def oauth_hash
  83. {
  84. :consumer_key => consumer_key || Bountybase.config.twitter_app["consumer_key"],
  85. :consumer_secret => consumer_secret || Bountybase.config.twitter_app["consumer_secret"],
  86. :oauth_token => oauth_token,
  87. :oauth_token_secret => oauth_secret
  88. }
  89. end
  90. #
  91. # Returns the applications's twitter auth as a Hash.
  92. def self.oauth_hash
  93. {
  94. :consumer_key => Bountybase.config.twitter_app["consumer_key"],
  95. :consumer_secret => Bountybase.config.twitter_app["consumer_secret"],
  96. :oauth_token => Bountybase.config.twitter_app["oauth_token"],
  97. :oauth_token_secret => Bountybase.config.twitter_app["oauth_secret"]
  98. }
  99. end
  100. end