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.



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

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.



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

def block
  @block
end

#parserObject (readonly)

Returns the value of attribute parser.



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

def parser
  @parser
end

#tokenizerObject (readonly)

Returns the value of attribute tokenizer.



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

def tokenizer
  @tokenizer
end

Instance Method Details

#<<(data) ⇒ Object



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

def <<(data)
  parser << data
end

#on_body(data) ⇒ Object



23
24
25
26
27
28
# File 'lib/simple_stream/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_headers_complete(headers) ⇒ Object



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

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