File: tweet_spec.rb

package info (click to toggle)
ruby-twitter 7.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,856 kB
  • sloc: ruby: 10,919; makefile: 6
file content (484 lines) | stat: -rw-r--r-- 17,103 bytes parent folder | download | duplicates (3)
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
require 'helper'

describe Twitter::Tweet do
  before do
    @old_stderr = $stderr
    $stderr = StringIO.new
  end

  after do
    $stderr = @old_stderr
  end

  describe '#==' do
    it 'returns true when objects IDs are the same' do
      tweet = Twitter::Tweet.new(id: 1, text: 'foo')
      other = Twitter::Tweet.new(id: 1, text: 'bar')
      expect(tweet == other).to be true
    end
    it 'returns false when objects IDs are different' do
      tweet = Twitter::Tweet.new(id: 1)
      other = Twitter::Tweet.new(id: 2)
      expect(tweet == other).to be false
    end
    it 'returns false when classes are different' do
      tweet = Twitter::Tweet.new(id: 1)
      other = Twitter::Identity.new(id: 1)
      expect(tweet == other).to be false
    end
  end

  describe '#created_at' do
    it 'returns a Time when set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014, created_at: 'Mon Jul 16 12:59:01 +0000 2007')
      expect(tweet.created_at).to be_a Time
      expect(tweet.created_at).to be_utc
    end
    it 'returns nil when not set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014)
      expect(tweet.created_at).to be_nil
    end
  end

  describe '#created?' do
    it 'returns true when created_at is set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014, created_at: 'Mon Jul 16 12:59:01 +0000 2007')
      expect(tweet.created?).to be true
    end
    it 'returns false when created_at is not set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014)
      expect(tweet.created?).to be false
    end
  end

  describe '#entities?' do
    it 'returns true if there are entities set' do
      urls_array = [
        {
          url: 'https://t.co/L2xIBazMPf',
          expanded_url: 'http://example.com/expanded',
          display_url: 'example.com/expanded...',
          indices: [10, 33],
        },
      ]
      tweet = Twitter::Tweet.new(id: 28_669_546_014, entities: {urls: urls_array})
      expect(tweet.entities?).to be true
    end
    it 'returns false if there are blank lists of entities set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014, entities: {urls: []})
      expect(tweet.entities?).to be false
    end
    it 'returns false if there are no entities set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014)
      expect(tweet.entities?).to be false
    end
  end

  describe '#filter_level' do
    it 'returns the filter level when filter_level is set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014, filter_level: 'high')
      expect(tweet.filter_level).to be_a String
      expect(tweet.filter_level).to eq('high')
    end
    it 'returns nil when not set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014)
      expect(tweet.filter_level).to be_nil
    end
  end

  describe '#full_text' do
    it 'returns the text of a Tweet' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014, text: 'BOOSH')
      expect(tweet.full_text).to be_a String
      expect(tweet.full_text).to eq('BOOSH')
    end
    it 'returns the text of an extended Tweet' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014, text: nil, full_text: 'BOOSH')
      expect(tweet.full_text).to be_a String
      expect(tweet.full_text).to eq('BOOSH')
    end
    it 'returns the text of a Tweet without a user' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014, text: 'BOOSH', retweeted_status: {id: 28_561_922_517, text: 'BOOSH'})
      expect(tweet.full_text).to be_a String
      expect(tweet.full_text).to eq('BOOSH')
    end
    it 'returns the full text of a retweeted Tweet' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014, text: 'RT @sferik: BOOSH', retweeted_status: {id: 540_897_316_908_331_009, text: 'BOOSH'})
      expect(tweet.full_text).to be_a String
      expect(tweet.full_text).to eq('RT @sferik: BOOSH')
    end
    it 'returns nil when retweeted_status is not set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014)
      expect(tweet.full_text).to be_nil
    end
  end

  describe '#geo' do
    it 'returns a Twitter::Geo::Point when geo is set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014, geo: {id: 1, type: 'Point'})
      expect(tweet.geo).to be_a Twitter::Geo::Point
    end
    it 'returns nil when geo is not set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014)
      expect(tweet.geo).to be_nil
    end
  end

  describe '#geo?' do
    it 'returns true when geo is set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014, geo: {id: 1, type: 'Point'})
      expect(tweet.geo?).to be true
    end
    it 'returns false when geo is not set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014)
      expect(tweet.geo?).to be false
    end
  end

  describe '#hashtags' do
    context 'when entities are set' do
      let(:hashtags_array) do
        [{
          text: 'twitter',
          indices: [10, 33],
        }]
      end
      let(:subject) do
        Twitter::Tweet.new(id: 28_669_546_014, entities: {hashtags: hashtags_array})
      end
      it 'returns an array of Entity::Hashtag' do
        hashtags = subject.hashtags
        expect(hashtags).to be_an Array
        expect(hashtags.first).to be_a Twitter::Entity::Hashtag
        expect(hashtags.first.indices).to eq([10, 33])
        expect(hashtags.first.text).to eq('twitter')
      end
    end
    context 'when entities are set, but empty' do
      subject { Twitter::Tweet.new(id: 28_669_546_014, entities: {hashtags: []}) }
      it 'is empty' do
        expect(subject.hashtags).to be_empty
      end
      it 'does not warn' do
        subject.hashtags
        expect($stderr.string).to be_empty
      end
    end
    context 'when entities are not set' do
      subject { Twitter::Tweet.new(id: 28_669_546_014) }
      it 'is empty' do
        expect(subject.hashtags).to be_empty
      end
    end
  end

  describe '#hashtags?' do
    it 'returns true when the tweet includes hashtags entities' do
      entities = {hashtags: [{text: 'twitter', indices: [10, 33]}]}
      tweet = Twitter::Tweet.new(id: 28_669_546_014, entities: entities)
      expect(tweet.hashtags?).to be true
    end
    it 'returns false when no entities are present' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014)
      expect(tweet.hashtags?).to be false
    end
  end

  describe '#media' do
    it 'returns media' do
      media = Twitter::Tweet.new(id: 28_669_546_014, entities: {media: [{id: 1, type: 'photo'}]}).media
      expect(media).to be_an Array
      expect(media.first).to be_a Twitter::Media::Photo
    end
    it 'is empty when not set' do
      media = Twitter::Tweet.new(id: 28_669_546_014).media
      expect(media).to be_empty
    end
  end

  describe '#media?' do
    it 'returns true when the tweet includes media entities' do
      entities = {media: [{id: '1', type: 'photo'}]}
      tweet = Twitter::Tweet.new(id: 28_669_546_014, entities: entities)
      expect(tweet.media?).to be true
    end
    it 'returns false when no entities are present' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014)
      expect(tweet.media?).to be false
    end
  end

  describe '#metadata' do
    it 'returns a Twitter::Metadata when metadata is set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014, metadata: {result_type: 'recent'})
      expect(tweet.metadata).to be_a Twitter::Metadata
    end
    it 'returns nil when metadata is not set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014)
      expect(tweet.metadata).to be_nil
    end
  end

  describe '#metadata?' do
    it 'returns true when metadata is set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014, metadata: {result_type: 'recent'})
      expect(tweet.metadata?).to be true
    end
    it 'returns false when metadata is not set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014)
      expect(tweet.metadata?).to be false
    end
  end

  describe '#place' do
    it 'returns a Twitter::Place when place is set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014, place: {id: '247f43d441defc03'})
      expect(tweet.place).to be_a Twitter::Place
    end
    it 'returns nil when place is not set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014)
      expect(tweet.place).to be_nil
    end
  end

  describe '#place?' do
    it 'returns true when place is set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014, place: {id: '247f43d441defc03'})
      expect(tweet.place?).to be true
    end
    it 'returns false when place is not set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014)
      expect(tweet.place?).to be false
    end
  end

  describe '#reply?' do
    it 'returns true when there is an in-reply-to user' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014, in_reply_to_user_id: 7_505_382)
      expect(tweet.reply?).to be true
    end
    it 'returns false when in_reply_to_user_id is not set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014)
      expect(tweet.reply?).to be false
    end
  end

  describe '#retweet?' do
    it 'returns true when there is a retweeted status' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014, retweeted_status: {id: 540_897_316_908_331_009, text: 'BOOSH'})
      expect(tweet.retweet?).to be true
    end
    it 'returns false when retweeted_status is not set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014)
      expect(tweet.retweet?).to be false
    end
  end

  describe '#retweeted_status' do
    it 'returns a Tweet when retweeted_status is set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014, retweeted_status: {id: 540_897_316_908_331_009, text: 'BOOSH'})
      expect(tweet.retweeted_tweet).to be_a Twitter::Tweet
      expect(tweet.retweeted_tweet.text).to eq('BOOSH')
    end
    it 'returns nil when retweeted_status is not set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014)
      expect(tweet.retweeted_tweet).to be_nil
    end
  end

  describe '#retweeted_status?' do
    it 'returns true when retweeted_status is set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014, retweeted_status: {id: 540_897_316_908_331_009, text: 'BOOSH'})
      expect(tweet.retweeted_status?).to be true
    end
    it 'returns false when retweeted_status is not set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014)
      expect(tweet.retweeted_status?).to be false
    end
  end

  describe '#quote?' do
    it 'returns true when there is a quoted status' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014, quoted_status: {id: 540_897_316_908_331_009, text: 'BOOSH'})
      expect(tweet.quote?).to be true
    end
    it 'returns false when quoted_status is not set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014)
      expect(tweet.quote?).to be false
    end
  end

  describe '#quoted_status' do
    it 'returns a Tweet when quoted_status is set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014, quoted_status: {id: 540_897_316_908_331_009, text: 'BOOSH'})
      expect(tweet.quoted_tweet).to be_a Twitter::Tweet
      expect(tweet.quoted_tweet.text).to eq('BOOSH')
    end
    it 'returns nil when quoted_status is not set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014)
      expect(tweet.quoted_tweet).to be_nil
    end
  end

  describe '#quoted_status?' do
    it 'returns true when quoted_status is set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014, quoted_status: {id: 540_897_316_908_331_009, text: 'BOOSH'})
      expect(tweet.quoted_status?).to be true
    end
    it 'returns false when quoted_status is not set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014)
      expect(tweet.quoted_status?).to be false
    end
  end

  describe '#symbols' do
    it 'returns an array of Entity::Symbol when symbols are set' do
      symbols_array = [
        {text: 'PEP', indices: [114, 118]},
        {text: 'COKE', indices: [128, 133]},
      ]
      symbols = Twitter::Tweet.new(id: 28_669_546_014, entities: {symbols: symbols_array}).symbols
      expect(symbols).to be_an Array
      expect(symbols.size).to eq(2)
      expect(symbols.first).to be_a Twitter::Entity::Symbol
      expect(symbols.first.indices).to eq([114, 118])
      expect(symbols.first.text).to eq('PEP')
    end
    it 'is empty when not set' do
      symbols = Twitter::Tweet.new(id: 28_669_546_014).symbols
      expect(symbols).to be_empty
    end
  end

  describe '#symbols?' do
    it 'returns true when the tweet includes symbols entities' do
      entities = {symbols: [{text: 'PEP'}]}
      tweet = Twitter::Tweet.new(id: 28_669_546_014, entities: entities)
      expect(tweet.symbols?).to be true
    end
    it 'returns false when no entities are present' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014)
      expect(tweet.symbols?).to be false
    end
  end

  describe '#uris' do
    it 'returns an array of Entity::URIs when entities are set' do
      urls_array = [
        {
          url: 'https://t.co/L2xIBazMPf',
          expanded_url: 'http://example.com/expanded',
          display_url: 'example.com/expanded...',
          indices: [10, 33],
        },
      ]
      tweet = Twitter::Tweet.new(id: 28_669_546_014, entities: {urls: urls_array})
      expect(tweet.uris).to be_an Array
      expect(tweet.uris.first).to be_a Twitter::Entity::URI
      expect(tweet.uris.first.indices).to eq([10, 33])
      expect(tweet.uris.first.display_uri).to be_a String
      expect(tweet.uris.first.display_uri).to eq('example.com/expanded...')
    end
    it 'is empty when not set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014)
      expect(tweet.uris).to be_empty
    end
    it 'can handle strange urls' do
      urls_array = [
        {
          url: 'https://t.co/L2xIBazMPf',
          expanded_url: 'http://with_underscore.example.com/expanded',
          display_url: 'with_underscore.example.com/expanded...',
          indices: [10, 33],
        },
      ]
      tweet = Twitter::Tweet.new(id: 28_669_546_014, entities: {urls: urls_array})
      uri = tweet.uris.first
      expect { uri.url }.not_to raise_error
      expect { uri.expanded_url }.not_to raise_error
      expect { uri.display_url }.not_to raise_error
    end
  end

  describe '#uri' do
    it 'returns the URI to the tweet' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014, user: {id: 7_505_382, screen_name: 'sferik'})
      expect(tweet.uri).to be_an Addressable::URI
      expect(tweet.uri.to_s).to eq('https://twitter.com/sferik/status/28669546014')
    end
  end

  describe '#uris?' do
    it 'returns true when the tweet includes urls entities' do
      entities = {urls: [{url: 'https://t.co/L2xIBazMPf'}]}
      tweet = Twitter::Tweet.new(id: 28_669_546_014, entities: entities)
      expect(tweet.uris?).to be true
    end
    it 'returns false when no entities are present' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014)
      expect(tweet.uris?).to be false
    end
  end

  describe '#user' do
    it 'returns a User when user is set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014, user: {id: 7_505_382})
      expect(tweet.user).to be_a Twitter::User
    end
    it 'returns nil when user is not set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014)
      expect(tweet.user).to be_nil
    end
    it 'has a status is set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014, text: 'Tweet text.', user: {id: 7_505_382})
      expect(tweet.user.status).to be_a Twitter::Tweet
      expect(tweet.user.status.id).to eq 28_669_546_014
    end
  end

  describe '#user?' do
    it 'returns true when status is set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014, user: {id: 7_505_382})
      expect(tweet.user?).to be true
    end
    it 'returns false when status is not set' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014)
      expect(tweet.user?).to be false
    end
  end

  describe '#user_mentions' do
    it 'returns an array of Entity::UserMention when entities are set' do
      user_mentions_array = [
        {
          screen_name: 'sferik',
          name: 'Erik Berlin',
          id_str: '7_505_382',
          indices: [0, 6],
          id: 7_505_382,
        },
      ]
      user_mentions = Twitter::Tweet.new(id: 28_669_546_014, entities: {user_mentions: user_mentions_array}).user_mentions
      expect(user_mentions).to be_an Array
      expect(user_mentions.first).to be_a Twitter::Entity::UserMention
      expect(user_mentions.first.indices).to eq([0, 6])
      expect(user_mentions.first.id).to eq(7_505_382)
    end
    it 'is empty when not set' do
      user_mentions = Twitter::Tweet.new(id: 28_669_546_014).user_mentions
      expect(user_mentions).to be_empty
    end
  end

  describe '#user_mentions?' do
    it 'returns true when the tweet includes user_mention entities' do
      entities = {user_mentions: [{screen_name: 'sferik'}]}
      tweet = Twitter::Tweet.new(id: 28_669_546_014, entities: entities)
      expect(tweet.user_mentions?).to be true
    end
    it 'returns false when no entities are present' do
      tweet = Twitter::Tweet.new(id: 28_669_546_014)
      expect(tweet.user_mentions?).to be false
    end
  end
end