Class: Pitchfork::Chunked::Body

Inherits:
Object
  • Object
show all
Defined in:
lib/pitchfork/chunked.rb

Overview

A body wrapper that emits chunked responses.

Direct Known Subclasses

TrailerBody

Constant Summary collapse

TERM =
"\r\n"
TAIL =
"0#{TERM}"

Instance Method Summary collapse

Constructor Details

#initialize(body) ⇒ Body

Store the response body to be chunked.



37
38
39
# File 'lib/pitchfork/chunked.rb', line 37

def initialize(body)
  @body = body
end

Instance Method Details

#closeObject

Close the response body if the response body supports it.



57
58
59
# File 'lib/pitchfork/chunked.rb', line 57

def close
  @body.close if @body.respond_to?(:close)
end

#each {|TAIL| ... } ⇒ Object

For each element yielded by the response body, yield the element in chunked encoding.

Yields:



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/pitchfork/chunked.rb', line 43

def each(&block)
  term = TERM
  @body.each do |chunk|
    size = chunk.bytesize
    next if size == 0

    yield [size.to_s(16), term, chunk.b, term].join
  end
  yield TAIL
  yield_trailers(&block)
  yield term
end