Sha256: 9702a587c37b5ac3b7c02f077d3b113f9f62f41fbae84abdf15158197424f441

Contents?: true

Size: 662 Bytes

Versions: 1

Compression:

Stored size: 662 Bytes

Contents

require 'buftok'
require 'json'
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

1 entries across 1 versions & 1 rubygems

Version Path
simple_stream-0.0.3 lib/simple_stream/response.rb