Skip to content

Commit

Permalink
Consistent implementation of #wrap for Wrapper sub-class.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Aug 28, 2024
1 parent f381717 commit 4b5a295
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
25 changes: 10 additions & 15 deletions lib/protocol/http/body/buffered.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,24 @@ module HTTP
module Body
# A body which buffers all it's contents.
class Buffered < Readable
# Wraps an array into a buffered body.
# Tries to wrap an object in a {Buffered} instance.
#
# For compatibility, also accepts anything that behaves like an `Array(String)`.
#
# @parameter body [String | Array(String) | Readable | nil] the body to wrap.
# @returns [Readable | nil] the wrapped body or nil if nil was given.
def self.for(body)
if body.is_a?(Readable)
return body
elsif body.is_a?(Array)
return self.new(body)
elsif body.is_a?(String)
return self.new([body])
elsif body
return self.read(body)
def self.wrap(object)
if object.is_a?(Readable)
return object
elsif object.is_a?(Array)
return self.new(object)
elsif object.is_a?(String)
return self.new([object])
elsif object
return self.read(object)
end
end

# @deprecated Use {#for} instead.
def self.wrap(body)
self.for(body)
end

# Read the entire body into a buffered representation.
#
# @parameter body [Readable] the body to read.
Expand Down
16 changes: 10 additions & 6 deletions lib/protocol/http/body/rewindable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ module HTTP
module Body
# A body which buffers all it's contents as it is `#read`.
class Rewindable < Wrapper
def self.for(body)
if body.rewindable?
body
else
self.new(body)
def self.wrap(message)
if body = message.body
if body.rewindable?
body
else
message.body = self.new(body)
end
end
end

Expand All @@ -34,7 +36,9 @@ def ready?
(@index < @chunks.size) || super
end

# A rewindable body wraps some other body. Convert it to a buffered body
# A rewindable body wraps some other body. Convert it to a buffered body. The buffered body will share the same chunks as the rewindable body.
#
# @returns [Buffered] the buffered body.
def buffered
Buffered.new(@chunks)
end
Expand Down
4 changes: 4 additions & 0 deletions lib/protocol/http/body/wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ module HTTP
module Body
# 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.
def self.wrap(message)
if body = message.body
message.body = self.new(body)
Expand Down

0 comments on commit 4b5a295

Please sign in to comment.