Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty data callback #39

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion examples/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
:path => '/1/statuses/filter.json',
:auth => 'LOGIN:PASSWORD',
:method => 'POST',
:content => 'track=basketball,football,baseball,footy,soccer'
:params => {
:track => 'basketball,football,baseball,footy,soccer',
:locations => '-122.75,36.8,-121.75,37.8,-74,40,-73,41',
:follow => '12,13,15,16,20,87'
}
)

stream.each_item do |item|
Expand Down
18 changes: 4 additions & 14 deletions lib/twitter/json_stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class JSONStream < EventMachine::Connection
:method => 'GET',
:path => '/',
:content_type => "application/x-www-form-urlencoded",
:content => '',
:path => '/1/statuses/filter.json',
:host => 'stream.twitter.com',
:port => 443,
Expand All @@ -36,7 +35,6 @@ class JSONStream < EventMachine::Connection
:proxy => ENV['HTTP_PROXY'],
:auth => nil,
:oauth => {},
:filters => [],
:params => {},
:auto_reconnect => true
}
Expand Down Expand Up @@ -90,8 +88,6 @@ def on_reconnect &block
@reconnect_callback = block
end

# Called when no data has been received for NO_DATA_TIMEOUT seconds.
# Reconnecting is probably in order as per the Twitter recommendations
def on_no_data &block
@no_data_callback = block
end
Expand Down Expand Up @@ -127,7 +123,7 @@ def unbind
# Receives raw data from the HTTP connection and pushes it into the
# HTTP parser which then drives subsequent callbacks.
def receive_data(data)
@last_data_received_at = Time.now
@last_data_received_at = Time.now if !data.strip.empty?
@parser << data
end

Expand Down Expand Up @@ -246,7 +242,7 @@ def send_request
request_uri = "#{uri_base}:#{@options[:port]}#{request_uri}"
end

content = @options[:content]
content = ''

unless (q = query).empty?
if @options[:method].to_s.upcase == 'GET'
Expand Down Expand Up @@ -346,21 +342,15 @@ def uri_base
# Normalized query hash of escaped string keys and escaped string values
# nil values are skipped
def params
flat = {}
@options[:params].merge( :track => @options[:filters] ).each do |param, val|
next if val.to_s.empty? || (val.respond_to?(:empty?) && val.empty?)
val = val.join(",") if val.respond_to?(:join)
flat[param.to_s] = val.to_s
end
flat
@options[:params]
end

def query
params.map{|param, value| [escape(param), escape(value)].join("=")}.sort.join("&")
end

def escape str
URI.escape(str.to_s, /[^a-zA-Z0-9\-\.\_\~]/)
URI.escape(str.to_s)
end
end
end
16 changes: 15 additions & 1 deletion spec/twitter/json_stream_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,28 @@ def receive_data data
end

it "should call no data callback after no data received for 90 seconds" do
connect_stream :stop_in => 6 do
connect_stream(stop_in: 6, ssl: false) do
stream.last_data_received_at = Time.now - 88
stream.should_receive(:no_data).once
end
end

end

context "on empty data received" do
attr_reader :stream
before :each do
$data_to_send = "\n\r"
$close_connection = false
end
it "should call no data callback after empty data received for 90 seconds" do
connect_stream(stop_in: 6, ssl: false) do
stream.last_data_received_at = Time.now - 88
stream.should_receive(:no_data).once
end
end
end

context "on server unavailable" do

attr_reader :stream
Expand Down