Class: Hope::Source::Twitter

Inherits:
Base show all
Defined in:
lib/hope/source/twitter.rb

Defined Under Namespace

Classes: Tweet, TwitterUser

Instance Attribute Summary

Attributes inherited from Base

#name, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#parse, #serializable_hash, #to_json

Constructor Details

#initialize(name, opts = {}) ⇒ Twitter

Returns a new instance of Twitter.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/hope/source/twitter.rb', line 86

def initialize(name, opts={})
  
  @logger = Logger.new("tmp/#{name}.json")
  
  EM::PeriodicTimer.new(1) do
    puts "#{@received[:success]} tweets Received from Twitter::#{name}"
  end
  
  @name = name
  @received = { :success => 0, :errors => 0, :latest_error => "" }
  @options = {}.merge(opts)
  
  stream = Twitter::JSONStream.connect(
    :path    => "/1/statuses/filter.json?track=#{@options["track"]}",
    :auth    => "#{@options["login"]}:#{@options["password"]}"
  )

  stream.each_item do |item|
    # @logger.info(item)
    @received[:success] += 1
    tweet = JSON.parse item
    Hope.pub.send_msg name, { "data" => tweet, "type" => "Tweet" }.to_json
  end

  stream.on_error do |message|
    @received[:errors] += 1
    puts "Error: #{message}"
  end

  stream.on_max_reconnects do |timeout, retries|
    @received[:latest_error] = "Max reconnect: #{timeout} || retried #{retries} times..."
  end
  
  Hope::Source.register self
end

Class Method Details

.event_typesObject



82
83
84
# File 'lib/hope/source/twitter.rb', line 82

def self.event_types
  [ TwitterUser, Tweet ]
end