Class: SimpleStream::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Response

Returns a new instance of Response.


9
10
11
12
13
# File 'lib/simple_stream/response.rb', line 9

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

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block


7
8
9
# File 'lib/simple_stream/response.rb', line 7

def block
  @block
end

#parserObject (readonly)

Returns the value of attribute parser


7
8
9
# File 'lib/simple_stream/response.rb', line 7

def parser
  @parser
end

#tokenizerObject (readonly)

Returns the value of attribute tokenizer


7
8
9
# File 'lib/simple_stream/response.rb', line 7

def tokenizer
  @tokenizer
end

Instance Method Details

#<<(data) ⇒ Object


15
16
17
# File 'lib/simple_stream/response.rb', line 15

def <<(data)
  parser << data
end

#on_body(data) ⇒ Object


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

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


19
20
21
22
# File 'lib/simple_stream/response.rb', line 19

def on_headers_complete(headers)
  # TODO
  p(status_code: parser.status_code, header: headers) unless parser.status_code == 200
end