Skip to content

Commit f54c6db

Browse files
Improve consistency of Body #inspect.
1 parent f899b0f commit f54c6db

File tree

14 files changed

+98
-11
lines changed

14 files changed

+98
-11
lines changed

lib/protocol/http/body/buffered.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,10 @@ def rewind
153153
#
154154
# @returns [String] a string representation of the buffered body.
155155
def inspect
156-
if @chunks
157-
"\#<#{self.class} #{@chunks.size} chunks, #{self.length} bytes>"
156+
if @chunks and @chunks.size > 0
157+
"#<#{self.class} #{@index}/#{@chunks.size} chunks, #{self.length} bytes>"
158+
else
159+
"#<#{self.class} empty>"
158160
end
159161
end
160162
end

lib/protocol/http/body/completable.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ def close(error = nil)
5353

5454
super
5555
end
56+
57+
# Inspect the completable body.
58+
#
59+
# @returns [String] a string representation of the completable body.
60+
def inspect
61+
callback_status = @callback ? "callback pending" : "callback completed"
62+
return "#{super} | #<#{self.class} #{callback_status}>"
63+
end
5664
end
5765
end
5866
end

lib/protocol/http/body/deflate.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def ratio
7979
#
8080
# @returns [String] a string representation of the body.
8181
def inspect
82-
"#{super} | \#<#{self.class} #{(ratio*100).round(2)}%>"
82+
"#{super} | #<#{self.class} #{(ratio*100).round(2)}%>"
8383
end
8484
end
8585

lib/protocol/http/body/file.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,11 @@ def join
135135
#
136136
# @returns [String] a string representation of the file body.
137137
def inspect
138-
"\#<#{self.class} file=#{@file.inspect} offset=#{@offset} remaining=#{@remaining}>"
138+
if @offset > 0
139+
"#<#{self.class} #{@file.inspect} +#{@offset}, #{@remaining} bytes remaining>"
140+
else
141+
"#<#{self.class} #{@file.inspect}, #{@remaining} bytes remaining>"
142+
end
139143
end
140144
end
141145
end

lib/protocol/http/body/head.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ def ready?
5353
def length
5454
@length
5555
end
56+
57+
# Inspect the head body.
58+
#
59+
# @returns [String] a string representation of the head body.
60+
def inspect
61+
"#<#{self.class} #{@length} bytes (empty)>"
62+
end
5663
end
5764
end
5865
end

lib/protocol/http/body/rewindable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def rewindable?
8686
#
8787
# @returns [String] a string representation of the body.
8888
def inspect
89-
"\#<#{self.class} #{@index}/#{@chunks.size} chunks read>"
89+
"#{super} | #<#{self.class} #{@index}/#{@chunks.size} chunks read>"
9090
end
9191
end
9292
end

lib/protocol/http/body/stream.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,21 @@ def closed?
386386
@closed
387387
end
388388

389+
# Inspect the stream.
390+
#
391+
# @returns [String] a string representation of the stream.
392+
def inspect
393+
buffer_info = @buffer ? "#{@buffer.bytesize} bytes buffered" : "no buffer"
394+
395+
status = []
396+
status << "closed" if @closed
397+
status << "read-closed" if @closed_read
398+
399+
status_info = status.empty? ? "open" : status.join(", ")
400+
401+
return "#<#{self.class} #{buffer_info}, #{status_info}>"
402+
end
403+
389404
# @returns [Boolean] Whether there are any output chunks remaining.
390405
def empty?
391406
@output.empty?

lib/protocol/http/body/streamable.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,23 @@ def close_input(error = nil)
147147
#
148148
# @parameter error [Exception | Nil] The error that caused this stream to be closed, if any.
149149
def close_output(error = nil)
150-
@output&.close(error)
150+
if output = @output
151+
@output = nil
152+
output.close(error)
153+
end
154+
end
155+
156+
# Inspect the streaming body.
157+
#
158+
# @returns [String] a string representation of the streaming body.
159+
def inspect
160+
if @block
161+
"#<#{self.class} block available, not consumed>"
162+
elsif @output
163+
"#<#{self.class} block consumed, output active>"
164+
else
165+
"#<#{self.class} block consumed, output closed>"
166+
end
151167
end
152168
end
153169

lib/protocol/http/body/writable.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ def output
166166
# @returns [String] A string representation of the body.
167167
def inspect
168168
if @error
169-
"\#<#{self.class} #{@count} chunks written, #{status}, error=#{@error}>"
169+
"#<#{self.class} #{@count} chunks written, #{status}, error=#{@error}>"
170170
else
171-
"\#<#{self.class} #{@count} chunks written, #{status}>"
171+
"#<#{self.class} #{@count} chunks written, #{status}>"
172172
end
173173
end
174174

test/protocol/http/body/buffered.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,14 @@
148148
end
149149
end
150150

151+
with "#inspect" do
152+
let(:body) {subject.new}
153+
154+
it "generates string representation for empty body" do
155+
expect(body.inspect).to be == "#<Protocol::HTTP::Body::Buffered empty>"
156+
end
157+
end
158+
151159
with "#each" do
152160
with "a block" do
153161
it "iterates over chunks" do

0 commit comments

Comments
 (0)