Sha256: 20b109bdda694e1880b302653861693c45999f2947bf36b836d68ee7c85985a1

Contents?: true

Size: 647 Bytes

Versions: 2

Compression:

Stored size: 647 Bytes

Contents

require 'buftok'
require 'http/parser'

module SimpleStream
  class Response
    attr_reader :block, :parser, :tokenizer

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

    def <<(data)
      parser << data
    end

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

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
simple_stream-0.0.2 lib/simple_stream/response.rb
simple_stream-0.0.1 lib/simple_stream/response.rb