Sha256: 60632f668c1d76af0072aef9b91e62284854c07c35b4e5e11bd2ff8fa6990329

Contents?: true

Size: 783 Bytes

Versions: 4

Compression:

Stored size: 783 Bytes

Contents

require 'buftok'

module Twitter
  module Streaming
    class Response

      # Initializes a new Response object
      #
      # @return [Twitter::Streaming::Response]
      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: handle response codes
        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
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
twitter-5.1.1 lib/twitter/streaming/response.rb
twitter-5.1.0 lib/twitter/streaming/response.rb
twitter-5.0.1 lib/twitter/streaming/response.rb
twitter-5.0.0 lib/twitter/streaming/response.rb