Skip to content
Browse files

Support passing client up for on_direct_messsage

  • Loading branch information...
1 parent b0721f2 commit 0f74bc22d67aaa66a18d1b6d7ef2414274f0ecab @coreyhaines coreyhaines committed Sep 24, 2011
Showing with 31 additions and 14 deletions.
  1. +8 −2 lib/tweetstream/client.rb
  2. +23 −12 spec/tweetstream/client_spec.rb
View
10 lib/tweetstream/client.rb
@@ -258,7 +258,6 @@ def start(path, query_parameters = {}, &block) #:nodoc:
}.merge(auth_params).merge(extra_stream_parameters)
EventMachine::run {
-
@stream = Twitter::JSONStream.connect(stream_params)
@stream.each_item do |item|
begin
@@ -281,7 +280,14 @@ def start(path, query_parameters = {}, &block) #:nodoc:
limit_proc.call(hash[:limit][:track]) if limit_proc.is_a?(Proc)
elsif hash[:direct_message]
- direct_message_proc.call(TweetStream::DirectMessage.new(hash[:direct_message])) if direct_message_proc.is_a?(Proc)
+ if direct_message_proc.is_a?(Proc)
+ case direct_message_proc.arity
+ when 1
+ direct_message_proc.call(TweetStream::DirectMessage.new(hash[:direct_message]))
+ when 2
+ direct_message_proc.call(TweetStream::DirectMessage.new(hash[:direct_message]), self)
+ end
+ end
elsif hash[:text] && hash[:user]
@last_status = TweetStream::Status.new(hash)
View
35 spec/tweetstream/client_spec.rb
@@ -123,18 +123,29 @@
end.track('abc')
end
- it 'should call the on_direct_message if specified' do
- direct_message = sample_direct_messages[0]
- direct_message["direct_message"]["id"] = 1234
- direct_message["direct_message"]["sender"]["screen_name"] = "coder"
- @stream.should_receive(:each_item).and_yield(direct_message.to_json)
- yielded_dm = nil
- @client.on_direct_message do |dm|
- yielded_dm = dm
- end.user_stream
- yielded_dm.should_not be_nil
- yielded_dm.id.should == 1234
- yielded_dm.user.screen_name.should == "coder"
+ context "calling on_direct_message" do
+ it 'yields a DirectMessage' do
+ direct_message = sample_direct_messages[0]
+ direct_message["direct_message"]["id"] = 1234
+ direct_message["direct_message"]["sender"]["screen_name"] = "coder"
+ @stream.should_receive(:each_item).and_yield(direct_message.to_json)
+ yielded_dm = nil
+ @client.on_direct_message do |dm|
+ yielded_dm = dm
+ end.user_stream
+ yielded_dm.should_not be_nil
+ yielded_dm.id.should == 1234
+ yielded_dm.user.screen_name.should == "coder"
+ end
+
+ it 'yields itself if block has an arity of 2' do
+ @stream.should_receive(:each_item).and_yield(sample_direct_messages[0].to_json)
+ yielded_client = nil
+ @client.on_direct_message do |_, client|
+ yielded_client = client
+ end.user_stream
+ yielded_client.should == @client
+ end
end
it 'should call on_error if a non-hash response is received' do

0 comments on commit 0f74bc2

Please sign in to comment.
Something went wrong with that request. Please try again.