Skip to content

Commit 3dd3571

Browse files
author
Michael Bleigh
committed
Full test coverage of client, ready for 1.0 release.
1 parent d613a0a commit 3dd3571

File tree

8 files changed

+119
-19
lines changed

8 files changed

+119
-19
lines changed

lib/tweetstream/client.rb

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ def on_error(&block)
173173
@on_limit = block
174174
self
175175
else
176-
return Proc.new{|e| puts e.inspect}
177176
@on_limit
178177
end
179178
end
@@ -187,10 +186,6 @@ def start(path, query_parameters = {}, &block) #:nodoc:
187186
uri = method == :get ? build_uri(path, query_parameters) : build_uri(path)
188187

189188
EventMachine::run {
190-
@cancel_timer = EventMachine::PeriodicTimer.new(1) do
191-
EventMachine::stop_event_loop if @stop || TweetStream::Client.stop?
192-
end
193-
194189
@stream = Twitter::JSONStream.connect(
195190
:path => uri,
196191
:auth => "#{URI.encode self.username}:#{URI.encode self.password}",
@@ -230,24 +225,14 @@ def start(path, query_parameters = {}, &block) #:nodoc:
230225
}
231226
end
232227

233-
# Terminate the currently running TweetStream.
234-
def self.stop
235-
raise TweetStream::Terminated
236-
end
237-
238228
# Terminate the currently running TweetStream.
239229
def stop
240230
EventMachine.stop_event_loop
241-
true
242231
end
243232

233+
# Terminate the currently running TweetStream
244234
def self.stop
245235
EventMachine.stop_event_loop
246-
true
247-
end
248-
249-
def self.stop?
250-
@stop
251236
end
252237

253238
protected

lib/tweetstream/hash.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
class TweetStream::Hash < ::Hash #:nodoc: all
22
def initialize(other_hash = {})
33
other_hash.keys.each do |key|
4-
self[key.to_sym] = other_hash[key]
4+
value = other_hash[key]
5+
value = TweetStream::Hash.new(value) if value.is_a?(::Hash)
6+
self[key.to_sym] = value
57
end
68
end
79

lib/tweetstream/status.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@ def initialize(hash)
55
super
66
self[:user] = TweetStream::User.new(self[:user])
77
end
8+
9+
def id
10+
self[:id] || super
11+
end
812
end

lib/tweetstream/user.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# A simple Hash wrapper that gives you method-based
22
# access to user properties returned by the streamer.
33
class TweetStream::User < TweetStream::Hash
4-
4+
def id
5+
self[:id] || super
6+
end
57
end

spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require 'spec'
77
require 'spec/autorun'
88
require 'yajl'
9+
require 'json'
910

1011
def sample_tweets
1112
if @tweets

spec/tweetstream/client_spec.rb

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,98 @@
5353

5454
describe '#start' do
5555
before do
56+
@stream = stub("Twitter::JSONStream",
57+
:connect => true,
58+
:unbind => true,
59+
:each_item => true,
60+
:on_error => true,
61+
:on_max_reconnects => true,
62+
:connection_completed => true
63+
)
64+
EM.stub!(:run).and_yield
65+
Twitter::JSONStream.stub!(:connect).and_return(@stream)
5666
@client = TweetStream::Client.new('abc','def')
5767
end
58-
5968

69+
it 'should try to connect via a JSON stream' do
70+
Twitter::JSONStream.should_receive(:connect).with(
71+
:auth => 'abc:def',
72+
:content => 'track=monday',
73+
:path => URI.parse('/1/statuses/filter.json'),
74+
:method => 'POST',
75+
:user_agent => 'TweetStream'
76+
).and_return(@stream)
77+
78+
@client.track('monday')
79+
end
80+
81+
describe '#each_item' do
82+
it 'should call the appropriate parser' do
83+
@client = TweetStream::Client.new('abc','def',:active_support)
84+
TweetStream::Parsers::ActiveSupport.should_receive(:decode).and_return({})
85+
@stream.should_receive(:each_item).and_yield(sample_tweets[0].to_json)
86+
@client.track('abc','def')
87+
end
88+
89+
it 'should yield a TweetStream::Status' do
90+
@stream.should_receive(:each_item).and_yield(sample_tweets[0].to_json)
91+
@client.track('abc'){|s| s.should be_kind_of(TweetStream::Status)}
92+
end
93+
94+
it 'should also yield the client if a block with arity 2 is given' do
95+
@stream.should_receive(:each_item).and_yield(sample_tweets[0].to_json)
96+
@client.track('abc'){|s,c| c.should == @client}
97+
end
98+
99+
it 'should include the proper values' do
100+
tweet = sample_tweets[0]
101+
tweet[:id] = 123
102+
tweet[:user][:screen_name] = 'monkey'
103+
tweet[:text] = "Oo oo aa aa"
104+
@stream.should_receive(:each_item).and_yield(tweet.to_json)
105+
@client.track('abc') do |s|
106+
s[:id].should == 123
107+
s.user.screen_name.should == 'monkey'
108+
s.text.should == 'Oo oo aa aa'
109+
end
110+
end
111+
112+
it 'should call the on_delete if specified' do
113+
delete = '{ "delete": { "status": { "id": 1234, "user_id": 3 } } }'
114+
@stream.should_receive(:each_item).and_yield(delete)
115+
@client.on_delete do |id, user_id|
116+
id.should == 1234
117+
user_id.should == 3
118+
end.track('abc')
119+
end
120+
121+
it 'should call the on_limit if specified' do
122+
limit = '{ "limit": { "track": 1234 } }'
123+
@stream.should_receive(:each_item).and_yield(limit)
124+
@client.on_limit do |track|
125+
track.should == 1234
126+
end.track('abc')
127+
end
128+
end
129+
130+
describe '#on_error' do
131+
it 'should pass the message on to the error block' do
132+
@stream.should_receive(:on_error).and_yield('Uh oh')
133+
@client.on_error do |m|
134+
m.should == 'Uh oh'
135+
end.track('abc')
136+
end
137+
end
138+
139+
describe '#on_max_reconnects' do
140+
it 'should raise a ReconnectError' do
141+
@stream.should_receive(:on_max_reconnects).and_yield(30, 20)
142+
lambda{@client.track('abc')}.should raise_error(TweetStream::ReconnectError) do |e|
143+
e.timeout.should == 30
144+
e.retries.should == 20
145+
end
146+
end
147+
end
60148
end
61149

62150
describe ' API methods' do

spec/tweetstream/parser_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,16 @@
2424
end
2525
end
2626
end
27+
28+
class FakeParser
29+
def self.decode(text)
30+
{}
31+
end
32+
end
33+
34+
it 'should be settable to a class' do
35+
@client = TweetStream::Client.new('abc','def')
36+
@client.parser = FakeParser
37+
@client.parser.should == FakeParser
38+
end
2739
end

spec/tweetstream/status_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,10 @@
66
@status.user.is_a?(TweetStream::User).should be_true
77
@status.user.screen_name.should == 'bob'
88
end
9+
10+
it 'should override the #id method for itself and the user' do
11+
@status = TweetStream::Status.new(:id => 123, :user => {:id => 345})
12+
@status.id.should == 123
13+
@status.user.id.should == 345
14+
end
915
end

0 commit comments

Comments
 (0)