Class: Riak::Util::Multipart::StreamParser

Inherits:
Object
  • Object
show all
Includes:
Riak::Util::Multipart, Translation
Defined in:
lib/riak/util/multipart/stream_parser.rb

Overview

This class is parses chunked/streamed multipart HTTP streams. It is used by streaming MapReduce queries, and in the future streaming key-lists (once implemented on the Riak side).

Constant Summary

Instance Method Summary (collapse)

Methods included from Translation

#i18n_scope, #t

Methods included from Riak::Util::Multipart

#extract_boundary, #parse

Constructor Details

- (StreamParser) initialize {|Hash| ... }

Creates a new StreamParser.

Example usage:

http.get(200, "/riak", "foo", {}, &StreamParser.new {|part| ... })

Yields:

  • (Hash)

    parts of the multipart/mixed stream, containing :headers and :body keys

Raises:

  • (ArgumentError)


20
21
22
23
24
25
# File 'lib/riak/util/multipart/stream_parser.rb', line 20

def initialize(&block)
  raise ArgumentError, t('missing_block') unless block_given?
  @buffer = ""
  @block = block
  @state = :get_boundary
end

Instance Method Details

- (Object) accept(chunk)

Accepts a chunk of the HTTP response stream, and yields to the block when appropriate.



29
30
31
32
# File 'lib/riak/util/multipart/stream_parser.rb', line 29

def accept(chunk)
  @buffer << chunk
  @state = send(@state)
end

- (Object) to_proc

Returns a Proc that can be passed to an HTTP request method.



35
36
37
# File 'lib/riak/util/multipart/stream_parser.rb', line 35

def to_proc
  method(:accept).to_proc
end