Class: LesserEvil::TweetController
- Inherits:
-
Object
- Object
- LesserEvil::TweetController
- Defined in:
- lib/lesser_evil/tweet_controller.rb
Instance Attribute Summary collapse
-
#batch_qty ⇒ Object
Returns the value of attribute batch_qty.
-
#candidate ⇒ Object
Returns the value of attribute candidate.
-
#fast_print ⇒ Object
Returns the value of attribute fast_print.
-
#is_intense ⇒ Object
Returns the value of attribute is_intense.
-
#result ⇒ Object
Returns the value of attribute result.
-
#sentiment ⇒ Object
Returns the value of attribute sentiment.
-
#tweet_qty ⇒ Object
Returns the value of attribute tweet_qty.
Instance Method Summary collapse
- #get_batch(candidate, is_intense, max_id = nil) ⇒ Object
- #get_print_tweets ⇒ Object
- #get_sentiment(text) ⇒ Object
-
#initialize(options) ⇒ TweetController
constructor
A new instance of TweetController.
Constructor Details
#initialize(options) ⇒ TweetController
Returns a new instance of TweetController.
5 6 7 8 9 10 11 12 13 |
# File 'lib/lesser_evil/tweet_controller.rb', line 5 def initialize() @result = [] @candidate = [:candidate] @is_intense = [:is_intense] @sentiment = [:sentiment] || "Negative" @fast_print = [:fast_print] == nil ? true : [:fast_print] @tweet_qty = [:tweet_qty] || LesserEvil::DEFAULT_TWEET_QTY @batch_qty = [:batch_qty] || LesserEvil::DEFAULT_BATCH_QTY end |
Instance Attribute Details
#batch_qty ⇒ Object
Returns the value of attribute batch_qty.
3 4 5 |
# File 'lib/lesser_evil/tweet_controller.rb', line 3 def batch_qty @batch_qty end |
#candidate ⇒ Object
Returns the value of attribute candidate.
3 4 5 |
# File 'lib/lesser_evil/tweet_controller.rb', line 3 def candidate @candidate end |
#fast_print ⇒ Object
Returns the value of attribute fast_print.
3 4 5 |
# File 'lib/lesser_evil/tweet_controller.rb', line 3 def fast_print @fast_print end |
#is_intense ⇒ Object
Returns the value of attribute is_intense.
3 4 5 |
# File 'lib/lesser_evil/tweet_controller.rb', line 3 def is_intense @is_intense end |
#result ⇒ Object
Returns the value of attribute result.
3 4 5 |
# File 'lib/lesser_evil/tweet_controller.rb', line 3 def result @result end |
#sentiment ⇒ Object
Returns the value of attribute sentiment.
3 4 5 |
# File 'lib/lesser_evil/tweet_controller.rb', line 3 def sentiment @sentiment end |
#tweet_qty ⇒ Object
Returns the value of attribute tweet_qty.
3 4 5 |
# File 'lib/lesser_evil/tweet_controller.rb', line 3 def tweet_qty @tweet_qty end |
Instance Method Details
#get_batch(candidate, is_intense, max_id = nil) ⇒ Object
15 16 17 18 |
# File 'lib/lesser_evil/tweet_controller.rb', line 15 def get_batch(candidate,is_intense,max_id = nil) response = HTTParty.get("#{LesserEvil::BASE_TWITTER_URL}?q=#{LesserEvil::SEARCH_TERMS[candidate.to_sym]}&count=#{@batch_qty}&max_id=#{max_id}", headers: {"Authorization" => LesserEvil::APP_AUTH}) response["statuses"] end |
#get_print_tweets ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/lesser_evil/tweet_controller.rb', line 26 def get_print_tweets Whirly.configure spinner: "bouncingBar", remove_after_stop: true, stop: LesserEvil::SEPARATOR Whirly.start max_id = nil while @result.length < @tweet_qty batch = get_batch(@candidate,@is_intense,max_id) max_id = batch.last["id"] - 1 index = 0 while index < batch.length && @result.length < @tweet_qty status = batch[index] # Filter out retweets if status["retweeted_status"] == nil || (!@result.collect {|tweet_slim| tweet_slim.orig_id}.include?(status["retweeted_status"]["id"]) && !@result.collect {|tweet_slim| tweet_slim.orig_id}.include?(status["id"])) sentiment_analysis = get_sentiment(status["text"]) intensity = @is_intense ? 1 : 0 if sentiment_analysis["sentiment"] == @sentiment && sentiment_analysis["confidence"].to_f >= 75 * intensity && @result.length < @tweet_qty tweet_slim = TweetSlim.new(@candidate,status) Whirly.stop tweet_slim.prettyprint if @fast_print @result << tweet_slim Whirly.start end end index += 1 end end Whirly.stop @result end |
#get_sentiment(text) ⇒ Object
20 21 22 23 24 |
# File 'lib/lesser_evil/tweet_controller.rb', line 20 def get_sentiment(text) = { body: { txt: text }} response = HTTParty.post(LesserEvil::SENTIMENT_URL, ) response["result"] end |