Skip to content

Commit b6c795b

Browse files
committed
Consistent implementation of #wrap for Wrapper sub-class.
1 parent 7ace618 commit b6c795b

File tree

3 files changed

+24
-21
lines changed

3 files changed

+24
-21
lines changed

lib/protocol/http/body/buffered.rb

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,24 @@ module HTTP
1111
module Body
1212
# A body which buffers all it's contents.
1313
class Buffered < Readable
14-
# Wraps an array into a buffered body.
14+
# Tries to wrap an object in a {Buffered} instance.
1515
#
1616
# For compatibility, also accepts anything that behaves like an `Array(String)`.
1717
#
1818
# @parameter body [String | Array(String) | Readable | nil] the body to wrap.
1919
# @returns [Readable | nil] the wrapped body or nil if nil was given.
20-
def self.for(body)
21-
if body.is_a?(Readable)
22-
return body
23-
elsif body.is_a?(Array)
24-
return self.new(body)
25-
elsif body.is_a?(String)
26-
return self.new([body])
27-
elsif body
28-
return self.read(body)
20+
def self.wrap(object)
21+
if object.is_a?(Readable)
22+
return object
23+
elsif object.is_a?(Array)
24+
return self.new(object)
25+
elsif object.is_a?(String)
26+
return self.new([object])
27+
elsif object
28+
return self.read(object)
2929
end
3030
end
3131

32-
# @deprecated Use {#for} instead.
33-
def self.wrap(body)
34-
self.for(body)
35-
end
36-
3732
# Read the entire body into a buffered representation.
3833
#
3934
# @parameter body [Readable] the body to read.

lib/protocol/http/body/rewindable.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ module HTTP
1111
module Body
1212
# A body which buffers all it's contents as it is `#read`.
1313
class Rewindable < Wrapper
14-
def self.for(body)
15-
if body.rewindable?
16-
body
17-
else
18-
self.new(body)
14+
def self.wrap(message)
15+
if body = message.body
16+
if body.rewindable?
17+
body
18+
else
19+
message.body = self.new(body)
20+
end
1921
end
2022
end
2123

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

37-
# A rewindable body wraps some other body. Convert it to a buffered body
39+
# 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.
40+
#
41+
# @returns [Buffered] the buffered body.
3842
def buffered
3943
Buffered.new(@chunks)
4044
end

lib/protocol/http/body/wrapper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ module HTTP
1010
module Body
1111
# Wrapping body instance. Typically you'd override `#read`.
1212
class Wrapper < Readable
13+
# Wrap the body of the given message in a new instance of this class.
14+
#
15+
# @parameter message [Request | Response] the message to wrap.
16+
# @returns [Wrapper | nil] the wrapped body or nil if the body was nil.
1317
def self.wrap(message)
1418
if body = message.body
1519
message.body = self.new(body)

0 commit comments

Comments
 (0)