Permalink
Browse files

Support a on_timeline_status proc

  • Loading branch information...
1 parent 0f74bc2 commit 2a5e1709c3d119f0afdb5c1c22412a85c479bf6b @coreyhaines coreyhaines committed Sep 24, 2011
Showing with 67 additions and 8 deletions.
  1. +39 −7 lib/tweetstream/client.rb
  2. +28 −1 spec/tweetstream/client_spec.rb
View
@@ -199,7 +199,7 @@ def on_error(&block)
# processing of the stream.
#
# @client = TweetStream::Client.new('user','pass')
- # @client.on_error do |direct_message|
+ # @client.on_direct_message do |direct_message|
# # do something with the direct message
# end
#
@@ -216,6 +216,27 @@ def on_direct_message(&block)
end
end
+ # Set a Proc to be run when a regular timeline message is encountered in the
+ # processing of the stream.
+ #
+ # @client = TweetStream::Client.new('user','pass')
+ # @client.on_timeline_message do |status|
+ # # do something with the status
+ # end
+ #
+ # Block can take one or two arguments. |status (, client)|
+ # If no block is given, it will return the currently set
+ # timeline status proc. When a block is given, the TweetStream::Client
+ # object is returned to allow for chaining.
+ def on_timeline_status(&block)
+ if block_given?
+ @on_timeline_status = block
+ self
+ else
+ @on_timeline_status
+ end
+ end
+
# Set a Proc to be run when connection established.
# Called in EventMachine::Connection#post_init
#
@@ -240,6 +261,7 @@ def start(path, query_parameters = {}, &block) #:nodoc:
error_proc = query_parameters.delete(:error) || self.on_error
inited_proc = query_parameters.delete(:inited) || self.on_inited
direct_message_proc = query_parameters.delete(:direct_message) || self.on_direct_message
+ timeline_status_proc = query_parameters.delete(:timeline_status) || self.on_timeline_status
params = normalize_filter_parameters(query_parameters)
@@ -291,14 +313,24 @@ def start(path, query_parameters = {}, &block) #:nodoc:
elsif hash[:text] && hash[:user]
@last_status = TweetStream::Status.new(hash)
-
- # Give the block the option to receive either one
- # or two arguments, depending on its arity.
- case block.arity
+ if timeline_status_proc.is_a?(Proc)
+ case timeline_status_proc.arity
when 1
- yield @last_status
+ timeline_status_proc.call(@last_status)
when 2
- yield @last_status, self
+ timeline_status_proc.call(@last_status, self)
+ end
+ end
+
+ if block_given?
+ # Give the block the option to receive either one
+ # or two arguments, depending on its arity.
+ case block.arity
+ when 1
+ yield @last_status
+ when 2
+ yield @last_status, self
+ end
end
end
end
@@ -123,7 +123,34 @@
end.track('abc')
end
- context "calling on_direct_message" do
+ context 'using on_timeline_status' do
+ it 'yields a Status' do
+ tweet = sample_tweets[0]
+ tweet[:id] = 123
+ tweet[:user][:screen_name] = 'monkey'
+ tweet[:text] = "Oo oo aa aa"
+ @stream.should_receive(:each_item).and_yield(tweet.to_json)
+ yielded_status = nil
+ @client.on_timeline_status do |status|
+ yielded_status = status
+ end.track('abc')
+ yielded_status.should_not be_nil
+ yielded_status[:id].should == 123
+ yielded_status.user.screen_name.should == 'monkey'
+ yielded_status.text.should == 'Oo oo aa aa'
+ end
+ it 'yields itself if block has an arity of 2' do
+ @stream.should_receive(:each_item).and_yield(sample_tweets[0].to_json)
+ yielded_client = nil
+ @client.on_timeline_status do |_, client|
+ yielded_client = client
+ end.track('abc')
+ yielded_client.should_not be_nil
+ yielded_client.should == @client
+ end
+ end
+
+ context 'using on_direct_message' do
it 'yields a DirectMessage' do
direct_message = sample_direct_messages[0]
direct_message["direct_message"]["id"] = 1234

0 comments on commit 2a5e170

Please sign in to comment.