From 67b6d7d2634b03c74e78a73625051a71ff854691 Mon Sep 17 00:00:00 2001 From: Tim Carey-Smith Date: Sun, 1 Sep 2013 21:05:30 +1200 Subject: [PATCH] Move the parser into the response --- lib/twitter/streaming/connection.rb | 3 +-- lib/twitter/streaming/response.rb | 8 +++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/twitter/streaming/connection.rb b/lib/twitter/streaming/connection.rb index 96be36428..9febd67bb 100644 --- a/lib/twitter/streaming/connection.rb +++ b/lib/twitter/streaming/connection.rb @@ -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" diff --git a/lib/twitter/streaming/response.rb b/lib/twitter/streaming/response.rb index 3dc26e5b2..14ec6e40f 100644 --- a/lib/twitter/streaming/response.rb +++ b/lib/twitter/streaming/response.rb @@ -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))