Class: Twitter::Streaming::Response

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

Overview

Handles streaming response parsing

Instance Method Summary collapse

Constructor Details

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

Initializes a new Response object

Examples:

response = Twitter::Streaming::Response.new { |data| puts data }


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

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

Instance Method Details

#<<(data) ⇒ void

This method returns an undefined value.

Appends data to the parser

Examples:

response << data

Parameters:

  • data (String)

    The data to append.



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

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

#on_body(data) ⇒ void

This method returns an undefined value.

Handles body data from the response

Examples:

response.on_body(data)

Parameters:

  • data (String)

    The body data.



42
43
44
45
46
47
48
# File 'lib/twitter/streaming/response.rb', line 42

def on_body(data)
  @tokenizer.extract(data).each do |line|
    next if line.empty?

    @block.call(JSON.parse(line, symbolize_names: true)) # steep:ignore UnknownConstant
  end
end

#on_status(_status) ⇒ void

This method returns an undefined value.

Handles status code from the response

Examples:

response.on_status(200)

Parameters:

  • _status (Integer)

    The status code.



57
58
59
60
# File 'lib/twitter/streaming/response.rb', line 57

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