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



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

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

Instance Method Details

#<<(data) ⇒ Object



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

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

#on_body(data) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/twitter/streaming/response.rb', line 23

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_status(_status) ⇒ Object



31
32
33
34
# File 'lib/twitter/streaming/response.rb', line 31

def on_status(_status)
  error = Twitter::Error::ERRORS[@parser.status_code]
  raise error if error
end