Class: LesserEvil::TweetController

Inherits:
Object
  • Object
show all
Defined in:
lib/lesser_evil/tweet_controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options)
	@result = []
   @candidate = options[:candidate]
   @is_intense = options[:is_intense]
   @sentiment = options[:sentiment] || "Negative"
   @fast_print = options[:fast_print] == nil ? true : options[:fast_print]
   @tweet_qty = options[:tweet_qty] || LesserEvil::DEFAULT_TWEET_QTY
   @batch_qty = options[:batch_qty] || LesserEvil::DEFAULT_BATCH_QTY
end

Instance Attribute Details

#batch_qtyObject

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

#candidateObject

Returns the value of attribute candidate.



3
4
5
# File 'lib/lesser_evil/tweet_controller.rb', line 3

def candidate
  @candidate
end

#fast_printObject

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_intenseObject

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

#resultObject

Returns the value of attribute result.



3
4
5
# File 'lib/lesser_evil/tweet_controller.rb', line 3

def result
  @result
end

#sentimentObject

Returns the value of attribute sentiment.



3
4
5
# File 'lib/lesser_evil/tweet_controller.rb', line 3

def sentiment
  @sentiment
end

#tweet_qtyObject

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_tweetsObject



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)
	options = { body: { txt: text }}
	response = HTTParty.post(LesserEvil::SENTIMENT_URL, options)
	response["result"]
end