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.



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

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.



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

def close(error = nil)
end

#eachObject

Enumerate all chunks until finished, then invoke ‘#close`.



85
86
87
88
89
90
91
# File 'lib/protocol/http/body/readable.rb', line 85

def each
  while chunk = self.read
    yield chunk
  end
ensure
  self.close($!)
end

#empty?Boolean

Will read return any data?

Returns:

  • (Boolean)


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

def empty?
  false
end

#finishObject

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



79
80
81
82
# File 'lib/protocol/http/body/readable.rb', line 79

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`.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/protocol/http/body/readable.rb', line 94

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

#lengthObject



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

def length
  nil
end

#readObject

Read the next available chunk.



59
60
61
# File 'lib/protocol/http/body/readable.rb', line 59

def read
  nil
end

#ready?Boolean

Whether calling read will block. We prefer pessimistic implementation, and thus default to ‘false`.

Returns:

  • (Boolean)


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

def ready?
  false
end

#stream?Boolean

Should the internal mechanism prefer to use #call?

Returns:

  • (Boolean)


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

def stream?
  false
end