Class: Weeter::Twitter::TweetConsumer

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/weeter/twitter/tweet_consumer.rb

Constant Summary collapse

TRACK_LIMIT =
400
FOLLOW_LIMIT =
5000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(twitter_config, notifier, limiter) ⇒ TweetConsumer

Returns a new instance of TweetConsumer.



17
18
19
20
21
# File 'lib/weeter/twitter/tweet_consumer.rb', line 17

def initialize(twitter_config, notifier, limiter)
  @config = twitter_config
  @notifier = notifier
  @limiter = limiter
end

Instance Attribute Details

#limiterObject (readonly)

Returns the value of attribute limiter.



11
12
13
# File 'lib/weeter/twitter/tweet_consumer.rb', line 11

def limiter
  @limiter
end

#notifierObject (readonly)

Returns the value of attribute notifier.



11
12
13
# File 'lib/weeter/twitter/tweet_consumer.rb', line 11

def notifier
  @notifier
end

Instance Method Details

#connect(filter_params) ⇒ Object



23
24
25
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
55
56
57
58
59
60
61
62
# File 'lib/weeter/twitter/tweet_consumer.rb', line 23

def connect(filter_params)
  filter_params = limit_filter_params(filter_params)
  filter_params = clean_filter_params(filter_params)


  connect_options = {
    ssl:    true,
    params: filter_params,
    method: 'POST'
  }.merge(@config.auth_options)

  Weeter.logger.info("Connecting to Twitter stream...")
  @stream = ::Twitter::JSONStream.connect(connect_options)

  @stream.each_item do |item|
    begin
      tweet_item = TweetItem.new(MultiJson.decode(item))

      if tweet_item.limit_notice?
        notify_missed_tweets(tweet_item)
      elsif tweet_item.deletion?
        delete_tweet(tweet_item)
      elsif tweet_item.publishable?
        publish_or_rate_limit(tweet_item)
      else
        ignore_tweet(tweet_item)
      end
    rescue => ex
      Weeter.logger.error("Twitter stream tweet exception: #{ex.class.name}: #{ex.message} #{tweet_item.to_json}")
    end
  end

  @stream.on_error do |msg|
    Weeter.logger.error("Twitter stream error: #{msg}. Connect options were #{connect_options.inspect}")
  end

  @stream.on_max_reconnects do |timeout, retries|
    Weeter.logger.error("Twitter stream max-reconnects reached: timeout=#{timeout}, retries=#{retries}")
  end
end

#reconnect(filter_params) ⇒ Object



64
65
66
67
68
# File 'lib/weeter/twitter/tweet_consumer.rb', line 64

def reconnect(filter_params)
  @stream.stop
  @stream.unbind
  connect(filter_params)
end