Class: TwitterToCsv::TwitterWatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/twitter_to_csv/twitter_watcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ TwitterWatcher

Returns a new instance of TwitterWatcher.



7
8
9
10
11
12
13
14
# File 'lib/twitter_to_csv/twitter_watcher.rb', line 7

def initialize(options)
  @api_key = options[:api_key]
  @api_secret = options[:api_secret]
  @access_token = options[:access_token]
  @access_token_secret = options[:access_token_secret]
  @filter = options[:filter]
  @fetch_errors = 0
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



5
6
7
# File 'lib/twitter_to_csv/twitter_watcher.rb', line 5

def access_token
  @access_token
end

#access_token_secretObject

Returns the value of attribute access_token_secret.



5
6
7
# File 'lib/twitter_to_csv/twitter_watcher.rb', line 5

def access_token_secret
  @access_token_secret
end

#api_keyObject

Returns the value of attribute api_key.



5
6
7
# File 'lib/twitter_to_csv/twitter_watcher.rb', line 5

def api_key
  @api_key
end

#api_secretObject

Returns the value of attribute api_secret.



5
6
7
# File 'lib/twitter_to_csv/twitter_watcher.rb', line 5

def api_secret
  @api_secret
end

#fetch_errorsObject

Returns the value of attribute fetch_errors.



5
6
7
# File 'lib/twitter_to_csv/twitter_watcher.rb', line 5

def fetch_errors
  @fetch_errors
end

#filterObject

Returns the value of attribute filter.



5
6
7
# File 'lib/twitter_to_csv/twitter_watcher.rb', line 5

def filter
  @filter
end

Instance Method Details

#handle_status(status, block) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/twitter_to_csv/twitter_watcher.rb', line 59

def handle_status(status, block)
  return unless status
  return if status.has_key?('delete')
  return unless status['text']
  status['text'] = status['text'].gsub(/&lt;/, "<").gsub(/&gt;/, ">").gsub(/[\t\n\r]/, '  ')
  block.call(status)
end

#progress(str) ⇒ Object



16
17
18
19
20
21
# File 'lib/twitter_to_csv/twitter_watcher.rb', line 16

def progress(str)
  STDERR.print "#{str}..."
  STDERR.flush
  yield
  STDERR.puts "done."
end

#run(&block) ⇒ 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
# File 'lib/twitter_to_csv/twitter_watcher.rb', line 23

def run(&block)
  while true
    EventMachine::run do
      stream = Twitter::JSONStream.connect(
        :path    => "/1/statuses/#{(filter && filter.length > 0) ? 'filter' : 'sample'}.json#{"?track=#{filter.map {|f| CGI::escape(f) }.join(",")}" if filter && filter.length > 0}",
        :ssl     => true,
        :oauth   => {
          :consumer_key    => api_key,
          :consumer_secret => api_secret,
          :access_key      => access_token,
          :access_secret   => access_token_secret
        }
      )

      stream.each_item do |item|
        handle_status JSON.parse(item), block
      end

      stream.on_error do |message|
        STDERR.puts " --> Twitter error: #{message} <--"
      end

      stream.on_no_data do |message|
        STDERR.puts " --> Got no data for awhile; trying to reconnect."
        EventMachine::stop_event_loop
      end

      stream.on_max_reconnects do |timeout, retries|
        STDERR.puts " --> Oops, tried too many times! <--"
        EventMachine::stop_event_loop
      end
    end
    puts " --> Reconnecting..."
  end
end