tweetstream 2.0.1 → 2.1.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.

data/Gemfile CHANGED
@@ -1,5 +1,3 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'twitter', :github => 'jnunemaker/twitter'
4
-
5
3
  gemspec
data/README.md CHANGED
@@ -213,7 +213,7 @@ JSON parser and it will be used to parse responses.
213
213
  Sometimes the Streaming API will send messages other than statuses.
214
214
  Specifically, it does so when a status is deleted or rate limitations
215
215
  have caused some tweets not to appear in the stream. To handle these,
216
- you can use the on_delete and on_limit methods. Example:
216
+ you can use the on_delete, on_limit and on_enhance_your_calm methods. Example:
217
217
 
218
218
  ```ruby
219
219
  @client = TweetStream::Client.new
@@ -226,6 +226,10 @@ end
226
226
  # do something
227
227
  end
228
228
 
229
+ @client.on_enhance_your_calm do
230
+ # do something
231
+ end
232
+
229
233
  @client.track('intridea')
230
234
  ```
231
235
 
data/lib/tweetstream.rb CHANGED
@@ -1,11 +1,19 @@
1
1
  require 'tweetstream/configuration'
2
2
  require 'tweetstream/client'
3
- require 'tweetstream/error'
4
3
  require 'tweetstream/daemon'
5
4
 
6
5
  module TweetStream
7
6
  extend Configuration
8
7
 
8
+ class ReconnectError < StandardError
9
+ attr_accessor :timeout, :retries
10
+ def initialize(timeout, retries)
11
+ self.timeout = timeout
12
+ self.retries = retries
13
+ super("Failed to reconnect after #{retries} tries.")
14
+ end
15
+ end
16
+
9
17
  class << self
10
18
  # Alias for TweetStream::Client.new
11
19
  #
@@ -1,8 +1,8 @@
1
1
  require 'cgi'
2
+ require 'em-twitter'
2
3
  require 'eventmachine'
3
4
  require 'multi_json'
4
5
  require 'twitter'
5
- require 'em-twitter'
6
6
  require 'uri'
7
7
 
8
8
  module TweetStream
@@ -118,21 +118,22 @@ module TweetStream
118
118
  end
119
119
 
120
120
  # Make a call to the userstream api for currently authenticated user
121
- def userstream(&block)
121
+ def userstream(query_params = {}, &block)
122
122
  stream_params = { :host => "userstream.twitter.com", :path => "/2/user.json" }
123
- start('', :extra_stream_parameters => stream_params, &block)
123
+ query_params.merge!(:extra_stream_parameters => stream_params)
124
+ start('', query_params, &block)
124
125
  end
125
126
 
126
127
  # Make a call to the userstream api
127
128
  def sitestream(user_ids = [], query_params = {}, &block)
128
129
  stream_params = { :host => "sitestream.twitter.com", :path => '/2b/site.json' }
129
- sitestream_params = {
130
+ query_params.merge!({
130
131
  :method => :post,
131
132
  :follow => user_ids,
132
133
  :extra_stream_parameters => stream_params
133
- }
134
- sitestream_params.merge!(:with => 'followings') if query_params[:followings]
135
- start('', sitestream_params, &block)
134
+ })
135
+ query_params.merge!(:with => 'followings') if query_params.delete(:followings)
136
+ start('', query_params, &block)
136
137
  end
137
138
 
138
139
  # Set a Proc to be run when a deletion notice is received
@@ -348,6 +349,21 @@ module TweetStream
348
349
  end
349
350
  end
350
351
 
352
+ # Set a Proc to be run when enhance_your_calm signal is received.
353
+ #
354
+ # @client = TweetStream::Client.new
355
+ # @client.on_enhance_your_calm do
356
+ # # do something, your account has been blocked
357
+ # end
358
+ def on_enhance_your_calm(&block)
359
+ if block_given?
360
+ @on_enhance_your_calm = block
361
+ self
362
+ else
363
+ @on_enhance_your_calm
364
+ end
365
+ end
366
+
351
367
  # connect to twitter while starting a new EventMachine run loop
352
368
  def start(path, query_parameters = {}, &block)
353
369
  if EventMachine.reactor_running?
@@ -369,6 +385,7 @@ module TweetStream
369
385
  scrub_geo_proc = query_parameters.delete(:scrub_geo) || self.on_scrub_geo
370
386
  limit_proc = query_parameters.delete(:limit) || self.on_limit
371
387
  error_proc = query_parameters.delete(:error) || self.on_error
388
+ enhance_your_calm_proc = query_parameters.delete(:enhance_your_calm) || self.on_error
372
389
  unauthorized_proc = query_parameters.delete(:unauthorized) || self.on_unauthorized
373
390
  reconnect_proc = query_parameters.delete(:reconnect) || self.on_reconnect
374
391
  inited_proc = query_parameters.delete(:inited) || self.on_inited
@@ -405,6 +422,8 @@ module TweetStream
405
422
  next
406
423
  end
407
424
 
425
+ Twitter.identity_map = false
426
+
408
427
  if hash[:control] && hash[:control][:control_uri]
409
428
  @control_uri = hash[:control][:control_uri]
410
429
  require 'tweetstream/site_stream_client'
@@ -458,6 +477,10 @@ module TweetStream
458
477
  unauthorized_proc.call if unauthorized_proc.is_a?(Proc)
459
478
  end
460
479
 
480
+ @stream.on_enhance_your_calm do
481
+ enhance_your_calm_proc.call if enhance_your_calm_proc.is_a?(Proc)
482
+ end
483
+
461
484
  @stream.on_reconnect do |timeout, retries|
462
485
  reconnect_proc.call(timeout, retries) if reconnect_proc.is_a?(Proc)
463
486
  end
@@ -28,67 +28,39 @@ module TweetStream
28
28
  end
29
29
 
30
30
  def info(&block)
31
- error_msg = 'Failed to retrieve SiteStream info.'
32
-
33
- http = connection.get(:path => info_path)
34
- http.callback do
35
- if http.response_header.status == 200
36
- block.call http.response if block && block.kind_of?(Proc)
37
- else
38
- @on_error.call(error_msg) if @on_error && @on_error.kind_of?(Proc)
39
- end
40
- end
41
- http.errback do
42
- @on_error.call(error_msg) if @on_error && @on_error.kind_of?(Proc)
43
- end
31
+ options = { :error_msg => 'Failed to retrieve SiteStream info.' }
32
+ request(:get, info_path, options, &block)
44
33
  end
45
34
 
46
35
  def add_user(user_id, &block)
47
- error_msg = 'Failed to add user to SiteStream'
48
- user_management(add_user_path, user_id, error_msg, &block)
36
+ options = {
37
+ :error_msg => 'Failed to add user to SiteStream',
38
+ :body => { 'user_id' => normalized_user_ids(user_id) }
39
+ }
40
+
41
+ request(:post, add_user_path, options, &block)
49
42
  end
50
43
 
51
44
  def remove_user(user_id, &block)
52
- error_msg = 'Failed to remove user from SiteStream.'
53
- user_management(remove_user_path, user_id, error_msg, &block)
45
+ options = {
46
+ :error_msg => 'Failed to remove user from SiteStream.',
47
+ :body => { 'user_id' => normalized_user_ids(user_id) }
48
+ }
49
+
50
+ request(:post, remove_user_path, options, &block)
54
51
  end
55
52
 
56
53
  def friends_ids(user_id, &block)
57
- error_msg = 'Failed to retrieve SiteStream friends ids.'
58
-
59
- http = connection.post(:path => friends_ids_path, :body => { 'user_id' => user_id })
60
- http.callback do
61
- if http.response_header.status == 200
62
- block.call http.response if block && block.kind_of?(Proc)
63
- else
64
- @on_error.call(error_msg) if @on_error && @on_error.kind_of?(Proc)
65
- end
66
- end
67
- http.errback do
68
- @on_error.call(error_msg) if @on_error && @on_error.kind_of?(Proc)
69
- end
54
+ options = { :error_msg => 'Failed to retrieve SiteStream friends ids.',
55
+ :body => { 'user_id' => user_id }
56
+ }
57
+ request(:post, friends_ids_path, options, &block)
70
58
  end
71
59
 
72
60
  private
73
61
 
74
- def user_management(path, user_id, error_msg, &block)
75
- user_id = user_id.join(',') if user_id.kind_of?(Array)
76
-
77
- http = connection.post(:path => path, :body => { 'user_id' => user_id })
78
- http.callback do
79
- if http.response_header.status == 200
80
- block.call if block && block.kind_of?(Proc)
81
- else
82
- @on_error.call(error_msg) if @on_error && @on_error.kind_of?(Proc)
83
- end
84
- end
85
- http.errback do
86
- @on_error.call(error_msg) if @on_error && @on_error.kind_of?(Proc)
87
- end
88
- end
89
-
90
62
  def connection
91
- return @conn if @conn
63
+ return @conn if defined?(@conn)
92
64
 
93
65
  @conn = EventMachine::HttpRequest.new('https://sitestream.twitter.com/')
94
66
  @conn.use EventMachine::Middleware::OAuth, oauth_configuration
@@ -119,5 +91,32 @@ module TweetStream
119
91
  def friends_ids_path
120
92
  @config_uri + '/friends/ids.json'
121
93
  end
94
+
95
+ def request(method, path, options, &block)
96
+ error_msg = options.delete(:error_msg)
97
+
98
+ http = connection.send(method, options.merge(:path => path))
99
+ http.callback do
100
+ if http.response_header.status == 200
101
+ if block && block.kind_of?(Proc)
102
+ if block.arity == 1
103
+ block.call http.response
104
+ else
105
+ block.call
106
+ end
107
+ end
108
+ else
109
+ @on_error.call(error_msg) if @on_error && @on_error.kind_of?(Proc)
110
+ end
111
+ end
112
+ http.errback do
113
+ @on_error.call(error_msg) if @on_error && @on_error.kind_of?(Proc)
114
+ end
115
+ end
116
+
117
+ def normalized_user_ids(user_id)
118
+ user_id.join(',') if user_id.kind_of?(Array)
119
+ end
120
+
122
121
  end
123
122
  end
@@ -1,3 +1,3 @@
1
1
  module TweetStream
2
- VERSION = '2.0.1' unless defined?(TweetStream::VERSION)
2
+ VERSION = '2.1.0' unless defined?(TweetStream::VERSION)
3
3
  end
File without changes
File without changes
data/spec/spec_helper.rb CHANGED
@@ -17,7 +17,7 @@ def sample_tweets
17
17
  return @tweets if @tweets
18
18
 
19
19
  @tweets = []
20
- Yajl::Parser.parse(File.open(File.dirname(__FILE__) + '/data/statuses.json', 'r'), :symbolize_keys => true) do |hash|
20
+ Yajl::Parser.parse(fixture('statuses.json'), :symbolize_keys => true) do |hash|
21
21
  @tweets << hash
22
22
  end
23
23
  @tweets
@@ -27,7 +27,7 @@ def sample_direct_messages
27
27
  return @direct_messages if @direct_messages
28
28
 
29
29
  @direct_messages = []
30
- Yajl::Parser.parse(File.open(File.dirname(__FILE__) + '/data/direct_messages.json', 'r')) do |hash|
30
+ Yajl::Parser.parse(fixture('direct_messages.json'), :symbolize_keys => true) do |hash|
31
31
  @direct_messages << hash
32
32
  end
33
33
  @direct_messages
@@ -0,0 +1,81 @@
1
+ require 'spec_helper'
2
+
3
+ describe TweetStream::Client do
4
+ before do
5
+ @stream = stub("EM::Twitter::Client",
6
+ :connect => true,
7
+ :unbind => true,
8
+ :each => true,
9
+ :on_error => true,
10
+ :on_max_reconnects => true,
11
+ :on_reconnect => true,
12
+ :connection_completed => true,
13
+ :on_no_data_received => true,
14
+ :on_unauthorized => true,
15
+ :on_enhance_your_calm => true
16
+ )
17
+ EM.stub!(:run).and_yield
18
+ EM::Twitter::Client.stub!(:connect).and_return(@stream)
19
+ end
20
+
21
+ describe "basic auth" do
22
+ before do
23
+ TweetStream.configure do |config|
24
+ config.username = 'tweetstream'
25
+ config.password = 'rubygem'
26
+ config.auth_method = :basic
27
+ end
28
+
29
+ @client = TweetStream::Client.new
30
+ end
31
+
32
+ it 'should try to connect via a JSON stream with basic auth' do
33
+ EM::Twitter::Client.should_receive(:connect).with(
34
+ :path => URI.parse('/1/statuses/filter.json'),
35
+ :method => 'POST',
36
+ :user_agent => TweetStream::Configuration::DEFAULT_USER_AGENT,
37
+ :on_inited => nil,
38
+ :params => {:track => 'monday'},
39
+ :basic => {
40
+ :username => 'tweetstream',
41
+ :password => 'rubygem'
42
+ }
43
+ ).and_return(@stream)
44
+
45
+ @client.track('monday')
46
+ end
47
+ end
48
+
49
+ describe "oauth" do
50
+ before do
51
+ TweetStream.configure do |config|
52
+ config.consumer_key = '123456789'
53
+ config.consumer_secret = 'abcdefghijklmnopqrstuvwxyz'
54
+ config.oauth_token = '123456789'
55
+ config.oauth_token_secret = 'abcdefghijklmnopqrstuvwxyz'
56
+ config.auth_method = :oauth
57
+ end
58
+
59
+ @client = TweetStream::Client.new
60
+ end
61
+
62
+ it 'should try to connect via a JSON stream with oauth' do
63
+ EM::Twitter::Client.should_receive(:connect).with(
64
+ :path => URI.parse('/1/statuses/filter.json'),
65
+ :method => 'POST',
66
+ :user_agent => TweetStream::Configuration::DEFAULT_USER_AGENT,
67
+ :on_inited => nil,
68
+ :params => {:track => 'monday'},
69
+ :oauth => {
70
+ :consumer_key => '123456789',
71
+ :consumer_secret => 'abcdefghijklmnopqrstuvwxyz',
72
+ :token => '123456789',
73
+ :token_secret => 'abcdefghijklmnopqrstuvwxyz'
74
+ }
75
+ ).and_return(@stream)
76
+
77
+ @client.track('monday')
78
+ end
79
+ end
80
+
81
+ end
@@ -0,0 +1,125 @@
1
+ require 'spec_helper'
2
+
3
+ describe TweetStream::Client do
4
+ before(:each) do
5
+ TweetStream.configure do |config|
6
+ config.consumer_key = 'abc'
7
+ config.consumer_secret = 'def'
8
+ config.oauth_token = '123'
9
+ config.oauth_token_secret = '456'
10
+ end
11
+ @client = TweetStream::Client.new
12
+
13
+ @stream = stub("EM::Twitter::Client",
14
+ :connect => true,
15
+ :unbind => true,
16
+ :each => true,
17
+ :on_error => true,
18
+ :on_max_reconnects => true,
19
+ :on_reconnect => true,
20
+ :connection_completed => true,
21
+ :on_no_data_received => true,
22
+ :on_unauthorized => true,
23
+ :on_enhance_your_calm => true
24
+ )
25
+ EM.stub!(:run).and_yield
26
+ EM::Twitter::Client.stub!(:connect).and_return(@stream)
27
+ end
28
+
29
+ describe "Site Stream support" do
30
+ context "when calling #sitestream" do
31
+ it "sends the sitestream host" do
32
+ EM::Twitter::Client.should_receive(:connect).with(hash_including(:host => "sitestream.twitter.com")).and_return(@stream)
33
+ @client.sitestream
34
+ end
35
+
36
+ it "uses the sitestream uri" do
37
+ EM::Twitter::Client.should_receive(:connect).with(hash_including(:path => "/2b/site.json")).and_return(@stream)
38
+ @client.sitestream
39
+ end
40
+
41
+ it 'supports "with followings" when followings set as a boolean' do
42
+ @client.should_receive(:start).once.with('', hash_including(:with => 'followings')).and_return(@stream)
43
+ @client.sitestream(['115192457'], :followings => true)
44
+ end
45
+
46
+ it 'supports "with followings" when followings set as an option' do
47
+ @client.should_receive(:start).once.with('', hash_including(:with => 'followings')).and_return(@stream)
48
+ @client.sitestream(['115192457'], :with => 'followings')
49
+ end
50
+
51
+ it 'supports "with user"' do
52
+ @client.should_receive(:start).once.with('', hash_including(:with => 'user')).and_return(@stream)
53
+ @client.sitestream(['115192457'], :with => 'user')
54
+ end
55
+
56
+ it 'supports "replies all"' do
57
+ @client.should_receive(:start).once.with('', hash_including(:replies => 'all')).and_return(@stream)
58
+ @client.sitestream(['115192457'], :replies => 'all')
59
+ end
60
+
61
+ context 'control management' do
62
+ before do
63
+ @control_response = {"control" =>
64
+ {
65
+ "control_uri" =>"/2b/site/c/01_225167_334389048B872A533002B34D73F8C29FD09EFC50"
66
+ }
67
+ }
68
+ end
69
+ it 'assigns the control_uri' do
70
+ @stream.should_receive(:each).and_yield(@control_response.to_json)
71
+ @client.sitestream
72
+
73
+ @client.control_uri.should eq("/2b/site/c/01_225167_334389048B872A533002B34D73F8C29FD09EFC50")
74
+ end
75
+
76
+ it 'instantiates a SiteStreamClient' do
77
+ @stream.should_receive(:each).and_yield(@control_response.to_json)
78
+ @client.sitestream
79
+
80
+ @client.control.should be_kind_of(TweetStream::SiteStreamClient)
81
+ end
82
+
83
+ it "passes the client's on_error to the SiteStreamClient" do
84
+ called = false
85
+ @client.on_error { |err| called = true }
86
+ @stream.should_receive(:each).and_yield(@control_response.to_json)
87
+ @client.sitestream
88
+
89
+ @client.control.on_error.call
90
+
91
+ called.should be_true
92
+ end
93
+ end
94
+
95
+ context 'data handling' do
96
+ before do
97
+ tweet = sample_tweets[0]
98
+ @ss_message = {'for_user' => '12345', 'message' => {'id' => 123, 'user' => {'screen_name' => 'monkey'}, 'text' => 'Oo oo aa aa'}}
99
+ end
100
+
101
+ it 'yields a site stream message' do
102
+ @stream.should_receive(:each).and_yield(@ss_message.to_json)
103
+ yielded_status = nil
104
+ @client.sitestream do |message|
105
+ yielded_status = message
106
+ end
107
+ yielded_status.should_not be_nil
108
+ yielded_status[:for_user].should == '12345'
109
+ yielded_status[:message][:user][:screen_name].should == 'monkey'
110
+ yielded_status[:message][:text].should == 'Oo oo aa aa'
111
+ end
112
+ it 'yields itself if block has an arity of 2' do
113
+ @stream.should_receive(:each).and_yield(@ss_message.to_json)
114
+ yielded_client = nil
115
+ @client.sitestream do |_, client|
116
+ yielded_client = client
117
+ end
118
+ yielded_client.should_not be_nil
119
+ yielded_client.should == @client
120
+ end
121
+ end
122
+ end
123
+ end
124
+
125
+ end
@@ -58,49 +58,43 @@ describe TweetStream::Client do
58
58
  :on_reconnect => true,
59
59
  :connection_completed => true,
60
60
  :on_no_data_received => true,
61
- :on_unauthorized => true
61
+ :on_unauthorized => true,
62
+ :on_enhance_your_calm => true
62
63
  )
63
64
  EM.stub!(:run).and_yield
64
65
  EM::Twitter::Client.stub!(:connect).and_return(@stream)
65
66
  end
66
67
 
67
- it 'should try to connect via a JSON stream with basic auth' do
68
- EM::Twitter::Client.should_receive(:connect).with(
69
- :path => URI.parse('/1/statuses/filter.json'),
70
- :method => 'POST',
71
- :user_agent => TweetStream::Configuration::DEFAULT_USER_AGENT,
72
- :on_inited => nil,
73
- :params => { :track => 'monday'},
74
- :oauth => {
75
- :consumer_key => 'abc',
76
- :consumer_secret => 'def',
77
- :token => '123',
78
- :token_secret => '456'
79
- }
80
- ).and_return(@stream)
81
-
82
- @client.track('monday')
68
+ it 'connects if the reactor is already running' do
69
+ EM.stub!(:reactor_running?).and_return(true)
70
+ @client.should_receive(:connect)
71
+ @client.track('abc')
72
+ end
73
+
74
+ it 'starts the reactor if not already running' do
75
+ EM.should_receive(:run).once
76
+ @client.track('abc')
83
77
  end
84
78
 
85
79
  describe '#each' do
86
- it 'should call the appropriate parser' do
80
+ it 'calls the appropriate parser' do
87
81
  @client = TweetStream::Client.new
88
82
  MultiJson.should_receive(:decode).and_return({})
89
83
  @stream.should_receive(:each).and_yield(sample_tweets[0].to_json)
90
84
  @client.track('abc','def')
91
85
  end
92
86
 
93
- it 'should yield a Twitter::Status' do
87
+ it 'yields a Twitter::Status' do
94
88
  @stream.should_receive(:each).and_yield(sample_tweets[0].to_json)
95
89
  @client.track('abc'){|s| s.should be_kind_of(Twitter::Status)}
96
90
  end
97
91
 
98
- it 'should also yield the client if a block with arity 2 is given' do
92
+ it 'yields the client if a block with arity 2 is given' do
99
93
  @stream.should_receive(:each).and_yield(sample_tweets[0].to_json)
100
94
  @client.track('abc'){|s,c| c.should == @client}
101
95
  end
102
96
 
103
- it 'should include the proper values' do
97
+ it 'includes the proper values' do
104
98
  tweet = sample_tweets[0]
105
99
  tweet[:id] = 123
106
100
  tweet[:user][:screen_name] = 'monkey'
@@ -113,7 +107,7 @@ describe TweetStream::Client do
113
107
  end
114
108
  end
115
109
 
116
- it 'should call the on_scrub_geo if specified' do
110
+ it 'should call the on_scrub_geo callback if specified' do
117
111
  scrub_geo = '{ "scrub_geo": { "user_id": 1234, "user_id_str": "1234", "up_to_status_id":9876, "up_to_status_id_string": "9876" } }'
118
112
  @stream.should_receive(:each).and_yield(scrub_geo)
119
113
  @client.on_scrub_geo do |up_to_status_id, user_id|
@@ -122,7 +116,7 @@ describe TweetStream::Client do
122
116
  end.track('abc')
123
117
  end
124
118
 
125
- it 'should call the delete if specified' do
119
+ it 'calls the on_delete callback' do
126
120
  delete = '{ "delete": { "status": { "id": 1234, "user_id": 3 } } }'
127
121
  @stream.should_receive(:each).and_yield(delete)
128
122
  @client.on_delete do |id, user_id|
@@ -131,7 +125,7 @@ describe TweetStream::Client do
131
125
  end.track('abc')
132
126
  end
133
127
 
134
- it 'should call the on_limit if specified' do
128
+ it 'calls the on_limit callback' do
135
129
  limit = '{ "limit": { "track": 1234 } }'
136
130
  @stream.should_receive(:each).and_yield(limit)
137
131
  @client.on_limit do |track|
@@ -192,8 +186,8 @@ describe TweetStream::Client do
192
186
  context 'using on_direct_message' do
193
187
  it 'yields a DirectMessage' do
194
188
  direct_message = sample_direct_messages[0]
195
- direct_message["direct_message"]["id"] = 1234
196
- direct_message["direct_message"]["sender"]["screen_name"] = "coder"
189
+ direct_message[:direct_message][:id] = 1234
190
+ direct_message[:direct_message][:sender][:screen_name] = "coder"
197
191
  @stream.should_receive(:each).and_yield(direct_message.to_json)
198
192
  yielded_dm = nil
199
193
  @client.on_direct_message do |dm|
@@ -238,9 +232,7 @@ describe TweetStream::Client do
238
232
  end
239
233
 
240
234
  it 'should return the block when defined' do
241
- @client.on_error do |m|
242
- puts 'ohai'
243
- end
235
+ @client.on_error { |m| true; }
244
236
  @client.on_error.should be_kind_of(Proc)
245
237
  end
246
238
 
@@ -252,10 +244,7 @@ describe TweetStream::Client do
252
244
  describe '#on_max_reconnects' do
253
245
  it 'should raise a ReconnectError' do
254
246
  @stream.should_receive(:on_max_reconnects).and_yield(30, 20)
255
- lambda{@client.track('abc')}.should raise_error(TweetStream::ReconnectError) do |e|
256
- e.timeout.should == 30
257
- e.retries.should == 20
258
- end
247
+ lambda{@client.track('abc')}.should raise_error(TweetStream::ReconnectError, "Failed to reconnect after 20 tries.")
259
248
  end
260
249
  end
261
250
  end
@@ -326,7 +315,7 @@ describe TweetStream::Client do
326
315
  end
327
316
  end
328
317
 
329
- %w(on_delete on_limit on_inited on_reconnect on_no_data_received on_unauthorized).each do |proc_setter|
318
+ %w(on_delete on_limit on_inited on_reconnect on_no_data_received on_unauthorized on_enhance_your_calm).each do |proc_setter|
330
319
  describe "##{proc_setter}" do
331
320
  it 'should set when a block is given' do
332
321
  proc = Proc.new{|a,b| puts a }
@@ -373,6 +362,7 @@ describe TweetStream::Client do
373
362
  :connection_completed => true,
374
363
  :on_no_data_received => true,
375
364
  :on_unauthorized => true,
365
+ :on_enhance_your_calm => true,
376
366
  :stop => true
377
367
  )
378
368
  EM::Twitter::Client.stub!(:connect).and_return(@stream)
@@ -390,184 +380,4 @@ describe TweetStream::Client do
390
380
  @client.stop_stream
391
381
  end
392
382
  end
393
-
394
- describe "basic auth" do
395
- before do
396
- TweetStream.configure do |config|
397
- config.username = 'tweetstream'
398
- config.password = 'rubygem'
399
- config.auth_method = :basic
400
- end
401
- @client = TweetStream::Client.new
402
-
403
- @stream = stub("EM::Twitter::Client",
404
- :connect => true,
405
- :unbind => true,
406
- :each => true,
407
- :on_error => true,
408
- :on_max_reconnects => true,
409
- :on_reconnect => true,
410
- :connection_completed => true,
411
- :on_no_data_received => true,
412
- :on_unauthorized => true
413
- )
414
- EM.stub!(:run).and_yield
415
- EM::Twitter::Client.stub!(:connect).and_return(@stream)
416
- end
417
-
418
- it 'should try to connect via a JSON stream with oauth' do
419
- EM::Twitter::Client.should_receive(:connect).with(
420
- :path => URI.parse('/1/statuses/filter.json'),
421
- :method => 'POST',
422
- :user_agent => TweetStream::Configuration::DEFAULT_USER_AGENT,
423
- :on_inited => nil,
424
- :params => {:track => 'monday'},
425
- :basic => {
426
- :username => 'tweetstream',
427
- :password => 'rubygem'
428
- }
429
- ).and_return(@stream)
430
-
431
- @client.track('monday')
432
- end
433
- end
434
-
435
- describe "oauth" do
436
- describe '#start' do
437
- before do
438
- TweetStream.configure do |config|
439
- config.consumer_key = '123456789'
440
- config.consumer_secret = 'abcdefghijklmnopqrstuvwxyz'
441
- config.oauth_token = '123456789'
442
- config.oauth_token_secret = 'abcdefghijklmnopqrstuvwxyz'
443
- config.auth_method = :oauth
444
- end
445
- @client = TweetStream::Client.new
446
-
447
- @stream = stub("EM::Twitter::Client",
448
- :connect => true,
449
- :unbind => true,
450
- :each => true,
451
- :on_error => true,
452
- :on_max_reconnects => true,
453
- :on_reconnect => true,
454
- :connection_completed => true,
455
- :on_no_data_received => true,
456
- :on_unauthorized => true
457
- )
458
- EM.stub!(:run).and_yield
459
- EM::Twitter::Client.stub!(:connect).and_return(@stream)
460
- end
461
-
462
- it 'should try to connect via a JSON stream with oauth' do
463
- EM::Twitter::Client.should_receive(:connect).with(
464
- :path => URI.parse('/1/statuses/filter.json'),
465
- :method => 'POST',
466
- :user_agent => TweetStream::Configuration::DEFAULT_USER_AGENT,
467
- :on_inited => nil,
468
- :params => {:track => 'monday'},
469
- :oauth => {
470
- :consumer_key => '123456789',
471
- :consumer_secret => 'abcdefghijklmnopqrstuvwxyz',
472
- :token => '123456789',
473
- :token_secret => 'abcdefghijklmnopqrstuvwxyz'
474
- }
475
- ).and_return(@stream)
476
-
477
- @client.track('monday')
478
- end
479
-
480
- context "when calling #userstream" do
481
- it "sends the userstream host" do
482
- EM::Twitter::Client.should_receive(:connect).with(hash_including(:host => "userstream.twitter.com")).and_return(@stream)
483
- @client.userstream
484
- end
485
-
486
- it "uses the userstream uri" do
487
- EM::Twitter::Client.should_receive(:connect).with(hash_including(:path => "/2/user.json")).and_return(@stream)
488
- @client.userstream
489
- end
490
- end
491
-
492
- context "when calling #sitestream" do
493
- it "sends the sitestream host" do
494
- EM::Twitter::Client.should_receive(:connect).with(hash_including(:host => "sitestream.twitter.com")).and_return(@stream)
495
- @client.sitestream
496
- end
497
-
498
- it "uses the userstream uri" do
499
- EM::Twitter::Client.should_receive(:connect).with(hash_including(:path => "/2b/site.json")).and_return(@stream)
500
- @client.sitestream
501
- end
502
-
503
- it 'supports the "with followings"' do
504
- @client.should_receive(:start).once.with('', hash_including(:with => 'followings')).and_return(@stream)
505
- @client.sitestream(['115192457'], :followings => true)
506
- end
507
-
508
- context 'control management' do
509
- before do
510
- @control_response = {"control" =>
511
- {
512
- "control_uri" =>"/2b/site/c/01_225167_334389048B872A533002B34D73F8C29FD09EFC50"
513
- }
514
- }
515
- end
516
- it 'assigns the control_uri' do
517
- @stream.should_receive(:each).and_yield(@control_response.to_json)
518
- @client.sitestream
519
-
520
- @client.control_uri.should eq("/2b/site/c/01_225167_334389048B872A533002B34D73F8C29FD09EFC50")
521
- end
522
-
523
- it 'instantiates a SiteStreamClient' do
524
- @stream.should_receive(:each).and_yield(@control_response.to_json)
525
- @client.sitestream
526
-
527
- @client.control.should be_kind_of(TweetStream::SiteStreamClient)
528
- end
529
-
530
- it "passes the client's on_error to the SiteStreamClient" do
531
- called = false
532
- @client.on_error { |err| called = true }
533
- @stream.should_receive(:each).and_yield(@control_response.to_json)
534
- @client.sitestream
535
-
536
- @client.control.on_error.call
537
-
538
- called.should be_true
539
- end
540
- end
541
-
542
- context 'data handling' do
543
- before do
544
- tweet = sample_tweets[0]
545
- @ss_message = {'for_user' => '12345', 'message' => {'id' => 123, 'user' => {'screen_name' => 'monkey'}, 'text' => 'Oo oo aa aa'}}
546
- end
547
-
548
- it 'yields a site stream message' do
549
- @stream.should_receive(:each).and_yield(@ss_message.to_json)
550
- yielded_status = nil
551
- @client.sitestream do |message|
552
- yielded_status = message
553
- end
554
- yielded_status.should_not be_nil
555
- yielded_status[:for_user].should == '12345'
556
- yielded_status[:message][:user][:screen_name].should == 'monkey'
557
- yielded_status[:message][:text].should == 'Oo oo aa aa'
558
- end
559
- it 'yields itself if block has an arity of 2' do
560
- @stream.should_receive(:each).and_yield(@ss_message.to_json)
561
- yielded_client = nil
562
- @client.sitestream do |_, client|
563
- yielded_client = client
564
- end
565
- yielded_client.should_not be_nil
566
- yielded_client.should == @client
567
- end
568
- end
569
- end
570
- end
571
- end
572
-
573
383
  end
@@ -0,0 +1,63 @@
1
+ require 'spec_helper'
2
+
3
+ describe TweetStream::Client do
4
+ before(:each) do
5
+ TweetStream.configure do |config|
6
+ config.consumer_key = 'abc'
7
+ config.consumer_secret = 'def'
8
+ config.oauth_token = '123'
9
+ config.oauth_token_secret = '456'
10
+ end
11
+ @client = TweetStream::Client.new
12
+
13
+ @stream = stub("EM::Twitter::Client",
14
+ :connect => true,
15
+ :unbind => true,
16
+ :each => true,
17
+ :on_error => true,
18
+ :on_max_reconnects => true,
19
+ :on_reconnect => true,
20
+ :connection_completed => true,
21
+ :on_no_data_received => true,
22
+ :on_unauthorized => true,
23
+ :on_enhance_your_calm => true
24
+ )
25
+ EM.stub!(:run).and_yield
26
+ EM::Twitter::Client.stub!(:connect).and_return(@stream)
27
+ end
28
+
29
+ describe "User Stream support" do
30
+ context 'when calling #userstream' do
31
+ it "sends the userstream host" do
32
+ EM::Twitter::Client.should_receive(:connect).with(hash_including(:host => "userstream.twitter.com")).and_return(@stream)
33
+ @client.userstream
34
+ end
35
+
36
+ it "uses the userstream uri" do
37
+ EM::Twitter::Client.should_receive(:connect).with(hash_including(:path => "/2/user.json")).and_return(@stream)
38
+ @client.userstream
39
+ end
40
+
41
+ it 'supports "replies"' do
42
+ @client.should_receive(:start).once.with('', hash_including(:replies => 'all')).and_return(@stream)
43
+ @client.userstream(:replies => 'all')
44
+ end
45
+
46
+ it 'supports "stall_warnings"' do
47
+ @client.should_receive(:start).once.with('', hash_including(:stall_warnings => 'true')).and_return(@stream)
48
+ @client.userstream(:stall_warnings => 'true')
49
+ end
50
+
51
+ it 'supports "with followings"' do
52
+ @client.should_receive(:start).once.with('', hash_including(:with => 'followings')).and_return(@stream)
53
+ @client.userstream(:with => 'followings')
54
+ end
55
+
56
+ it 'supports "with user"' do
57
+ @client.should_receive(:start).once.with('', hash_including(:with => 'user')).and_return(@stream)
58
+ @client.userstream(:with => 'user')
59
+ end
60
+ end
61
+ end
62
+
63
+ end
@@ -18,4 +18,12 @@ describe TweetStream::Daemon do
18
18
  client.app_name.should eq('tweet_tracker')
19
19
  end
20
20
  end
21
+
22
+ describe '#start' do
23
+ it 'starts the daemon' do
24
+ client = TweetStream::Daemon.new
25
+ Daemons.should_receive(:run_proc).once
26
+ client.track('intridea')
27
+ end
28
+ end
21
29
  end
@@ -101,6 +101,7 @@ describe TweetStream::SiteStreamClient do
101
101
  end
102
102
  called.should be_true
103
103
  end
104
+
104
105
  end
105
106
  end
106
107
 
@@ -229,4 +230,4 @@ describe TweetStream::SiteStreamClient do
229
230
  end
230
231
  end
231
232
 
232
- end
233
+ end
@@ -16,7 +16,8 @@ describe TweetStream do
16
16
  :on_reconnect => true,
17
17
  :connection_completed => true,
18
18
  :on_no_data_received => true,
19
- :on_unauthorized => true
19
+ :on_unauthorized => true,
20
+ :on_enhance_your_calm => true
20
21
  )
21
22
  EM.stub!(:run).and_yield
22
23
  EM::Twitter::Client.stub!(:connect).and_return(@stream)
data/tweetstream.gemspec CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |s|
12
12
  s.homepage = 'http://github.com/intridea/tweetstream'
13
13
 
14
14
  s.add_dependency 'em-twitter', '~> 0.1'
15
- s.add_dependency 'twitter', '~> 3.0.0.rc.1'
15
+ s.add_dependency 'twitter', '~> 3.2'
16
16
  s.add_dependency 'daemons', '~> 1.1'
17
17
  s.add_dependency 'multi_json', '~> 1.3'
18
18
  s.add_dependency 'em-http-request', '~> 1.0.2'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tweetstream
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-06-26 00:00:00.000000000 Z
13
+ date: 2012-07-17 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: em-twitter
17
- requirement: &70341058157740 !ruby/object:Gem::Requirement
17
+ requirement: &70273353293360 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ~>
@@ -22,21 +22,21 @@ dependencies:
22
22
  version: '0.1'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *70341058157740
25
+ version_requirements: *70273353293360
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: twitter
28
- requirement: &70341058157100 !ruby/object:Gem::Requirement
28
+ requirement: &70273353292340 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ~>
32
32
  - !ruby/object:Gem::Version
33
- version: 3.0.0.rc.1
33
+ version: '3.2'
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *70341058157100
36
+ version_requirements: *70273353292340
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: daemons
39
- requirement: &70341058156560 !ruby/object:Gem::Requirement
39
+ requirement: &70273353291760 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ~>
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: '1.1'
45
45
  type: :runtime
46
46
  prerelease: false
47
- version_requirements: *70341058156560
47
+ version_requirements: *70273353291760
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: multi_json
50
- requirement: &70341058155840 !ruby/object:Gem::Requirement
50
+ requirement: &70273353291280 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ~>
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: '1.3'
56
56
  type: :runtime
57
57
  prerelease: false
58
- version_requirements: *70341058155840
58
+ version_requirements: *70273353291280
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: em-http-request
61
- requirement: &70341058154820 !ruby/object:Gem::Requirement
61
+ requirement: &70273353290820 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ~>
@@ -66,10 +66,10 @@ dependencies:
66
66
  version: 1.0.2
67
67
  type: :runtime
68
68
  prerelease: false
69
- version_requirements: *70341058154820
69
+ version_requirements: *70273353290820
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: guard-rspec
72
- requirement: &70341058153760 !ruby/object:Gem::Requirement
72
+ requirement: &70273353290200 !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
75
75
  - - ! '>='
@@ -77,10 +77,10 @@ dependencies:
77
77
  version: '0'
78
78
  type: :development
79
79
  prerelease: false
80
- version_requirements: *70341058153760
80
+ version_requirements: *70273353290200
81
81
  - !ruby/object:Gem::Dependency
82
82
  name: json
83
- requirement: &70341058152780 !ruby/object:Gem::Requirement
83
+ requirement: &70273353289680 !ruby/object:Gem::Requirement
84
84
  none: false
85
85
  requirements:
86
86
  - - ! '>='
@@ -88,10 +88,10 @@ dependencies:
88
88
  version: '0'
89
89
  type: :development
90
90
  prerelease: false
91
- version_requirements: *70341058152780
91
+ version_requirements: *70273353289680
92
92
  - !ruby/object:Gem::Dependency
93
93
  name: pry
94
- requirement: &70341058152120 !ruby/object:Gem::Requirement
94
+ requirement: &70273353289140 !ruby/object:Gem::Requirement
95
95
  none: false
96
96
  requirements:
97
97
  - - ! '>='
@@ -99,10 +99,10 @@ dependencies:
99
99
  version: '0'
100
100
  type: :development
101
101
  prerelease: false
102
- version_requirements: *70341058152120
102
+ version_requirements: *70273353289140
103
103
  - !ruby/object:Gem::Dependency
104
104
  name: rake
105
- requirement: &70341058151700 !ruby/object:Gem::Requirement
105
+ requirement: &70273353288560 !ruby/object:Gem::Requirement
106
106
  none: false
107
107
  requirements:
108
108
  - - ! '>='
@@ -110,10 +110,10 @@ dependencies:
110
110
  version: '0'
111
111
  type: :development
112
112
  prerelease: false
113
- version_requirements: *70341058151700
113
+ version_requirements: *70273353288560
114
114
  - !ruby/object:Gem::Dependency
115
115
  name: rdiscount
116
- requirement: &70341058151240 !ruby/object:Gem::Requirement
116
+ requirement: &70273353288060 !ruby/object:Gem::Requirement
117
117
  none: false
118
118
  requirements:
119
119
  - - ! '>='
@@ -121,10 +121,10 @@ dependencies:
121
121
  version: '0'
122
122
  type: :development
123
123
  prerelease: false
124
- version_requirements: *70341058151240
124
+ version_requirements: *70273353288060
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: rspec
127
- requirement: &70341058150560 !ruby/object:Gem::Requirement
127
+ requirement: &70273353287560 !ruby/object:Gem::Requirement
128
128
  none: false
129
129
  requirements:
130
130
  - - ! '>='
@@ -132,10 +132,10 @@ dependencies:
132
132
  version: '0'
133
133
  type: :development
134
134
  prerelease: false
135
- version_requirements: *70341058150560
135
+ version_requirements: *70273353287560
136
136
  - !ruby/object:Gem::Dependency
137
137
  name: simplecov
138
- requirement: &70341058133520 !ruby/object:Gem::Requirement
138
+ requirement: &70273353287060 !ruby/object:Gem::Requirement
139
139
  none: false
140
140
  requirements:
141
141
  - - ! '>='
@@ -143,10 +143,10 @@ dependencies:
143
143
  version: '0'
144
144
  type: :development
145
145
  prerelease: false
146
- version_requirements: *70341058133520
146
+ version_requirements: *70273353287060
147
147
  - !ruby/object:Gem::Dependency
148
148
  name: webmock
149
- requirement: &70341058132360 !ruby/object:Gem::Requirement
149
+ requirement: &70273353286220 !ruby/object:Gem::Requirement
150
150
  none: false
151
151
  requirements:
152
152
  - - ! '>='
@@ -154,10 +154,10 @@ dependencies:
154
154
  version: '0'
155
155
  type: :development
156
156
  prerelease: false
157
- version_requirements: *70341058132360
157
+ version_requirements: *70273353286220
158
158
  - !ruby/object:Gem::Dependency
159
159
  name: yajl-ruby
160
- requirement: &70341058130880 !ruby/object:Gem::Requirement
160
+ requirement: &70273353259820 !ruby/object:Gem::Requirement
161
161
  none: false
162
162
  requirements:
163
163
  - - ! '>='
@@ -165,10 +165,10 @@ dependencies:
165
165
  version: '0'
166
166
  type: :development
167
167
  prerelease: false
168
- version_requirements: *70341058130880
168
+ version_requirements: *70273353259820
169
169
  - !ruby/object:Gem::Dependency
170
170
  name: yard
171
- requirement: &70341058129920 !ruby/object:Gem::Requirement
171
+ requirement: &70273353267580 !ruby/object:Gem::Requirement
172
172
  none: false
173
173
  requirements:
174
174
  - - ! '>='
@@ -176,7 +176,7 @@ dependencies:
176
176
  version: '0'
177
177
  type: :development
178
178
  prerelease: false
179
- version_requirements: *70341058129920
179
+ version_requirements: *70273353267580
180
180
  description: TweetStream allows you to easily consume the Twitter Streaming API utilizing
181
181
  the YAJL Ruby gem.
182
182
  email:
@@ -206,15 +206,17 @@ files:
206
206
  - lib/tweetstream/client.rb
207
207
  - lib/tweetstream/configuration.rb
208
208
  - lib/tweetstream/daemon.rb
209
- - lib/tweetstream/error.rb
210
209
  - lib/tweetstream/site_stream_client.rb
211
210
  - lib/tweetstream/version.rb
212
- - spec/data/direct_messages.json
213
- - spec/data/statuses.json
211
+ - spec/fixtures/direct_messages.json
214
212
  - spec/fixtures/ids.json
215
213
  - spec/fixtures/info.json
214
+ - spec/fixtures/statuses.json
216
215
  - spec/spec_helper.rb
216
+ - spec/tweetstream/client_authentication_spec.rb
217
+ - spec/tweetstream/client_site_stream_spec.rb
217
218
  - spec/tweetstream/client_spec.rb
219
+ - spec/tweetstream/client_userstream_spec.rb
218
220
  - spec/tweetstream/daemon_spec.rb
219
221
  - spec/tweetstream/site_stream_client_spec.rb
220
222
  - spec/tweetstream_spec.rb
@@ -244,12 +246,15 @@ signing_key:
244
246
  specification_version: 3
245
247
  summary: TweetStream is a simple wrapper for consuming the Twitter Streaming API.
246
248
  test_files:
247
- - spec/data/direct_messages.json
248
- - spec/data/statuses.json
249
+ - spec/fixtures/direct_messages.json
249
250
  - spec/fixtures/ids.json
250
251
  - spec/fixtures/info.json
252
+ - spec/fixtures/statuses.json
251
253
  - spec/spec_helper.rb
254
+ - spec/tweetstream/client_authentication_spec.rb
255
+ - spec/tweetstream/client_site_stream_spec.rb
252
256
  - spec/tweetstream/client_spec.rb
257
+ - spec/tweetstream/client_userstream_spec.rb
253
258
  - spec/tweetstream/daemon_spec.rb
254
259
  - spec/tweetstream/site_stream_client_spec.rb
255
260
  - spec/tweetstream_spec.rb
@@ -1,15 +0,0 @@
1
- module TweetStream
2
- class Terminated < ::StandardError; end
3
- class Error < ::StandardError; end
4
- class ConnectionError < TweetStream::Error; end
5
- # A ReconnectError is raised when the maximum number of retries has
6
- # failed to re-establish a connection.
7
- class ReconnectError < StandardError
8
- attr_accessor :timeout, :retries
9
- def initialize(timeout, retries)
10
- self.timeout = timeout
11
- self.retries = retries
12
- super("Failed to reconnect after #{retries} tries.")
13
- end
14
- end
15
- end