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.



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

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.



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

def close(error = nil)
end

#eachObject

Enumerate all chunks until finished, then invoke #close.



68
69
70
71
72
73
74
# File 'lib/protocol/http/body/readable.rb', line 68

def each
  while chunk = self.read
    yield chunk
  end
ensure
  self.close($!)
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)


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

def empty?
  false
end

#finishObject

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



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

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.



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/protocol/http/body/readable.rb', line 77

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



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

def length
  nil
end

#readObject

Read the next available chunk.



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

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)


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

def ready?
  false
end

#stream?Boolean

Should the internal mechanism prefer to use #call?

Returns:

  • (Boolean)


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

def stream?
  false
end