Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added on_interval callback

  • Loading branch information...
commit 5f7599c37a29066d756f4de8f16418ff6ebaea05 1 parent 0e84ea4
@stve stve authored
View
16 lib/tweetstream/client.rb
@@ -191,6 +191,16 @@ def on_inited(&block)
end
end
+ def on_interval(time_interval=nil, &block)
+ if block_given?
+ @on_interval_time = time_interval
+ @on_interval_proc = block
+ self
+ else
+ [@on_interval_time, @on_interval_proc]
+ end
+ end
+
def start(path, query_parameters = {}, &block) #:nodoc:
method = query_parameters.delete(:method) || :get
delete_proc = query_parameters.delete(:delete) || self.on_delete
@@ -206,6 +216,12 @@ def start(path, query_parameters = {}, &block) #:nodoc:
EventMachine.kqueue = EM.kqueue?
EventMachine::run {
+ if @on_interval_proc.is_a?(Proc)
+ timer = @on_interval_time || Configuration::DEFAULT_TIMER_INTERVAL
+ proc = @on_interval_proc
+ EM.add_periodic_timer(timer, &proc)
+ end
+
stream_params = {
:path => uri,
:method => method.to_s.upcase,
View
3  lib/tweetstream/configuration.rb
@@ -47,6 +47,9 @@ module Configuration
# By default, don't set a user oauth secret
DEFAULT_OAUTH_TOKEN_SECRET = nil
+ # Default time interval for use with on_interval
+ DEFAULT_TIMER_INTERVAL = 30
+
# @private
attr_accessor *VALID_OPTIONS_KEYS
View
17 spec/tweetstream/client_spec.rb
@@ -226,6 +226,23 @@
end
end
+ describe '#on_interval' do
+ it 'should set when a block is given' do
+ @client.on_interval(5) { puts 'hi' }
+ @client.on_interval[0].should == 5
+ @client.on_interval[1].should be_kind_of(Proc)
+ end
+
+ it 'should should create a periodic timer' do
+ # need to figure out a better way to test this
+ # for now, using on_inited to stop the reactor
+ proc = Proc.new{ puts 'hi' }
+ EM.should_receive(:add_periodic_timer).once.with(5)
+ @client.on_inited { EM.stop }.on_interval(5, &proc)
+ @client.track('go')
+ end
+ end
+
describe '#track' do
it 'should call #start with "statuses/filter" and the provided queries' do
@client.should_receive(:start).once.with('statuses/filter', :track => ['rock'], :method => :post)
Please sign in to comment.
Something went wrong with that request. Please try again.