Skip to content

Commit a2edc32

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

File tree

12 files changed

+58
-9
lines changed

12 files changed

+58
-9
lines changed

lib/protocol/http/body/buffered.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ def rewind
154154
# @returns [String] a string representation of the buffered body.
155155
def inspect
156156
if @chunks
157-
"\#<#{self.class} #{@chunks.size} chunks, #{self.length} bytes>"
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,19 @@ def close_input(error = nil)
149149
def close_output(error = nil)
150150
@output&.close(error)
151151
end
152+
153+
# Inspect the streaming body.
154+
#
155+
# @returns [String] a string representation of the streaming body.
156+
def inspect
157+
if @block
158+
"#<#{self.class} block available, not consumed>"
159+
elsif @output
160+
"#<#{self.class} block consumed, output active>"
161+
else
162+
"#<#{self.class} block consumed, output closed>"
163+
end
164+
end
152165
end
153166

154167
# A response body is used on the server side to generate the response body using a block.

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/deflate.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858

5959
with "#inspect" do
6060
it "can generate string representation" do
61-
expect(compressed_body.inspect).to be == "#<Protocol::HTTP::Body::Buffered 0 chunks, 0 bytes> | #<Protocol::HTTP::Body::Deflate 100.0%>"
61+
expect(compressed_body.inspect).to be == "#<Protocol::HTTP::Body::Buffered 0/0 chunks, 0 bytes> | #<Protocol::HTTP::Body::Deflate 100.0%>"
6262
end
6363
end
6464
end

0 commit comments

Comments
 (0)