lib/protocol/http/body/wrapper.rb in protocol-http-0.45.0 vs lib/protocol/http/body/wrapper.rb in protocol-http-0.46.0

- old
+ new

@@ -11,74 +11,96 @@ # Wrapping body instance. Typically you'd override `#read`. class Wrapper < Readable # Wrap the body of the given message in a new instance of this class. # # @parameter message [Request | Response] the message to wrap. - # @returns [Wrapper | nil] the wrapped body or nil if the body was nil. + # @returns [Wrapper | Nil] the wrapped body or `nil`` if the body was `nil`. def self.wrap(message) if body = message.body message.body = self.new(body) end end + # Initialize the wrapper with the given body. + # + # @parameter body [Readable] The body to wrap. def initialize(body) @body = body end - # The wrapped body. + # @attribute [Readable] The wrapped body. attr :body + # Close the body. + # + # @parameter error [Exception | Nil] The error that caused this stream to be closed, if any. def close(error = nil) @body.close(error) # It's a no-op: # super end + # Forwards to the wrapped body. def empty? @body.empty? end + # Forwards to the wrapped body. def ready? @body.ready? end + # Forwards to the wrapped body. def buffered @body.buffered end + # Forwards to the wrapped body. def rewind @body.rewind end + # Forwards to the wrapped body. def rewindable? @body.rewindable? end + # Forwards to the wrapped body. def length @body.length end - # Read the next available chunk. + # Forwards to the wrapped body. def read @body.read end + # Forwards to the wrapped body. def discard @body.discard end + # Convert the body to a hash suitable for serialization. + # + # @returns [Hash] The body as a hash. def as_json(...) { class: self.class.name, body: @body&.as_json } end + # Convert the body to JSON. + # + # @returns [String] The body as JSON. def to_json(...) as_json.to_json(...) end + # Inspect the wrapped body. The wrapper, by default, is transparent. + # + # @returns [String] a string representation of the wrapped body. def inspect @body.inspect end end end