Browse Source

Merge pull request #536 from cantino/twitter_is_optional_too

twitter agents can be disabled too
Andrew Cantino 10 years ago
parent
commit
1a512dafae

+ 5 - 3
Gemfile

@@ -15,6 +15,11 @@ gem "google-api-client"           # GoogleCalendarPublishAgent
 gem 'mqtt'                        # MQTTAgent
 gem 'slack-notifier', '~> 0.5.0'  # SlackAgent
 
+# Twitter Agents
+gem 'twitter', '~> 5.8.0' # Must to be loaded before cantino-twitter-stream.
+gem 'cantino-twitter-stream', github: 'cantino/twitter-stream', branch: 'master'
+gem 'omniauth-twitter'
+
 # Optional Services.
 gem 'omniauth-37signals'          # BasecampAgent
 # gem 'omniauth-github'
@@ -31,8 +36,6 @@ gem 'protected_attributes', '~>1.0.8' # This must be loaded before some other ge
 gem 'ace-rails-ap', '~> 2.0.1'
 gem 'bootstrap-kaminari-views', '~> 0.0.3'
 gem 'bundler', '>= 1.5.0'
-gem 'twitter', '~> 5.8.0' # needs to be loaded before cantino-twitter-stream.
-gem 'cantino-twitter-stream', github: 'cantino/twitter-stream', branch: 'master'
 gem 'coffee-rails', '~> 4.0.0'
 gem 'daemons', '~> 1.1.9'
 gem 'delayed_job', '~> 4.0.0'
@@ -59,7 +62,6 @@ gem 'mysql2', '~> 0.3.16'
 gem 'multi_xml'
 gem 'nokogiri', '~> 1.6.1'
 gem 'omniauth'
-gem 'omniauth-twitter'
 gem 'rails' , '4.1.5'
 gem 'rufus-scheduler', '~> 3.0.8', require: false
 gem 'sass-rails',   '~> 4.0.0'

+ 8 - 0
app/concerns/twitter_concern.rb

@@ -6,6 +6,8 @@ module TwitterConcern
 
     validate :validate_twitter_options
     valid_oauth_providers 'twitter'
+
+    gem_dependency_check { defined?(Twitter) && has_oauth_configuration_for?('twitter') }
   end
 
   def validate_twitter_options
@@ -41,4 +43,10 @@ module TwitterConcern
       config.access_token_secret = twitter_oauth_token_secret
     end
   end
+
+  module ClassMethods
+    def twitter_dependencies_missing
+      "## Include the `twitter`, `omniauth-twitter`, and `cantino-twitter-stream` gems in your Gemfile to use Twitter Agents."
+    end
+  end
 end

+ 1 - 2
app/models/agents/twitter_publish_agent.rb

@@ -1,5 +1,3 @@
-require "twitter"
-
 module Agents
   class TwitterPublishAgent < Agent
     include TwitterConcern
@@ -7,6 +5,7 @@ module Agents
     cannot_be_scheduled!
 
     description <<-MD
+      #{twitter_dependencies_missing if dependencies_missing?}
       The TwitterPublishAgent publishes tweets from the events it receives.
 
       To be able to use this Agent you need to authenticate with Twitter in the [Services](/services) section first.

+ 1 - 0
app/models/agents/twitter_stream_agent.rb

@@ -5,6 +5,7 @@ module Agents
     cannot_receive_events!
 
     description <<-MD
+      #{twitter_dependencies_missing if dependencies_missing?}
       The TwitterStreamAgent follows the Twitter stream in real time, watching for certain keywords, or filters, that you provide.
 
       To follow the Twitter stream, provide an array of `filters`.  Multiple words in a filter must all show up in a tweet, but are independent of order.

+ 1 - 2
app/models/agents/twitter_user_agent.rb

@@ -1,5 +1,3 @@
-require "twitter"
-
 module Agents
   class TwitterUserAgent < Agent
     include TwitterConcern
@@ -7,6 +5,7 @@ module Agents
     cannot_receive_events!
 
     description <<-MD
+      #{twitter_dependencies_missing if dependencies_missing?}
       The TwitterUserAgent follows the timeline of a specified Twitter user.
 
       To be able to use this Agent you need to authenticate with Twitter in the [Services](/services) section first.

+ 1 - 0
bin/threaded.rb

@@ -17,6 +17,7 @@ def safely(&block)
   rescue StandardError => e
     STDERR.puts "\nException #{e.message}:\n#{e.backtrace.join("\n")}\n\n"
     STDERR.puts "Terminating myself ..."
+    STDERR.flush
     stop
   end
 end

+ 8 - 1
lib/twitter_stream.rb

@@ -1,6 +1,5 @@
 require 'cgi'
 require 'json'
-require 'twitter/json_stream'
 require 'em-http-request'
 require 'pp'
 
@@ -88,6 +87,14 @@ class TwitterStream
   SEPARATOR = /[^\w_\-]+/
 
   def run
+    if Agents::TwitterStreamAgent.dependencies_missing?
+      STDERR.puts Agents::TwitterStreamAgent.twitter_dependencies_missing
+      STDERR.flush
+      return
+    end
+
+    require 'twitter/json_stream'
+
     while @running
       begin
         agents = Agents::TwitterStreamAgent.active.all