File: client_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 (134 lines) | stat: -rw-r--r-- 4,755 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
require 'helper'

class FakeConnection
  def initialize(body)
    @body = body
  end

  def stream(_request, response)
    @body.each_line do |line|
      response.on_body(line)
    end
  end
end

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

  describe '#before_request' do
    it 'runs before a request' do
      @client.connection = FakeConnection.new(fixture('track_streaming.json'))
      var = false
      @client.before_request do
        var = true
      end
      expect(var).to be false
      @client.user {}
      expect(var).to be true
    end
  end

  describe '#filter' do
    it 'returns an arary of Tweets' do
      @client.connection = FakeConnection.new(fixture('track_streaming.json'))
      objects = []
      @client.filter(track: 'india') do |object|
        objects << object
      end
      expect(objects.size).to eq(2)
      expect(objects.first).to be_a Twitter::Tweet
      expect(objects.first.text).to eq "The problem with your code is that it's doing exactly what you told it to do."
    end
  end

  describe '#firehose' do
    it 'returns an arary of Tweets' do
      @client.connection = FakeConnection.new(fixture('track_streaming.json'))
      objects = []
      @client.firehose do |object|
        objects << object
      end
      expect(objects.size).to eq(2)
      expect(objects.first).to be_a Twitter::Tweet
      expect(objects.first.text).to eq "The problem with your code is that it's doing exactly what you told it to do."
    end
  end

  describe '#sample' do
    it 'returns an arary of Tweets' do
      @client.connection = FakeConnection.new(fixture('track_streaming.json'))
      objects = []
      @client.sample do |object|
        objects << object
      end
      expect(objects.size).to eq(2)
      expect(objects.first).to be_a Twitter::Tweet
      expect(objects.first.text).to eq "The problem with your code is that it's doing exactly what you told it to do."
    end
  end

  describe '#site' do
    context 'with a user ID passed' do
      it 'returns an arary of Tweets' do
        @client.connection = FakeConnection.new(fixture('track_streaming.json'))
        objects = []
        @client.site(7_505_382) do |object|
          objects << object
        end
        expect(objects.size).to eq(2)
        expect(objects.first).to be_a Twitter::Tweet
        expect(objects.first.text).to eq "The problem with your code is that it's doing exactly what you told it to do."
      end
    end
    context 'with a user object passed' do
      it 'returns an arary of Tweets' do
        @client.connection = FakeConnection.new(fixture('track_streaming.json'))
        objects = []
        user = Twitter::User.new(id: 7_505_382)
        @client.site(user) do |object|
          objects << object
        end
        expect(objects.size).to eq(2)
        expect(objects.first).to be_a Twitter::Tweet
        expect(objects.first.text).to eq "The problem with your code is that it's doing exactly what you told it to do."
      end
    end
  end

  describe '#user' do
    it 'returns an arary of Tweets' do
      @client.connection = FakeConnection.new(fixture('track_streaming_user.json'))
      objects = []
      @client.user do |object|
        objects << object
      end
      expect(objects.size).to eq(6)
      expect(objects[0]).to be_a Twitter::Streaming::FriendList
      expect(objects[0]).to eq([488_736_931, 311_444_249])
      expect(objects[1]).to be_a Twitter::Tweet
      expect(objects[1].text).to eq("The problem with your code is that it's doing exactly what you told it to do.")
      expect(objects[2]).to be_a Twitter::DirectMessage
      expect(objects[2].text).to eq('hello bot')
      expect(objects[3]).to be_a Twitter::Streaming::Event
      expect(objects[3].name).to eq(:follow)
      expect(objects[4]).to be_a Twitter::Streaming::DeletedTweet
      expect(objects[4].id).to eq(272_691_609_211_117_568)
      expect(objects[5]).to be_a Twitter::Streaming::StallWarning
      expect(objects[5].code).to eq('FALLING_BEHIND')
    end
  end

  context 'when using a proxy' do
    let(:proxy) { {host: '127.0.0.1', port: 3328} }
    before do
      @client = Twitter::Streaming::Client.new(consumer_key: 'CK', consumer_secret: 'CS', access_token: 'AT', access_token_secret: 'AS', proxy: proxy)
    end
    it 'requests via the proxy' do
      @client.connection = FakeConnection.new(fixture('track_streaming.json'))
      expect(HTTP::Request).to receive(:new).with(verb: :get, uri: 'https://stream.twitter.com:443/1.1/statuses/sample.json?', headers: kind_of(Hash), proxy: proxy)
      @client.sample {}
    end
  end
end