Class: Twitter::Streaming::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/twitter/streaming/response.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Twitter::Streaming::Response

Initializes a new Response object



12
13
14
15
16
# File 'lib/twitter/streaming/response.rb', line 12

def initialize(&block)
  @block     = block
  @parser    = Http::Parser.new(self)
  @tokenizer = BufferedTokenizer.new("\r\n")
end

Instance Method Details

#<<(data) ⇒ Object



18
19
20
# File 'lib/twitter/streaming/response.rb', line 18

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

#on_body(data) ⇒ Object



27
28
29
30
31
32
# File 'lib/twitter/streaming/response.rb', line 27

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

#on_headers_complete(_headers) ⇒ Object

Raises:

  • (error)


22
23
24
25
# File 'lib/twitter/streaming/response.rb', line 22

def on_headers_complete(_headers)
  error = Twitter::Error.errors[@parser.status_code]
  raise error.new if error
end