1
0

twitter_concern.rb 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. module TwitterConcern
  2. extend ActiveSupport::Concern
  3. included do
  4. include Oauthable
  5. validate :validate_twitter_options
  6. valid_oauth_providers 'twitter'
  7. gem_dependency_check { defined?(Twitter) && has_oauth_configuration_for?('twitter') }
  8. end
  9. def validate_twitter_options
  10. unless twitter_consumer_key.present? &&
  11. twitter_consumer_secret.present? &&
  12. twitter_oauth_token.present? &&
  13. twitter_oauth_token_secret.present?
  14. errors.add(:base, "Twitter consumer_key, consumer_secret, oauth_token, and oauth_token_secret are required to authenticate with the Twitter API. You can provide these as options to this Agent, or as Credentials with the same names, but starting with 'twitter_'.")
  15. end
  16. end
  17. def twitter_consumer_key
  18. ENV['TWITTER_OAUTH_KEY']
  19. end
  20. def twitter_consumer_secret
  21. ENV['TWITTER_OAUTH_SECRET']
  22. end
  23. def twitter_oauth_token
  24. service.token
  25. end
  26. def twitter_oauth_token_secret
  27. service.secret
  28. end
  29. def twitter
  30. Twitter::REST::Client.new do |config|
  31. config.consumer_key = twitter_consumer_key
  32. config.consumer_secret = twitter_consumer_secret
  33. config.access_token = twitter_oauth_token
  34. config.access_token_secret = twitter_oauth_token_secret
  35. end
  36. end
  37. module ClassMethods
  38. def twitter_dependencies_missing
  39. "## Include the `twitter`, `omniauth-twitter`, and `cantino-twitter-stream` gems in your Gemfile to use Twitter Agents."
  40. end
  41. end
  42. end