tweetstream 2.3.0 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of tweetstream might be problematic. Click here for more details.

@@ -1,3 +1,5 @@
1
+ require 'spec_helper'
2
+
1
3
  describe TweetStream::Client do
2
4
  before(:each) do
3
5
  TweetStream.configure do |config|
@@ -9,7 +11,7 @@ describe TweetStream::Client do
9
11
  @client = TweetStream::Client.new
10
12
  end
11
13
 
12
- describe '#start' do
14
+ describe "#start" do
13
15
  before do
14
16
  @stream = stub("EM::Twitter::Client",
15
17
  :connect => true,
@@ -27,105 +29,105 @@ describe TweetStream::Client do
27
29
  EM::Twitter::Client.stub!(:connect).and_return(@stream)
28
30
  end
29
31
 
30
- it 'connects if the reactor is already running' do
32
+ it "connects if the reactor is already running" do
31
33
  EM.stub!(:reactor_running?).and_return(true)
32
34
  @client.should_receive(:connect)
33
35
  @client.track('abc')
34
36
  end
35
37
 
36
- it 'starts the reactor if not already running' do
38
+ it "starts the reactor if not already running" do
37
39
  EM.should_receive(:run).once
38
40
  @client.track('abc')
39
41
  end
40
42
 
41
- it 'warns when callbacks are passed as options' do
43
+ it "warns when callbacks are passed as options" do
42
44
  @stream.stub(:each).and_yield(nil)
43
45
  Kernel.should_receive(:warn).with(/Passing callbacks via the options hash is deprecated and will be removed in TweetStream 3.0/)
44
46
  @client.track('abc', :inited => Proc.new { })
45
47
  end
46
48
 
47
- describe '#each' do
48
- it 'calls the appropriate parser' do
49
+ describe "#each" do
50
+ it "calls the appropriate parser" do
49
51
  @client = TweetStream::Client.new
50
- MultiJson.should_receive(:decode).and_return({})
52
+ Yajl::Parser.should_receive(:parse).twice.and_return({})
51
53
  @stream.should_receive(:each).and_yield(sample_tweets[0].to_json)
52
54
  @client.track('abc','def')
53
55
  end
54
56
 
55
- it 'yields a Twitter::Tweet' do
57
+ it "yields a Twitter::Tweet" do
56
58
  @stream.should_receive(:each).and_yield(sample_tweets[0].to_json)
57
- @client.track('abc'){|s| s.should be_kind_of(Twitter::Tweet)}
59
+ @client.track('abc'){|s| expect(s).to be_kind_of(Twitter::Tweet)}
58
60
  end
59
61
 
60
- it 'yields the client if a block with arity 2 is given' do
62
+ it "yields the client if a block with arity 2 is given" do
61
63
  @stream.should_receive(:each).and_yield(sample_tweets[0].to_json)
62
- @client.track('abc'){|s,c| c.should == @client}
64
+ @client.track('abc'){|s,c| expect(c).to eq(@client)}
63
65
  end
64
66
 
65
- it 'includes the proper values' do
67
+ it "includes the proper values" do
66
68
  tweet = sample_tweets[0]
67
69
  tweet[:id] = 123
68
70
  tweet[:user][:screen_name] = 'monkey'
69
71
  tweet[:text] = "Oo oo aa aa"
70
72
  @stream.should_receive(:each).and_yield(tweet.to_json)
71
73
  @client.track('abc') do |s|
72
- s[:id].should == 123
73
- s.user.screen_name.should == 'monkey'
74
- s.text.should == 'Oo oo aa aa'
74
+ expect(s[:id]).to eq(123)
75
+ expect(s.user.screen_name).to eq('monkey')
76
+ expect(s.text).to eq('Oo oo aa aa')
75
77
  end
76
78
  end
77
79
 
78
- it 'calls the on_stall_warning callback if specified' do
80
+ it "calls the on_stall_warning callback if specified" do
79
81
  @stream.should_receive(:each).and_yield(fixture('stall_warning.json'))
80
82
  @client.on_stall_warning do |warning|
81
- warning[:code].should eq('FALLING_BEHIND')
83
+ expect(warning[:code]).to eq('FALLING_BEHIND')
82
84
  end.track('abc')
83
85
  end
84
86
 
85
- it 'calls the on_scrub_geo callback if specified' do
87
+ it "calls the on_scrub_geo callback if specified" do
86
88
  @stream.should_receive(:each).and_yield(fixture('scrub_geo.json'))
87
89
  @client.on_scrub_geo do |up_to_status_id, user_id|
88
- up_to_status_id.should == 9876
89
- user_id.should == 1234
90
+ expect(up_to_status_id).to eq(9876)
91
+ expect(user_id).to eq(1234)
90
92
  end.track('abc')
91
93
  end
92
94
 
93
- it 'calls the on_delete callback' do
95
+ it "calls the on_delete callback" do
94
96
  @stream.should_receive(:each).and_yield(fixture('delete.json'))
95
97
  @client.on_delete do |id, user_id|
96
- id.should == 1234
97
- user_id.should == 3
98
+ expect(id).to eq(1234)
99
+ expect(user_id).to eq(3)
98
100
  end.track('abc')
99
101
  end
100
102
 
101
- it 'calls the on_limit callback' do
103
+ it "calls the on_limit callback" do
102
104
  limit = nil
103
105
  @stream.should_receive(:each).and_yield(fixture('limit.json'))
104
106
  @client.on_limit do |l|
105
107
  limit = l
106
108
  end.track('abc')
107
109
 
108
- limit.should eq(1234)
110
+ expect(limit).to eq(1234)
109
111
  end
110
112
 
111
- it 'calls the on_status_withheld callback' do
113
+ it "calls the on_status_withheld callback" do
112
114
  status = nil
113
115
  @stream.should_receive(:each).and_yield(fixture('status_withheld.json'))
114
116
  @client.on_status_withheld do |s|
115
117
  status = s
116
118
  end.track('abc')
117
119
 
118
- status[:user_id].should eq(123456)
120
+ expect(status[:user_id]).to eq(123456)
119
121
  end
120
122
 
121
- it 'calls the on_user_withheld callback' do
123
+ it "calls the on_user_withheld callback" do
122
124
  status = nil
123
125
  @stream.should_receive(:each).and_yield(fixture('user_withheld.json'))
124
126
  @client.on_user_withheld do |s|
125
127
  status = s
126
128
  end.track('abc')
127
129
 
128
- status[:id].should eq(123456)
130
+ expect(status[:id]).to eq(123456)
129
131
  end
130
132
 
131
133
  context "using on_anything" do
@@ -137,23 +139,23 @@ describe TweetStream::Client do
137
139
  yielded_hash = h
138
140
  end.track('abc')
139
141
 
140
- yielded_hash.should_not be_nil
141
- yielded_hash[:id].should eq(1234)
142
+ expect(yielded_hash).not_to be_nil
143
+ expect(yielded_hash[:id]).to eq(1234)
142
144
  end
143
- it 'yields itself if block has an arity of 2' do
145
+ it "yields itself if block has an arity of 2" do
144
146
  hash = {:id => 1234}
145
147
  @stream.should_receive(:each).and_yield(hash.to_json)
146
148
  yielded_client = nil
147
149
  @client.on_anything do |_, client|
148
150
  yielded_client = client
149
151
  end.track('abc')
150
- yielded_client.should_not be_nil
151
- yielded_client.should == @client
152
+ expect(yielded_client).not_to be_nil
153
+ expect(yielded_client).to eq(@client)
152
154
  end
153
155
  end
154
156
 
155
- context 'using on_timeline_status' do
156
- it 'yields a Status' do
157
+ context "using on_timeline_status" do
158
+ it "yields a Status" do
157
159
  tweet = sample_tweets[0]
158
160
  tweet[:id] = 123
159
161
  tweet[:user][:screen_name] = 'monkey'
@@ -163,24 +165,24 @@ describe TweetStream::Client do
163
165
  @client.on_timeline_status do |status|
164
166
  yielded_status = status
165
167
  end.track('abc')
166
- yielded_status.should_not be_nil
167
- yielded_status[:id].should == 123
168
- yielded_status.user.screen_name.should == 'monkey'
169
- yielded_status.text.should == 'Oo oo aa aa'
168
+ expect(yielded_status).not_to be_nil
169
+ expect(yielded_status[:id]).to eq(123)
170
+ expect(yielded_status.user.screen_name).to eq('monkey')
171
+ expect(yielded_status.text).to eq('Oo oo aa aa')
170
172
  end
171
- it 'yields itself if block has an arity of 2' do
173
+ it "yields itself if block has an arity of 2" do
172
174
  @stream.should_receive(:each).and_yield(sample_tweets[0].to_json)
173
175
  yielded_client = nil
174
176
  @client.on_timeline_status do |_, client|
175
177
  yielded_client = client
176
178
  end.track('abc')
177
- yielded_client.should_not be_nil
178
- yielded_client.should == @client
179
+ expect(yielded_client).not_to be_nil
180
+ expect(yielded_client).to eq(@client)
179
181
  end
180
182
  end
181
183
 
182
- context 'using on_direct_message' do
183
- it 'yields a DirectMessage' do
184
+ context "using on_direct_message" do
185
+ it "yields a DirectMessage" do
184
186
  direct_message = sample_direct_messages[0]
185
187
  direct_message[:direct_message][:id] = 1234
186
188
  direct_message[:direct_message][:sender][:screen_name] = "coder"
@@ -189,63 +191,63 @@ describe TweetStream::Client do
189
191
  @client.on_direct_message do |dm|
190
192
  yielded_dm = dm
191
193
  end.userstream
192
- yielded_dm.should_not be_nil
193
- yielded_dm.id.should == 1234
194
- yielded_dm.sender.screen_name.should == "coder"
194
+ expect(yielded_dm).not_to be_nil
195
+ expect(yielded_dm.id).to eq(1234)
196
+ expect(yielded_dm.sender.screen_name).to eq("coder")
195
197
  end
196
198
 
197
- it 'yields itself if block has an arity of 2' do
199
+ it "yields itself if block has an arity of 2" do
198
200
  @stream.should_receive(:each).and_yield(sample_direct_messages[0].to_json)
199
201
  yielded_client = nil
200
202
  @client.on_direct_message do |_, client|
201
203
  yielded_client = client
202
204
  end.userstream
203
- yielded_client.should == @client
205
+ expect(yielded_client).to eq(@client)
204
206
  end
205
207
  end
206
208
 
207
- it 'should call on_error if a non-hash response is received' do
209
+ it "calls on_error if a non-hash response is received" do
208
210
  @stream.should_receive(:each).and_yield('["favorited"]')
209
211
  @client.on_error do |message|
210
- message.should == 'Unexpected JSON object in stream: ["favorited"]'
212
+ expect(message).to eq('Unexpected JSON object in stream: ["favorited"]')
211
213
  end.track('abc')
212
214
  end
213
215
 
214
- it 'should call on_error if a json parse error occurs' do
216
+ it "calls on_error if a json parse error occurs" do
215
217
  @stream.should_receive(:each).and_yield("{'a_key':}")
216
218
  @client.on_error do |message|
217
- message.should == "MultiJson::DecodeError occured in stream: {'a_key':}"
219
+ expect(message).to eq("Yajl::ParseError occured in stream: {'a_key':}")
218
220
  end.track('abc')
219
221
  end
220
222
  end
221
223
 
222
- describe '#on_error' do
223
- it 'should pass the message on to the error block' do
224
+ describe "#on_error" do
225
+ it "passes the message on to the error block" do
224
226
  @stream.should_receive(:on_error).and_yield('Uh oh')
225
227
  @client.on_error do |m|
226
- m.should == 'Uh oh'
228
+ expect(m).to eq('Uh oh')
227
229
  end.track('abc')
228
230
  end
229
231
 
230
- it 'should return the block when defined' do
232
+ it "returns the block when defined" do
231
233
  @client.on_error { |m| true; }
232
- @client.on_error.should be_kind_of(Proc)
234
+ expect(@client.on_error).to be_kind_of(Proc)
233
235
  end
234
236
 
235
- it 'should return nil when undefined' do
236
- @client.on_error.should be_nil
237
+ it "returns nil when undefined" do
238
+ expect(@client.on_error).to be_nil
237
239
  end
238
240
  end
239
241
 
240
- describe '#on_max_reconnects' do
241
- it 'should raise a ReconnectError' do
242
+ describe "#on_max_reconnects" do
243
+ it "raises a ReconnectError" do
242
244
  @stream.should_receive(:on_max_reconnects).and_yield(30, 20)
243
- lambda{ @client.track('abc') }.should raise_error(TweetStream::ReconnectError, "Failed to reconnect after 20 tries.")
245
+ expect(lambda{ @client.track('abc') }).to raise_error(TweetStream::ReconnectError, "Failed to reconnect after 20 tries.")
244
246
  end
245
247
  end
246
248
  end
247
249
 
248
- describe 'API methods' do
250
+ describe "API methods" do
249
251
  %w(firehose retweet sample links).each do |method|
250
252
  it "##{method} should make a call to start with \"statuses/#{method}\"" do
251
253
  @client.should_receive(:start).once.with('/1.1/statuses/' + method + '.json', {})
@@ -253,58 +255,58 @@ describe TweetStream::Client do
253
255
  end
254
256
  end
255
257
 
256
- describe '#filter' do
257
- it 'makes a call to "statuses/filter" with the query params provided' do
258
+ describe "#filter" do
259
+ it "makes a call to 'statuses/filter' with the query params provided" do
258
260
  @client.should_receive(:start).once.with('/1.1/statuses/filter.json', :follow => 123, :method => :post)
259
261
  @client.filter(:follow => 123)
260
262
  end
261
- it 'makes a call to "statuses/filter" with the query params provided longitude/latitude pairs, separated by commas ' do
263
+ it "makes a call to 'statuses/filter' with the query params provided longitude/latitude pairs, separated by commas " do
262
264
  @client.should_receive(:start).once.with('/1.1/statuses/filter.json', :locations => '-122.75,36.8,-121.75,37.8,-74,40,-73,41', :method => :post)
263
265
  @client.filter(:locations => '-122.75,36.8,-121.75,37.8,-74,40,-73,41')
264
266
  end
265
267
  end
266
268
 
267
- describe '#follow' do
268
- it 'makes a call to start with "statuses/filter" and a follow query parameter' do
269
+ describe "#follow" do
270
+ it "makes a call to start with 'statuses/filter' and a follow query parameter" do
269
271
  @client.should_receive(:start).once.with('/1.1/statuses/filter.json', :follow => [123], :method => :post)
270
272
  @client.follow(123)
271
273
  end
272
274
 
273
- it 'comma-joins multiple arguments' do
275
+ it "comma-joins multiple arguments" do
274
276
  @client.should_receive(:start).once.with('/1.1/statuses/filter.json', :follow => [123,456], :method => :post)
275
277
  @client.follow(123, 456)
276
278
  end
277
279
  end
278
280
 
279
- describe '#locations' do
280
- it 'should call #start with "statuses/filter" with the query params provided longitude/latitude pairs' do
281
+ describe "#locations" do
282
+ it "calls #start with 'statuses/filter' with the query params provided longitude/latitude pairs" do
281
283
  @client.should_receive(:start).once.with('/1.1/statuses/filter.json', :locations => ['-122.75,36.8,-121.75,37.8,-74,40,-73,41'], :method => :post)
282
284
  @client.locations('-122.75,36.8,-121.75,37.8,-74,40,-73,41')
283
285
  end
284
286
 
285
- it 'should call #start with "statuses/filter" with the query params provided longitude/latitude pairs and additional filter' do
287
+ it "calls #start with 'statuses/filter' with the query params provided longitude/latitude pairs and additional filter" do
286
288
  @client.should_receive(:start).once.with('/1.1/statuses/filter.json', :locations => ['-122.75,36.8,-121.75,37.8,-74,40,-73,41'], :track => 'rock', :method => :post)
287
289
  @client.locations('-122.75,36.8,-121.75,37.8,-74,40,-73,41', :track => 'rock')
288
290
  end
289
291
  end
290
292
 
291
- describe '#track' do
292
- it 'makes a call to start with "statuses/filter" and a track query parameter' do
293
+ describe "#track" do
294
+ it "makes a call to start with 'statuses/filter' and a track query parameter" do
293
295
  @client.should_receive(:start).once.with('/1.1/statuses/filter.json', :track => ['test'], :method => :post)
294
296
  @client.track('test')
295
297
  end
296
298
 
297
- it 'comma-joins multiple arguments' do
299
+ it "comma-joins multiple arguments" do
298
300
  @client.should_receive(:start).once.with('/1.1/statuses/filter.json', :track => ['foo', 'bar', 'baz'], :method => :post)
299
301
  @client.track('foo', 'bar', 'baz')
300
302
  end
301
303
 
302
- it 'comma-joins an array of arguments' do
304
+ it "comma-joins an array of arguments" do
303
305
  @client.should_receive(:start).once.with('/1.1/statuses/filter.json', :track => [['foo','bar','baz']], :method => :post)
304
306
  @client.track(['foo','bar','baz'])
305
307
  end
306
308
 
307
- it 'should call #start with "statuses/filter" and the provided queries' do
309
+ it "calls #start with 'statuses/filter' and the provided queries" do
308
310
  @client.should_receive(:start).once.with('/1.1/statuses/filter.json', :track => ['rock'], :method => :post)
309
311
  @client.track('rock')
310
312
  end
@@ -313,40 +315,40 @@ describe TweetStream::Client do
313
315
 
314
316
  %w(on_delete on_limit on_inited on_reconnect on_no_data_received on_unauthorized on_enhance_your_calm).each do |proc_setter|
315
317
  describe "##{proc_setter}" do
316
- it 'should set when a block is given' do
318
+ it "sets when a block is given" do
317
319
  proc = Proc.new{|a,b| puts a }
318
320
  @client.send(proc_setter, &proc)
319
- @client.send(proc_setter).should == proc
321
+ expect(@client.send(proc_setter)).to eq(proc)
320
322
  end
321
323
 
322
- it 'should return nil when undefined' do
323
- @client.send(proc_setter).should be_nil
324
+ it "returns nil when undefined" do
325
+ expect(@client.send(proc_setter)).to be_nil
324
326
  end
325
327
  end
326
328
  end
327
329
 
328
- describe '#stop' do
329
- it 'should call EventMachine::stop_event_loop' do
330
+ describe "#stop" do
331
+ it "calls EventMachine::stop_event_loop" do
330
332
  EventMachine.should_receive :stop_event_loop
331
- TweetStream::Client.new.stop.should be_nil
333
+ expect(TweetStream::Client.new.stop).to be_nil
332
334
  end
333
335
 
334
- it 'should return the last status yielded' do
336
+ it "returns the last status yielded" do
335
337
  EventMachine.should_receive :stop_event_loop
336
338
  client = TweetStream::Client.new
337
339
  client.send(:instance_variable_set, :@last_status, {})
338
- client.stop.should == {}
340
+ expect(client.stop).to eq({})
339
341
  end
340
342
  end
341
343
 
342
- describe '#close_connection' do
343
- it 'should not call EventMachine::stop_event_loop' do
344
+ describe "#close_connection" do
345
+ it "does not call EventMachine::stop_event_loop" do
344
346
  EventMachine.should_not_receive :stop_event_loop
345
- TweetStream::Client.new.close_connection.should be_nil
347
+ expect(TweetStream::Client.new.close_connection).to be_nil
346
348
  end
347
349
  end
348
350
 
349
- describe '#stop_stream' do
351
+ describe "#stop_stream" do
350
352
  before(:each) do
351
353
  @stream = stub("EM::Twitter::Client",
352
354
  :connect => true,
@@ -366,12 +368,12 @@ describe TweetStream::Client do
366
368
  @client.connect('/')
367
369
  end
368
370
 
369
- it "should call stream.stop to cleanly stop the current stream" do
371
+ it "calls stream.stop to cleanly stop the current stream" do
370
372
  @stream.should_receive(:stop)
371
373
  @client.stop_stream
372
374
  end
373
375
 
374
- it 'should not stop eventmachine' do
376
+ it "does not stop eventmachine" do
375
377
  EventMachine.should_not_receive :stop_event_loop
376
378
  @client.stop_stream
377
379
  end
@@ -27,7 +27,7 @@ describe TweetStream::Client do
27
27
  end
28
28
 
29
29
  describe "User Stream support" do
30
- context 'when calling #userstream' do
30
+ context "when calling #userstream" do
31
31
  it "sends the userstream host" do
32
32
  EM::Twitter::Client.should_receive(:connect).with(hash_including(:host => "userstream.twitter.com")).and_return(@stream)
33
33
  @client.userstream
@@ -38,35 +38,35 @@ describe TweetStream::Client do
38
38
  @client.userstream
39
39
  end
40
40
 
41
- it 'supports "replies"' do
41
+ it "supports :replies => 'all'" do
42
42
  @client.should_receive(:start).once.with("/1.1/user.json", hash_including(:replies => 'all')).and_return(@stream)
43
43
  @client.userstream(:replies => 'all')
44
44
  end
45
45
 
46
- it 'supports "stall_warnings"' do
46
+ it "supports :stall_warnings => 'true'" do
47
47
  @client.should_receive(:start).once.with("/1.1/user.json", hash_including(:stall_warnings => 'true')).and_return(@stream)
48
48
  @client.userstream(:stall_warnings => 'true')
49
49
  end
50
50
 
51
- it 'supports "with followings"' do
51
+ it "supports :with => 'followings'" do
52
52
  @client.should_receive(:start).once.with("/1.1/user.json", hash_including(:with => 'followings')).and_return(@stream)
53
53
  @client.userstream(:with => 'followings')
54
54
  end
55
55
 
56
- it 'supports "with user"' do
56
+ it "supports :with => 'user'" do
57
57
  @client.should_receive(:start).once.with("/1.1/user.json", hash_including(:with => 'user')).and_return(@stream)
58
58
  @client.userstream(:with => 'user')
59
59
  end
60
60
 
61
- it 'supports event callbacks' do
61
+ it "supports event callbacks" do
62
62
  event = nil
63
63
  @stream.should_receive(:each).and_yield(fixture('favorite.json'))
64
64
  @client.on_event(:favorite) do |e|
65
65
  event = e
66
66
  end.userstream
67
67
 
68
- event[:source].should_not be_nil
69
- event[:target].should_not be_nil
68
+ expect(event[:source]).not_to be_nil
69
+ expect(event[:target]).not_to be_nil
70
70
  end
71
71
  end
72
72
  end