Class: Chirpstream

Inherits:
Object
  • Object
show all
Includes:
Connect
Defined in:
lib/chirpstream.rb,
lib/chirpstream/user.rb,
lib/chirpstream/event.rb,
lib/chirpstream/tweet.rb,
lib/chirpstream/connect.rb,
lib/chirpstream/event/delete.rb,
lib/chirpstream/event/follow.rb,
lib/chirpstream/event/retweet.rb,
lib/chirpstream/event/favorite.rb,
lib/chirpstream/event/unfavorite.rb,
lib/chirpstream/event/direct_message.rb

Defined Under Namespace

Modules: Connect Classes: Delete, DirectMessage, Event, Favorite, Follow, Handlers, Retweet, Tweet, Unfavorite, User

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Connect

#get_connection, #twitter_oauth_access_token, #twitter_oauth_consumer

Constructor Details

#initialize(options = nil) ⇒ Chirpstream

Returns a new instance of Chirpstream.



34
35
36
37
38
39
40
# File 'lib/chirpstream.rb', line 34

def initialize(options = nil)
  @consumer_token = options && options[:consumer_token]
  @consumer_secret = options && options[:consumer_secret]
  @fill_in = options && options[:fill_in]
  @connect_url = "http://chirpstream.twitter.com/2b/user.json"
  @handlers = Handlers.new([], [], [], [], [], [], [], [], [], [])
end

Instance Attribute Details

#consumer_secretObject (readonly)

Returns the value of attribute consumer_secret.



32
33
34
# File 'lib/chirpstream.rb', line 32

def consumer_secret
  @consumer_secret
end

#consumer_tokenObject (readonly)

Returns the value of attribute consumer_token.



32
33
34
# File 'lib/chirpstream.rb', line 32

def consumer_token
  @consumer_token
end

#fill_inObject (readonly)

Returns the value of attribute fill_in.



32
33
34
# File 'lib/chirpstream.rb', line 32

def fill_in
  @fill_in
end

#handlersObject (readonly)

Returns the value of attribute handlers.



28
29
30
# File 'lib/chirpstream.rb', line 28

def handlers
  @handlers
end

Instance Method Details

#connect(*users) ⇒ Object



204
205
206
207
208
209
210
211
212
# File 'lib/chirpstream.rb', line 204

def connect(*users)
  unless EM.reactor_running?
    EM.run { connect(*users) }
  else
    users.each do |user, index|
      connect_single(user)
    end
  end
end

#connect_single(user) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/chirpstream.rb', line 214

def connect_single(user)
  parser = Yajl::Parser.new
  parser.on_parse_complete = data_handler(user)
  http = get_connection(user, @connect_url, :get)
  http.errback { |e, err|
    dispatch_reconnect(user)
    connect
  }
  http.stream { |chunk|
    dispatch_connect(user)
    begin
      parser << chunk
    rescue Yajl::ParseError
      p $!
      puts "bad chunk: #{chunk.inspect}"
    end
  }
end

#data_handler(user) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/chirpstream.rb', line 173

def data_handler(user)
  Proc.new{|parsed_data|
    if parsed_data['direct_message']
      dispatch_direct_message(user, parsed_data)
    elsif parsed_data['friends']
      dispatch_friend(user, parsed_data)
    elsif parsed_data['text']
      dispatch_tweet(user, parsed_data)
    elsif parsed_data['event']
      case parsed_data['event']
      when 'follow'
        dispatch_follow(user, parsed_data)
      when 'favorite'
        dispatch_favorite(user, parsed_data)
      when 'unfavorite'
        dispatch_unfavorite(user, parsed_data)
      when 'retweet'
        dispatch_retweet(user, parsed_data)
      else
        puts "weird event"
        pp parsed_data
      end
    elsif parsed_data['delete']
      dispatch_delete(user, parsed_data)
    else
      puts "i didn't know what to do with this!"
      pp parsed_data
    end
  }
end

#dispatch_connect(user) ⇒ Object



167
168
169
170
171
# File 'lib/chirpstream.rb', line 167

def dispatch_connect(user)
  while h = @handlers.connect.shift
    h.call(user)
  end
end

#dispatch_delete(user, data) ⇒ Object



153
154
155
156
157
158
159
160
# File 'lib/chirpstream.rb', line 153

def dispatch_delete(user, data)
  unless @handlers.delete.empty?
    delete = Delete.new(self, data)
    @fill_in ? delete.load_all(user) { |f|
      @handlers.delete.each{|h| h.call(f, user)}
    } : @handlers.delete.each{|h| h.call(delete, user)}
  end
end

#dispatch_direct_message(user, data) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/chirpstream.rb', line 117

def dispatch_direct_message(user, data)
  unless @handlers.direct_message.empty?
    dm = DirectMessage.new(self, data)
    @fill_in ? dm.load_all(user) { |f|
      @handlers.direct_message.each{|h| h.call(f, user)}
    } : @handlers.direct_message.each{|h| h.call(dm, user)}
  end
end

#dispatch_favorite(user, data) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/chirpstream.rb', line 126

def dispatch_favorite(user, data)
  unless @handlers.favorite.empty?
    favorite = Favorite.new(self, data)
    @fill_in ? favorite.load_all(user) { |f|
      @handlers.favorite.each{|h| h.call(f, user)}
    } : @handlers.favorite.each{|h| h.call(favorite, user)}
  end
end

#dispatch_follow(user, data) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/chirpstream.rb', line 108

def dispatch_follow(user, data)
  unless @handlers.follow.empty?
    follow = Follow.new(self, data)
    @fill_in ? follow.load_all(user) { |f|
      @handlers.follow.each{|h| h.call(f, user)}
    } : @handlers.follow.each{|h| h.call(follow, user)}
  end
end

#dispatch_friend(user, data) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/chirpstream.rb', line 82

def dispatch_friend(user, data)
  unless @handlers.friend.empty?
    data['friends'].each_slice(100) do |friend_ids|
      parser = Yajl::Parser.new
      parser.on_parse_complete = proc { |friends|
        friends.each do |friend|
          @handlers.friend.each{|h| h.call(friend)}
        end
      }
      friend_http = get_connection(user, "http://api.twitter.com/1/users/lookup.json", :post, :body => {'user_id' => friend_ids.join(',')})
      friend_http.stream { |chunk|
        parser << chunk
      }
    end
  end
end

#dispatch_reconnect(user) ⇒ Object



162
163
164
165
# File 'lib/chirpstream.rb', line 162

def dispatch_reconnect(user)
  return if @handlers.reconnect.empty?
  @handlers.reconnect.each{|h| h.call(user)}
end

#dispatch_retweet(user, data) ⇒ Object



144
145
146
147
148
149
150
151
# File 'lib/chirpstream.rb', line 144

def dispatch_retweet(user, data)
  unless @handlers.retweet.empty?
    retweet = Retweet.new(self, data)
    @fill_in ? retweet.load_all(user) { |f|
      @handlers.retweet.each{|h| h.call(f, user)}
    } : @handlers.retweet.each{|h| h.call(retweet, user)}
  end
end

#dispatch_tweet(user, data) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/chirpstream.rb', line 99

def dispatch_tweet(user, data)
  unless @handlers.tweet.empty?
    tweet = Tweet.new(self, data)
    @fill_in ? tweet.load_all(user) { |f|
      @handlers.tweet.each{|h| h.call(f, user)}
    } : @handlers.tweet.each{|h| h.call(tweet, user)}
  end
end

#dispatch_unfavorite(user, data) ⇒ Object



135
136
137
138
139
140
141
142
# File 'lib/chirpstream.rb', line 135

def dispatch_unfavorite(user, data)
  unless @handlers.unfavorite.empty?
    unfavorite = Unfavorite.new(self, data)
    @fill_in ? unfavorite.load_all(user) { |f|
      @handlers.unfavorite.each{|h| h.call(f, user)}
    } : @handlers.unfavorite.each{|h| h.call(unfavorite, user)}
  end
end

#on_connect(&block) ⇒ Object



78
79
80
# File 'lib/chirpstream.rb', line 78

def on_connect(&block)
  @handlers.connect << block
end

#on_delete(&block) ⇒ Object



70
71
72
# File 'lib/chirpstream.rb', line 70

def on_delete(&block)
  @handlers.delete << block
end

#on_direct_message(&block) ⇒ Object



66
67
68
# File 'lib/chirpstream.rb', line 66

def on_direct_message(&block)
  @handlers.direct_message << block
end

#on_favorite(&block) ⇒ Object



54
55
56
# File 'lib/chirpstream.rb', line 54

def on_favorite(&block)
  @handlers.favorite << block
end

#on_follow(&block) ⇒ Object



50
51
52
# File 'lib/chirpstream.rb', line 50

def on_follow(&block)
  @handlers.follow << block
end

#on_friend(&block) ⇒ Object



42
43
44
# File 'lib/chirpstream.rb', line 42

def on_friend(&block)
  @handlers.friend << block
end

#on_reconnect(&block) ⇒ Object



74
75
76
# File 'lib/chirpstream.rb', line 74

def on_reconnect(&block)
  @handlers.reconnect << block
end

#on_retweet(&block) ⇒ Object



62
63
64
# File 'lib/chirpstream.rb', line 62

def on_retweet(&block)
  @handlers.retweet << block
end

#on_tweet(&block) ⇒ Object



46
47
48
# File 'lib/chirpstream.rb', line 46

def on_tweet(&block)
  @handlers.tweet << block
end

#on_unfavorite(&block) ⇒ Object



58
59
60
# File 'lib/chirpstream.rb', line 58

def on_unfavorite(&block)
  @handlers.unfavorite << block
end