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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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)
  oauth_options = @config.auth_options[:oauth]

  options = {
    :path   => '/1.1/statuses/filter.json',
    :params => filter_params,
    :oauth  => {
      :consumer_key     => oauth_options[:consumer_key],
      :consumer_secret  => oauth_options[:consumer_secret],
      :token            => oauth_options[:access_key],
      :token_secret     => oauth_options[:access_secret]
    }
  }

  Weeter.logger.info("Connecting to Twitter stream...")
  @client = EM::Twitter::Client.connect(options)
  @client.each 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

  @client.on_unauthorized do |msg|
    Weeter.logger.debug("on_unauthorized: #{msg}")
  end

  @client.on_forbidden do |msg|
    Weeter.logger.debug("on_forbidden: #{msg}")
  end

  @client.on_not_found do |msg|
    Weeter.logger.debug("on_not_found: #{msg}")
  end

  @client.on_not_acceptable do |msg|
    Weeter.logger.debug("on_not_acceptable: #{msg}")
  end

  @client.on_too_long do |msg|
    Weeter.logger.debug("on_too_long: #{msg}")
  end

  @client.on_range_unacceptable do |msg|
    Weeter.logger.debug("on_range_unacceptable: #{msg}")
  end

  @client.on_enhance_your_calm do |msg| # rate-limited
    Weeter.logger.debug("on_enhance_your_calm: #{msg}")
  end

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

  @client.on_reconnect do |msg|
    Weeter.logger.debug("on_reconnect: #{msg}")
  end

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

#reconnect(filter_params) ⇒ Object



100
101
102
103
104
# File 'lib/weeter/twitter/tweet_consumer.rb', line 100

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