File: tweets_spec.rb

package info (click to toggle)
ruby-twitter 7.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 4,840 kB
  • sloc: ruby: 10,919; makefile: 6
file content (678 lines) | stat: -rw-r--r-- 38,019 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
require 'helper'

describe Twitter::REST::Tweets do
  before do
    @client = Twitter::REST::Client.new(consumer_key: 'CK', consumer_secret: 'CS', access_token: 'AT', access_token_secret: 'AS')
  end

  describe '#retweets' do
    before do
      stub_get('/1.1/statuses/retweets/540897316908331009.json').to_return(body: fixture('retweets.json'), headers: {content_type: 'application/json; charset=utf-8'})
    end
    context 'with a tweet ID passed' do
      it 'requests the correct resource' do
        @client.retweets(540_897_316_908_331_009)
        expect(a_get('/1.1/statuses/retweets/540897316908331009.json')).to have_been_made
      end
      it 'returns up to 100 of the first retweets of a given tweet' do
        tweets = @client.retweets(540_897_316_908_331_009)
        expect(tweets).to be_an Array
        expect(tweets.first).to be_a Twitter::Tweet
        expect(tweets.first.text).to eq("RT @gruber: As for the Series, I'm for the Giants. Fuck Texas, fuck Nolan Ryan, fuck George Bush.")
      end
    end
    context 'with a URI object passed' do
      it 'requests the correct resource' do
        tweet = URI.parse('https://twitter.com/sferik/status/540897316908331009')
        @client.retweets(tweet)
        expect(a_get('/1.1/statuses/retweets/540897316908331009.json')).to have_been_made
      end
    end
    context 'with a Tweet passed' do
      it 'requests the correct resource' do
        tweet = Twitter::Tweet.new(id: 540_897_316_908_331_009)
        @client.retweets(tweet)
        expect(a_get('/1.1/statuses/retweets/540897316908331009.json')).to have_been_made
      end
    end
  end

  describe '#retweeters_of' do
    context 'with ids_only passed' do
      context 'with a tweet ID passed' do
        before do
          stub_get('/1.1/statuses/retweets/540897316908331009.json').to_return(body: fixture('retweets.json'), headers: {content_type: 'application/json; charset=utf-8'})
        end
        it 'requests the correct resource' do
          @client.retweeters_of(540_897_316_908_331_009, ids_only: true)
          expect(a_get('/1.1/statuses/retweets/540897316908331009.json')).to have_been_made
        end
        it 'returns an array of numeric user IDs of retweeters of a Tweet' do
          ids = @client.retweeters_of(540_897_316_908_331_009, ids_only: true)
          expect(ids).to be_an Array
          expect(ids.first).to eq(7_505_382)
        end
      end
    end
    context 'without ids_only passed' do
      before do
        stub_get('/1.1/statuses/retweets/540897316908331009.json').to_return(body: fixture('retweets.json'), headers: {content_type: 'application/json; charset=utf-8'})
      end
      it 'requests the correct resource' do
        @client.retweeters_of(540_897_316_908_331_009)
        expect(a_get('/1.1/statuses/retweets/540897316908331009.json')).to have_been_made
      end
      it 'returns an array of user of retweeters of a Tweet' do
        users = @client.retweeters_of(540_897_316_908_331_009)
        expect(users).to be_an Array
        expect(users.first).to be_a Twitter::User
        expect(users.first.id).to eq(7_505_382)
      end
      context 'with a URI object passed' do
        it 'requests the correct resource' do
          tweet = URI.parse('https://twitter.com/sferik/status/540897316908331009')
          @client.retweeters_of(tweet)
          expect(a_get('/1.1/statuses/retweets/540897316908331009.json')).to have_been_made
        end
      end
      context 'with a Tweet passed' do
        it 'requests the correct resource' do
          tweet = Twitter::Tweet.new(id: 540_897_316_908_331_009)
          @client.retweeters_of(tweet)
          expect(a_get('/1.1/statuses/retweets/540897316908331009.json')).to have_been_made
        end
      end
    end
  end

  describe '#status' do
    before do
      stub_get('/1.1/statuses/show/540897316908331009.json').to_return(body: fixture('status.json'), headers: {content_type: 'application/json; charset=utf-8'})
    end
    it 'requests the correct resource' do
      @client.status(540_897_316_908_331_009)
      expect(a_get('/1.1/statuses/show/540897316908331009.json')).to have_been_made
    end
    it 'returns a Tweet' do
      tweet = @client.status(540_897_316_908_331_009)
      expect(tweet).to be_a Twitter::Tweet
      expect(tweet.text).to eq('Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES')
    end
    context 'with a URI object passed' do
      it 'requests the correct resource' do
        tweet = URI.parse('https://twitter.com/sferik/status/540897316908331009')
        @client.status(tweet)
        expect(a_get('/1.1/statuses/show/540897316908331009.json')).to have_been_made
      end
    end
    context 'with a Tweet passed' do
      it 'requests the correct resource' do
        tweet = Twitter::Tweet.new(id: 540_897_316_908_331_009)
        @client.status(tweet)
        expect(a_get('/1.1/statuses/show/540897316908331009.json')).to have_been_made
      end
    end
  end

  describe '#statuses' do
    before do
      stub_post('/1.1/statuses/lookup.json').with(body: {id: '540897316908331009,91151181040201728'}).to_return(body: fixture('statuses.json'), headers: {content_type: 'application/json; charset=utf-8'})
    end
    it 'requests the correct resource' do
      @client.statuses(540_897_316_908_331_009, 91_151_181_040_201_728)
      expect(a_post('/1.1/statuses/lookup.json').with(body: {id: '540897316908331009,91151181040201728'})).to have_been_made
    end
    it 'returns an array of Tweets' do
      tweets = @client.statuses(540_897_316_908_331_009, 91_151_181_040_201_728)
      expect(tweets).to be_an Array
      expect(tweets.first).to be_a Twitter::Tweet
      expect(tweets.first.text).to eq('Happy Birthday @imdane. Watch out for those @rally pranksters!')
    end
    context 'with URI objects passed' do
      it 'requests the correct resource' do
        @client.statuses(URI.parse('https://twitter.com/sferik/status/540897316908331009'), URI.parse('https://twitter.com/sferik/status/91151181040201728'))
        expect(a_post('/1.1/statuses/lookup.json').with(body: {id: '540897316908331009,91151181040201728'})).to have_been_made
      end
    end
    context 'with Tweets passed' do
      it 'requests the correct resource' do
        @client.statuses(Twitter::Tweet.new(id: 540_897_316_908_331_009), Twitter::Tweet.new(id: 91_151_181_040_201_728))
        expect(a_post('/1.1/statuses/lookup.json').with(body: {id: '540897316908331009,91151181040201728'})).to have_been_made
      end
    end
  end

  describe '#destroy_status' do
    before do
      stub_post('/1.1/statuses/destroy/540897316908331009.json').to_return(body: fixture('status.json'), headers: {content_type: 'application/json; charset=utf-8'})
    end
    it 'requests the correct resource' do
      @client.destroy_status(540_897_316_908_331_009)
      expect(a_post('/1.1/statuses/destroy/540897316908331009.json')).to have_been_made
    end
    it 'returns an array of Tweets' do
      tweets = @client.destroy_status(540_897_316_908_331_009)
      expect(tweets).to be_an Array
      expect(tweets.first).to be_a Twitter::Tweet
      expect(tweets.first.text).to eq('Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES')
    end
    context 'with a URI object passed' do
      it 'requests the correct resource' do
        tweet = URI.parse('https://twitter.com/sferik/status/540897316908331009')
        @client.destroy_status(tweet)
        expect(a_post('/1.1/statuses/destroy/540897316908331009.json')).to have_been_made
      end
    end
    context 'with a Tweet passed' do
      it 'requests the correct resource' do
        tweet = Twitter::Tweet.new(id: 540_897_316_908_331_009)
        @client.destroy_status(tweet)
        expect(a_post('/1.1/statuses/destroy/540897316908331009.json')).to have_been_made
      end
    end
  end

  describe '#update' do
    before do
      stub_post('/1.1/statuses/update.json').with(body: {status: 'Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES'}).to_return(body: fixture('status.json'), headers: {content_type: 'application/json; charset=utf-8'})
    end
    it 'requests the correct resource' do
      @client.update('Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES')
      expect(a_post('/1.1/statuses/update.json').with(body: {status: 'Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES'})).to have_been_made
    end
    it 'returns a Tweet' do
      tweet = @client.update('Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES')
      expect(tweet).to be_a Twitter::Tweet
      expect(tweet.text).to eq('Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES')
    end
    context 'already posted' do
      before do
        stub_post('/1.1/statuses/update.json').to_return(status: 403, body: fixture('already_posted.json'), headers: {content_type: 'application/json; charset=utf-8'})
        stub_get('/1.1/statuses/user_timeline.json').with(query: {count: 1}).to_return(body: fixture('statuses.json'), headers: {content_type: 'application/json; charset=utf-8'})
      end
      it 'requests the correct resources' do
        @client.update('Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES')
        expect(a_post('/1.1/statuses/update.json').with(body: {status: 'Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES'})).to have_been_made
        expect(a_get('/1.1/statuses/user_timeline.json').with(query: {count: 1})).to have_been_made
      end
      it 'returns a Tweet' do
        tweet = @client.update('Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES')
        expect(tweet).to be_a Twitter::Tweet
        expect(tweet.text).to eq('Happy Birthday @imdane. Watch out for those @rally pranksters!')
      end
    end
    context 'with an in-reply-to status' do
      before do
        @tweet = Twitter::Tweet.new(id: 1)
        stub_post('/1.1/statuses/update.json').with(body: {status: 'Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES', in_reply_to_status_id: '1'}).to_return(body: fixture('status.json'), headers: {content_type: 'application/json; charset=utf-8'})
      end
      it 'requests the correct resource' do
        @client.update('Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES', in_reply_to_status: @tweet)
        expect(a_post('/1.1/statuses/update.json').with(body: {status: 'Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES', in_reply_to_status_id: '1'})).to have_been_made
      end
    end
    context 'with an in-reply-to status ID' do
      before do
        stub_post('/1.1/statuses/update.json').with(body: {status: 'Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES', in_reply_to_status_id: '1'}).to_return(body: fixture('status.json'), headers: {content_type: 'application/json; charset=utf-8'})
      end
      it 'requests the correct resource' do
        @client.update('Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES', in_reply_to_status_id: 1)
        expect(a_post('/1.1/statuses/update.json').with(body: {status: 'Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES', in_reply_to_status_id: '1'})).to have_been_made
      end
    end
    context 'with a place' do
      before do
        @place = Twitter::Place.new(woeid: 'df51dec6f4ee2b2c')
        stub_post('/1.1/statuses/update.json').with(body: {status: 'Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES', place_id: 'df51dec6f4ee2b2c'}).to_return(body: fixture('status.json'), headers: {content_type: 'application/json; charset=utf-8'})
      end
      it 'requests the correct resource' do
        @client.update('Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES', place: @place)
        expect(a_post('/1.1/statuses/update.json').with(body: {status: 'Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES', place_id: 'df51dec6f4ee2b2c'})).to have_been_made
      end
    end
    context 'with a place ID' do
      before do
        stub_post('/1.1/statuses/update.json').with(body: {status: 'Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES', place_id: 'df51dec6f4ee2b2c'}).to_return(body: fixture('status.json'), headers: {content_type: 'application/json; charset=utf-8'})
      end
      it 'requests the correct resource' do
        @client.update('Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES', place_id: 'df51dec6f4ee2b2c')
        expect(a_post('/1.1/statuses/update.json').with(body: {status: 'Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES', place_id: 'df51dec6f4ee2b2c'})).to have_been_made
      end
    end
  end

  describe '#update!' do
    before do
      stub_post('/1.1/statuses/update.json').with(body: {status: 'Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES'}).to_return(body: fixture('status.json'), headers: {content_type: 'application/json; charset=utf-8'})
    end
    it 'requests the correct resource' do
      @client.update!('Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES')
      expect(a_post('/1.1/statuses/update.json').with(body: {status: 'Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES'})).to have_been_made
    end
    it 'returns a Tweet' do
      tweet = @client.update!('Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES')
      expect(tweet).to be_a Twitter::Tweet
      expect(tweet.text).to eq('Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES')
    end
    context 'already posted' do
      before do
        stub_post('/1.1/statuses/update.json').to_return(status: 403, body: fixture('already_posted.json'), headers: {content_type: 'application/json; charset=utf-8'})
      end
      it 'raises an DuplicateStatus error' do
        expect { @client.update!('Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES') }.to raise_error(Twitter::Error::DuplicateStatus)
      end
    end
    context 'with an in-reply-to status' do
      before do
        @tweet = Twitter::Tweet.new(id: 1)
        stub_post('/1.1/statuses/update.json').with(body: {status: 'Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES', in_reply_to_status_id: '1'}).to_return(body: fixture('status.json'), headers: {content_type: 'application/json; charset=utf-8'})
      end
      it 'requests the correct resource' do
        @client.update!('Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES', in_reply_to_status: @tweet)
        expect(a_post('/1.1/statuses/update.json').with(body: {status: 'Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES', in_reply_to_status_id: '1'})).to have_been_made
      end
    end
    context 'with an in-reply-to status ID' do
      before do
        stub_post('/1.1/statuses/update.json').with(body: {status: 'Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES', in_reply_to_status_id: '1'}).to_return(body: fixture('status.json'), headers: {content_type: 'application/json; charset=utf-8'})
      end
      it 'requests the correct resource' do
        @client.update!('Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES', in_reply_to_status_id: 1)
        expect(a_post('/1.1/statuses/update.json').with(body: {status: 'Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES', in_reply_to_status_id: '1'})).to have_been_made
      end
    end
    context 'with a place' do
      before do
        @place = Twitter::Place.new(woeid: 'df51dec6f4ee2b2c')
        stub_post('/1.1/statuses/update.json').with(body: {status: 'Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES', place_id: 'df51dec6f4ee2b2c'}).to_return(body: fixture('status.json'), headers: {content_type: 'application/json; charset=utf-8'})
      end
      it 'requests the correct resource' do
        @client.update!('Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES', place: @place)
        expect(a_post('/1.1/statuses/update.json').with(body: {status: 'Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES', place_id: 'df51dec6f4ee2b2c'})).to have_been_made
      end
    end
    context 'with a place ID' do
      before do
        stub_post('/1.1/statuses/update.json').with(body: {status: 'Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES', place_id: 'df51dec6f4ee2b2c'}).to_return(body: fixture('status.json'), headers: {content_type: 'application/json; charset=utf-8'})
      end
      it 'requests the correct resource' do
        @client.update!('Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES', place_id: 'df51dec6f4ee2b2c')
        expect(a_post('/1.1/statuses/update.json').with(body: {status: 'Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES', place_id: 'df51dec6f4ee2b2c'})).to have_been_made
      end
    end
  end

  describe '#retweet' do
    before do
      stub_post('/1.1/statuses/retweet/540897316908331009.json').to_return(body: fixture('retweet.json'), headers: {content_type: 'application/json; charset=utf-8'})
    end
    it 'requests the correct resource' do
      @client.retweet(540_897_316_908_331_009)
      expect(a_post('/1.1/statuses/retweet/540897316908331009.json')).to have_been_made
    end
    it 'returns an array of Tweets with retweet details embedded' do
      tweets = @client.retweet(540_897_316_908_331_009)
      expect(tweets).to be_an Array
      expect(tweets.first).to be_a Twitter::Tweet
      expect(tweets.first.text).to eq("RT @gruber: As for the Series, I'm for the Giants. Fuck Texas, fuck Nolan Ryan, fuck George Bush.")
      expect(tweets.first.retweeted_tweet.text).to eq("As for the Series, I'm for the Giants. Fuck Texas, fuck Nolan Ryan, fuck George Bush.")
      expect(tweets.first.retweeted_tweet.id).not_to eq(tweets.first.id)
    end
    context 'already retweeted' do
      before do
        stub_post('/1.1/statuses/retweet/540897316908331009.json').to_return(status: 403, body: fixture('already_retweeted.json'), headers: {content_type: 'application/json; charset=utf-8'})
      end
      it 'does not raise an error' do
        expect { @client.retweet(540_897_316_908_331_009) }.not_to raise_error
      end
    end
    context 'not found' do
      before do
        stub_post('/1.1/statuses/retweet/540897316908331009.json').to_return(status: 404, body: fixture('not_found.json'), headers: {content_type: 'application/json; charset=utf-8'})
      end
      it 'does not raise an error' do
        expect { @client.retweet(540_897_316_908_331_009) }.not_to raise_error
      end
    end
    context 'with a URI object passed' do
      it 'requests the correct resource' do
        tweet = URI.parse('https://twitter.com/sferik/status/540897316908331009')
        @client.retweet(tweet)
        expect(a_post('/1.1/statuses/retweet/540897316908331009.json')).to have_been_made
      end
    end
    context 'with a Tweet passed' do
      it 'requests the correct resource' do
        tweet = Twitter::Tweet.new(id: 540_897_316_908_331_009)
        @client.retweet(tweet)
        expect(a_post('/1.1/statuses/retweet/540897316908331009.json')).to have_been_made
      end
    end
  end

  describe '#retweet!' do
    before do
      stub_post('/1.1/statuses/retweet/540897316908331009.json').to_return(body: fixture('retweet.json'), headers: {content_type: 'application/json; charset=utf-8'})
    end
    it 'requests the correct resource' do
      @client.retweet!(540_897_316_908_331_009)
      expect(a_post('/1.1/statuses/retweet/540897316908331009.json')).to have_been_made
    end
    it 'returns an array of Tweets with retweet details embedded' do
      tweets = @client.retweet!(540_897_316_908_331_009)
      expect(tweets).to be_an Array
      expect(tweets.first).to be_a Twitter::Tweet
      expect(tweets.first.text).to eq("RT @gruber: As for the Series, I'm for the Giants. Fuck Texas, fuck Nolan Ryan, fuck George Bush.")
      expect(tweets.first.retweeted_tweet.text).to eq("As for the Series, I'm for the Giants. Fuck Texas, fuck Nolan Ryan, fuck George Bush.")
      expect(tweets.first.retweeted_tweet.id).not_to eq(tweets.first.id)
    end
    context 'forbidden' do
      before do
        stub_post('/1.1/statuses/retweet/540897316908331009.json').to_return(status: 403, body: '{}', headers: {content_type: 'application/json; charset=utf-8'})
      end
      it 'raises a Forbidden error' do
        expect { @client.retweet!(540_897_316_908_331_009) }.to raise_error(Twitter::Error::Forbidden)
      end
    end
    context 'already retweeted' do
      before do
        stub_post('/1.1/statuses/retweet/540897316908331009.json').to_return(status: 403, body: fixture('already_retweeted.json'), headers: {content_type: 'application/json; charset=utf-8'})
      end
      it 'raises an AlreadyRetweeted error' do
        expect { @client.retweet!(540_897_316_908_331_009) }.to raise_error(Twitter::Error::AlreadyRetweeted)
      end
    end
    context 'not found' do
      before do
        stub_post('/1.1/statuses/retweet/540897316908331009.json').to_return(status: 404, body: fixture('not_found.json'), headers: {content_type: 'application/json; charset=utf-8'})
      end
      it 'raises a NotFound error' do
        expect { @client.retweet!(540_897_316_908_331_009) }.to raise_error(Twitter::Error::NotFound)
      end
    end
    context 'with a URI object passed' do
      it 'requests the correct resource' do
        tweet = URI.parse('https://twitter.com/sferik/status/540897316908331009')
        @client.retweet!(tweet)
        expect(a_post('/1.1/statuses/retweet/540897316908331009.json')).to have_been_made
      end
    end
    context 'with a Tweet passed' do
      it 'requests the correct resource' do
        tweet = Twitter::Tweet.new(id: 540_897_316_908_331_009)
        @client.retweet!(tweet)
        expect(a_post('/1.1/statuses/retweet/540897316908331009.json')).to have_been_made
      end
    end
  end

  describe '#update_with_media' do
    before do
      stub_post('/1.1/statuses/update.json').to_return(body: fixture('status.json'), headers: {content_type: 'application/json; charset=utf-8'})
      stub_request(:post, 'https://upload.twitter.com/1.1/media/upload.json').to_return(body: fixture('upload.json'), headers: {content_type: 'application/json; charset=utf-8'})
    end
    context 'with a gif image' do
      it 'requests the correct resource' do
        @client.update_with_media('Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES', fixture('pbjt.gif'))
        expect(a_request(:post, 'https://upload.twitter.com/1.1/media/upload.json')).to have_been_made
        expect(a_post('/1.1/statuses/update.json')).to have_been_made
      end
      it 'returns a Tweet' do
        tweet = @client.update_with_media('Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES', fixture('pbjt.gif'))
        expect(tweet).to be_a Twitter::Tweet
        expect(tweet.text).to eq('Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES')
      end
      context 'which size is bigger than 5 megabytes' do
        let(:big_gif) { fixture('pbjt.gif') }
        before do
          expect(File).to receive(:size).with(big_gif).and_return(7_000_000)
        end
        it 'requests the correct resource' do
          @client.update_with_media('Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES', big_gif)
          expect(a_request(:post, 'https://upload.twitter.com/1.1/media/upload.json')).to have_been_made.times(3)
          expect(a_post('/1.1/statuses/update.json')).to have_been_made
        end
        it 'returns a Tweet' do
          tweet = @client.update_with_media('Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES', big_gif)
          expect(tweet).to be_a Twitter::Tweet
          expect(tweet.text).to eq('Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES')
        end
      end
    end
    context 'with a jpe image' do
      it 'requests the correct resource' do
        @client.update_with_media('You always have options', fixture('wildcomet2.jpe'))
        expect(a_request(:post, 'https://upload.twitter.com/1.1/media/upload.json')).to have_been_made
        expect(a_post('/1.1/statuses/update.json')).to have_been_made
      end
    end
    context 'with a jpeg image' do
      it 'requests the correct resource' do
        @client.update_with_media('You always have options', fixture('me.jpeg'))
        expect(a_request(:post, 'https://upload.twitter.com/1.1/media/upload.json')).to have_been_made
        expect(a_post('/1.1/statuses/update.json')).to have_been_made
      end
    end
    context 'with a png image' do
      it 'requests the correct resource' do
        @client.update_with_media('You always have options', fixture('we_concept_bg2.png'))
        expect(a_request(:post, 'https://upload.twitter.com/1.1/media/upload.json')).to have_been_made
        expect(a_post('/1.1/statuses/update.json')).to have_been_made
      end
    end
    context 'with a mp4 video' do
      it 'requests the correct resources' do
        init_request = {body: fixture('chunk_upload_init.json'), headers: {content_type: 'application/json; charset=utf-8'}}
        append_request = {body: '', headers: {content_type: 'text/html;charset=utf-8'}}
        finalize_request = {body: fixture('chunk_upload_finalize_succeeded.json'), headers: {content_type: 'application/json; charset=utf-8'}}
        stub_request(:post, 'https://upload.twitter.com/1.1/media/upload.json').to_return(init_request, append_request, finalize_request)
        @client.update_with_media('You always have options', fixture('1080p.mp4'))
        expect(a_request(:post, 'https://upload.twitter.com/1.1/media/upload.json')).to have_been_made.times(3)
        expect(a_post('/1.1/statuses/update.json')).to have_been_made
      end
      context 'when the processing is not finished right after the upload' do
        context 'when it succeeds' do
          it 'asks for status until the processing is done' do
            init_request = {body: fixture('chunk_upload_init.json'), headers: {content_type: 'application/json; charset=utf-8'}}
            append_request = {body: '', headers: {content_type: 'text/html;charset=utf-8'}}
            finalize_request = {body: fixture('chunk_upload_finalize_pending.json'), headers: {content_type: 'application/json; charset=utf-8'}}
            pending_status_request = {body: fixture('chunk_upload_status_pending.json'), headers: {content_type: 'application/json; charset=utf-8'}}
            completed_status_request = {body: fixture('chunk_upload_status_succeeded.json'), headers: {content_type: 'application/json; charset=utf-8'}}
            stub_request(:post, 'https://upload.twitter.com/1.1/media/upload.json').to_return(init_request, append_request, finalize_request)
            stub_request(:get, 'https://upload.twitter.com/1.1/media/upload.json?command=STATUS&media_id=710511363345354753').to_return(pending_status_request, completed_status_request)
            expect_any_instance_of(Twitter::REST::DirectMessages).to receive(:sleep).with(5).and_return(5)
            expect_any_instance_of(Twitter::REST::DirectMessages).to receive(:sleep).with(10).and_return(10)
            @client.update_with_media('You always have options', fixture('1080p.mp4'))
            expect(a_request(:post, 'https://upload.twitter.com/1.1/media/upload.json')).to have_been_made.times(3)
            expect(a_request(:get, 'https://upload.twitter.com/1.1/media/upload.json?command=STATUS&media_id=710511363345354753')).to have_been_made.times(2)
            expect(a_post('/1.1/statuses/update.json')).to have_been_made
          end
        end
        context 'when it fails' do
          it 'raises an error' do
            init_request = {body: fixture('chunk_upload_init.json'), headers: {content_type: 'application/json; charset=utf-8'}}
            append_request = {body: '', headers: {content_type: 'text/html;charset=utf-8'}}
            finalize_request = {body: fixture('chunk_upload_finalize_pending.json'), headers: {content_type: 'application/json; charset=utf-8'}}
            failed_status_request = {body: fixture('chunk_upload_status_failed.json'), headers: {content_type: 'application/json; charset=utf-8'}}
            stub_request(:post, 'https://upload.twitter.com/1.1/media/upload.json').to_return(init_request, append_request, finalize_request)
            stub_request(:get, 'https://upload.twitter.com/1.1/media/upload.json?command=STATUS&media_id=710511363345354753').to_return(failed_status_request)
            expect_any_instance_of(Twitter::REST::DirectMessages).to receive(:sleep).with(5).and_return(5)
            expect { @client.update_with_media('You always have options', fixture('1080p.mp4')) }.to raise_error(Twitter::Error::InvalidMedia)
            expect(a_request(:post, 'https://upload.twitter.com/1.1/media/upload.json')).to have_been_made.times(3)
            expect(a_request(:get, 'https://upload.twitter.com/1.1/media/upload.json?command=STATUS&media_id=710511363345354753')).to have_been_made
          end
        end
        context 'when Twitter::Client#timeouts[:upload] is set' do
          before(:each) { @client.timeouts = {upload: 0.1} }
          it 'raises an error when the finalize step is too slow' do
            init_request = {body: fixture('chunk_upload_init.json'), headers: {content_type: 'application/json; charset=utf-8'}}
            append_request = {body: '', headers: {content_type: 'text/html;charset=utf-8'}}
            finalize_request = {body: fixture('chunk_upload_finalize_pending.json'), headers: {content_type: 'application/json; charset=utf-8'}}
            stub_request(:post, 'https://upload.twitter.com/1.1/media/upload.json').to_return(init_request, append_request, finalize_request)
            expect { @client.update_with_media('You always have options', fixture('1080p.mp4')) }.to raise_error(Twitter::Error::TimeoutError)
            expect(a_request(:post, 'https://upload.twitter.com/1.1/media/upload.json')).to have_been_made.times(3)
          end
        end
      end
    end
    context 'with a Tempfile' do
      it 'requests the correct resource' do
        @client.update_with_media('You always have options', Tempfile.new('tmp'))
        expect(a_request(:post, 'https://upload.twitter.com/1.1/media/upload.json')).to have_been_made
        expect(a_post('/1.1/statuses/update.json')).to have_been_made
      end
    end
    context 'with multiple images' do
      it 'requests the correct resource' do
        @client.update_with_media('You always have options', [fixture('me.jpeg'), fixture('me.jpeg')])
        expect(a_request(:post, 'https://upload.twitter.com/1.1/media/upload.json')).to have_been_made.times(2)
        expect(a_post('/1.1/statuses/update.json')).to have_been_made
      end
    end
  end

  describe '#oembed' do
    before do
      stub_get('/1.1/statuses/oembed.json').with(query: {id: '540897316908331009'}).to_return(body: fixture('oembed.json'), headers: {content_type: 'application/json; charset=utf-8'})
      stub_get('/1.1/statuses/oembed.json').with(query: {url: 'https://twitter.com/sferik/status/540897316908331009'}).to_return(body: fixture('oembed.json'), headers: {content_type: 'application/json; charset=utf-8'})
    end
    it 'requests the correct resource' do
      @client.oembed(540_897_316_908_331_009)
      expect(a_get('/1.1/statuses/oembed.json').with(query: {id: '540897316908331009'})).to have_been_made
    end
    it 'requests the correct resource when a URL is given' do
      @client.oembed('https://twitter.com/sferik/status/540897316908331009')
      expect(a_get('/1.1/statuses/oembed.json').with(query: {url: 'https://twitter.com/sferik/status/540897316908331009'}))
    end
    it 'returns an array of OEmbed instances' do
      oembed = @client.oembed(540_897_316_908_331_009)
      expect(oembed).to be_a Twitter::OEmbed
    end
    context 'with a URI object passed' do
      it 'requests the correct resource' do
        tweet = URI.parse('https://twitter.com/sferik/status/540897316908331009')
        @client.oembed(tweet)
        expect(a_get('/1.1/statuses/oembed.json').with(query: {id: '540897316908331009'})).to have_been_made
      end
    end
    context 'with a Tweet passed' do
      it 'requests the correct resource' do
        tweet = Twitter::Tweet.new(id: 540_897_316_908_331_009)
        @client.oembed(tweet)
        expect(a_get('/1.1/statuses/oembed.json').with(query: {id: '540897316908331009'})).to have_been_made
      end
    end
  end

  describe '#oembeds' do
    before do
      stub_get('/1.1/statuses/oembed.json').with(query: {id: '540897316908331009'}).to_return(body: fixture('oembed.json'), headers: {content_type: 'application/json; charset=utf-8'})
    end
    it 'requests the correct resource' do
      @client.oembeds(540_897_316_908_331_009)
      expect(a_get('/1.1/statuses/oembed.json').with(query: {id: '540897316908331009'})).to have_been_made
    end
    it 'requests the correct resource when a URL is given' do
      @client.oembeds('https://twitter.com/sferik/status/540897316908331009')
      expect(a_get('/1.1/statuses/oembed.json').with(query: {id: '540897316908331009'})).to have_been_made
    end
    it 'returns an array of OEmbed instances' do
      oembeds = @client.oembeds(540_897_316_908_331_009)
      expect(oembeds).to be_an Array
      expect(oembeds.first).to be_a Twitter::OEmbed
    end
    context 'with a URI object passed' do
      it 'requests the correct resource' do
        tweet = URI.parse('https://twitter.com/sferik/status/540897316908331009')
        @client.oembeds(tweet)
        expect(a_get('/1.1/statuses/oembed.json').with(query: {id: '540897316908331009'})).to have_been_made
      end
    end
    context 'with a Tweet passed' do
      it 'requests the correct resource' do
        tweet = Twitter::Tweet.new(id: 540_897_316_908_331_009)
        @client.oembeds(tweet)
        expect(a_get('/1.1/statuses/oembed.json').with(query: {id: '540897316908331009'})).to have_been_made
      end
    end
  end

  describe '#retweeters_ids' do
    before do
      stub_get('/1.1/statuses/retweeters/ids.json').with(query: {id: '540897316908331009', cursor: '-1'}).to_return(body: fixture('ids_list.json'), headers: {content_type: 'application/json; charset=utf-8'})
    end
    it 'requests the correct resource' do
      @client.retweeters_ids(540_897_316_908_331_009)
      expect(a_get('/1.1/statuses/retweeters/ids.json').with(query: {id: '540897316908331009', cursor: '-1'})).to have_been_made
    end
    it 'returns a collection of up to 100 user IDs belonging to users who have retweeted the tweet specified by the id parameter' do
      retweeters_ids = @client.retweeters_ids(540_897_316_908_331_009)
      expect(retweeters_ids).to be_a Twitter::Cursor
      expect(retweeters_ids.first).to eq(20_009_713)
    end
    context 'with each' do
      before do
        stub_get('/1.1/statuses/retweeters/ids.json').with(query: {id: '540897316908331009', cursor: '1305102810874389703'}).to_return(body: fixture('ids_list2.json'), headers: {content_type: 'application/json; charset=utf-8'})
      end
      it 'requests the correct resource' do
        @client.retweeters_ids(540_897_316_908_331_009).each {}
        expect(a_get('/1.1/statuses/retweeters/ids.json').with(query: {id: '540897316908331009', cursor: '-1'})).to have_been_made
        expect(a_get('/1.1/statuses/retweeters/ids.json').with(query: {id: '540897316908331009', cursor: '1305102810874389703'})).to have_been_made
      end
    end
    context 'with a URI object passed' do
      it 'requests the correct resource' do
        tweet = URI.parse('https://twitter.com/sferik/status/540897316908331009')
        @client.retweeters_ids(tweet)
        expect(a_get('/1.1/statuses/retweeters/ids.json').with(query: {id: '540897316908331009', cursor: '-1'})).to have_been_made
      end
    end
    context 'with a Tweet passed' do
      it 'requests the correct resource' do
        tweet = Twitter::Tweet.new(id: 540_897_316_908_331_009)
        @client.retweeters_ids(tweet)
        expect(a_get('/1.1/statuses/retweeters/ids.json').with(query: {id: '540897316908331009', cursor: '-1'})).to have_been_made
      end
    end
  end

  describe '#unretweet' do
    before do
      stub_post('/1.1/statuses/unretweet/540897316908331009.json').to_return(body: fixture('retweet.json'), headers: {content_type: 'application/json; charset=utf-8'})
    end
    it 'requests the correct resource' do
      @client.unretweet(540_897_316_908_331_009)
      expect(a_post('/1.1/statuses/unretweet/540897316908331009.json')).to have_been_made
    end
    it 'returns an array of Tweets with retweet details embedded' do
      tweets = @client.unretweet(540_897_316_908_331_009)
      expect(tweets).to be_an Array
      expect(tweets.first).to be_a Twitter::Tweet
      expect(tweets.first.text).to eq("RT @gruber: As for the Series, I'm for the Giants. Fuck Texas, fuck Nolan Ryan, fuck George Bush.")
      expect(tweets.first.retweeted_tweet.text).to eq("As for the Series, I'm for the Giants. Fuck Texas, fuck Nolan Ryan, fuck George Bush.")
      expect(tweets.first.retweeted_tweet.id).not_to eq(tweets.first.id)
    end
    context 'not found' do
      before do
        stub_post('/1.1/statuses/unretweet/540897316908331009.json').to_return(status: 404, body: fixture('not_found.json'), headers: {content_type: 'application/json; charset=utf-8'})
      end
      it 'does not raise an error' do
        expect { @client.unretweet(540_897_316_908_331_009) }.not_to raise_error
      end
    end
    context 'with a URI object passed' do
      it 'requests the correct resource' do
        tweet = URI.parse('https://twitter.com/sferik/status/540897316908331009')
        @client.unretweet(tweet)
        expect(a_post('/1.1/statuses/unretweet/540897316908331009.json')).to have_been_made
      end
    end
    context 'with a Tweet passed' do
      it 'requests the correct resource' do
        tweet = Twitter::Tweet.new(id: 540_897_316_908_331_009)
        @client.unretweet(tweet)
        expect(a_post('/1.1/statuses/unretweet/540897316908331009.json')).to have_been_made
      end
    end
  end
end