|
53 | 53 |
|
54 | 54 | describe '#start' do |
55 | 55 | before do |
| 56 | + @stream = stub("Twitter::JSONStream", |
| 57 | + :connect => true, |
| 58 | + :unbind => true, |
| 59 | + :each_item => true, |
| 60 | + :on_error => true, |
| 61 | + :on_max_reconnects => true, |
| 62 | + :connection_completed => true |
| 63 | + ) |
| 64 | + EM.stub!(:run).and_yield |
| 65 | + Twitter::JSONStream.stub!(:connect).and_return(@stream) |
56 | 66 | @client = TweetStream::Client.new('abc','def') |
57 | 67 | end |
58 | | - |
59 | 68 |
|
| 69 | + it 'should try to connect via a JSON stream' do |
| 70 | + Twitter::JSONStream.should_receive(:connect).with( |
| 71 | + :auth => 'abc:def', |
| 72 | + :content => 'track=monday', |
| 73 | + :path => URI.parse('/1/statuses/filter.json'), |
| 74 | + :method => 'POST', |
| 75 | + :user_agent => 'TweetStream' |
| 76 | + ).and_return(@stream) |
| 77 | + |
| 78 | + @client.track('monday') |
| 79 | + end |
| 80 | + |
| 81 | + describe '#each_item' do |
| 82 | + it 'should call the appropriate parser' do |
| 83 | + @client = TweetStream::Client.new('abc','def',:active_support) |
| 84 | + TweetStream::Parsers::ActiveSupport.should_receive(:decode).and_return({}) |
| 85 | + @stream.should_receive(:each_item).and_yield(sample_tweets[0].to_json) |
| 86 | + @client.track('abc','def') |
| 87 | + end |
| 88 | + |
| 89 | + it 'should yield a TweetStream::Status' do |
| 90 | + @stream.should_receive(:each_item).and_yield(sample_tweets[0].to_json) |
| 91 | + @client.track('abc'){|s| s.should be_kind_of(TweetStream::Status)} |
| 92 | + end |
| 93 | + |
| 94 | + it 'should also yield the client if a block with arity 2 is given' do |
| 95 | + @stream.should_receive(:each_item).and_yield(sample_tweets[0].to_json) |
| 96 | + @client.track('abc'){|s,c| c.should == @client} |
| 97 | + end |
| 98 | + |
| 99 | + it 'should include the proper values' do |
| 100 | + tweet = sample_tweets[0] |
| 101 | + tweet[:id] = 123 |
| 102 | + tweet[:user][:screen_name] = 'monkey' |
| 103 | + tweet[:text] = "Oo oo aa aa" |
| 104 | + @stream.should_receive(:each_item).and_yield(tweet.to_json) |
| 105 | + @client.track('abc') do |s| |
| 106 | + s[:id].should == 123 |
| 107 | + s.user.screen_name.should == 'monkey' |
| 108 | + s.text.should == 'Oo oo aa aa' |
| 109 | + end |
| 110 | + end |
| 111 | + |
| 112 | + it 'should call the on_delete if specified' do |
| 113 | + delete = '{ "delete": { "status": { "id": 1234, "user_id": 3 } } }' |
| 114 | + @stream.should_receive(:each_item).and_yield(delete) |
| 115 | + @client.on_delete do |id, user_id| |
| 116 | + id.should == 1234 |
| 117 | + user_id.should == 3 |
| 118 | + end.track('abc') |
| 119 | + end |
| 120 | + |
| 121 | + it 'should call the on_limit if specified' do |
| 122 | + limit = '{ "limit": { "track": 1234 } }' |
| 123 | + @stream.should_receive(:each_item).and_yield(limit) |
| 124 | + @client.on_limit do |track| |
| 125 | + track.should == 1234 |
| 126 | + end.track('abc') |
| 127 | + end |
| 128 | + end |
| 129 | + |
| 130 | + describe '#on_error' do |
| 131 | + it 'should pass the message on to the error block' do |
| 132 | + @stream.should_receive(:on_error).and_yield('Uh oh') |
| 133 | + @client.on_error do |m| |
| 134 | + m.should == 'Uh oh' |
| 135 | + end.track('abc') |
| 136 | + end |
| 137 | + end |
| 138 | + |
| 139 | + describe '#on_max_reconnects' do |
| 140 | + it 'should raise a ReconnectError' do |
| 141 | + @stream.should_receive(:on_max_reconnects).and_yield(30, 20) |
| 142 | + lambda{@client.track('abc')}.should raise_error(TweetStream::ReconnectError) do |e| |
| 143 | + e.timeout.should == 30 |
| 144 | + e.retries.should == 20 |
| 145 | + end |
| 146 | + end |
| 147 | + end |
60 | 148 | end |
61 | 149 |
|
62 | 150 | describe ' API methods' do |
|
0 commit comments