33 lines
734 B
Ruby
33 lines
734 B
Ruby
|
require 'buftok'
|
||
|
|
||
|
module Twitter
|
||
|
module Streaming
|
||
|
class Response
|
||
|
# Initializes a new Response object
|
||
|
#
|
||
|
# @return [Twitter::Streaming::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)
|
||
|
error = Twitter::Error.errors[@parser.status_code]
|
||
|
fail error.new if error
|
||
|
end
|
||
|
|
||
|
def on_body(data)
|
||
|
@tokenizer.extract(data).each do |line|
|
||
|
next if line.empty?
|
||
|
@block.call(JSON.parse(line, :symbolize_names => true))
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|