Skip to content

Commit

Permalink
Move the parser into the response
Browse files Browse the repository at this point in the history
  • Loading branch information
halorgium authored and sferik committed Sep 6, 2013
1 parent 32fd733 commit 67b6d7d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
3 changes: 1 addition & 2 deletions lib/twitter/streaming/connection.rb
Expand Up @@ -8,13 +8,12 @@ class Connection

def stream(request, response)
client_context = OpenSSL::SSL::SSLContext.new
parser = Http::Parser.new(response)
client = TCPSocket.new(Resolv.getaddress(request.uri.host), request.uri.port)
ssl_client = OpenSSL::SSL::SSLSocket.new(client, client_context)
ssl_client.connect
request.stream(ssl_client)
while body = ssl_client.readpartial(1024)
parser << body
response << body
end
rescue EOFError
puts "Stream ended"
Expand Down
8 changes: 7 additions & 1 deletion lib/twitter/streaming/response.rb
Expand Up @@ -5,15 +5,21 @@ module Streaming
class Response
def initialize(&block)
@block = block
@parser = Http::Parser.new(self)
@tokenizer = BufferedTokenizer.new("\r\n")
end

def <<(data)
@parser << data
end

def on_headers_complete(headers)
# TODO: handle response codes
p(headers)
p(status_code: @parser.status_code, header: headers)
end

def on_body(data)
p(data: data)
@tokenizer.extract(data).each do |line|
next if line.empty?
@block.call(JSON.parse(line, :symbolize_names => true))
Expand Down

0 comments on commit 67b6d7d

Please sign in to comment.