Class: Protocol::HTTP::Body::Readable

Inherits:
Object
  • Object
show all
Defined in:
lib/protocol/http/body/readable.rb

Overview

def finish -> buffer the stream and close it. def close(error = nil) -> close the stream immediately. end

Direct Known Subclasses

Buffered, File, Head, Wrapper

Instance Method Summary collapse

Instance Method Details

#call(stream) ⇒ Object

Write the body to the given stream.



54
55
56
57
58
59
60
# File 'lib/protocol/http/body/readable.rb', line 54

def call(stream)
  while chunk = self.read
    stream.write(chunk)
  end
ensure
  stream.close
end

#close(error = nil) ⇒ Object

The consumer can call stop to signal that the stream output has terminated.



21
22
# File 'lib/protocol/http/body/readable.rb', line 21

def close(error = nil)
end

#eachObject

Enumerate all chunks until finished, then invoke #close.



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/protocol/http/body/readable.rb', line 69

def each
  return to_enum(:each) unless block_given?
  
  begin
    while chunk = self.read
      yield chunk
    end
  ensure
    self.close($!)
  end
end

#empty?Boolean

Optimistically determine whether read (may) return any data. If this returns true, then calling read will definitely return nil. If this returns false, then calling read may return nil.

Returns:

  • (Boolean)


27
28
29
# File 'lib/protocol/http/body/readable.rb', line 27

def empty?
  false
end

#finishObject

Read all remaining chunks into a buffered body and close the underlying input.



63
64
65
66
# File 'lib/protocol/http/body/readable.rb', line 63

def finish
  # Internally, this invokes `self.each` which then invokes `self.close`.
  Buffered.for(self)
end

#joinObject

Read all remaining chunks into a single binary string using #each.



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/protocol/http/body/readable.rb', line 82

def join
  buffer = String.new.force_encoding(Encoding::BINARY)
  
  self.each do |chunk|
    buffer << chunk
  end
  
  if buffer.empty?
    return nil
  else
    return buffer
  end
end

#lengthObject



38
39
40
# File 'lib/protocol/http/body/readable.rb', line 38

def length
  nil
end

#readObject

Read the next available chunk.



43
44
45
# File 'lib/protocol/http/body/readable.rb', line 43

def read
  nil
end

#ready?Boolean

Whether calling read will return a chunk of data without blocking. We prefer pessimistic implementation, and thus default to false.

Returns:

  • (Boolean)


34
35
36
# File 'lib/protocol/http/body/readable.rb', line 34

def ready?
  false
end

#stream?Boolean

Should the internal mechanism prefer to use #call?

Returns:

  • (Boolean)


49
50
51
# File 'lib/protocol/http/body/readable.rb', line 49

def stream?
  false
end