Sha256: 3c23d5973bbec0f172672a3875df8dad5a6557b92d1469848fb649e400fe04b5

Contents?: true

Size: 1.43 KB

Versions: 8

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2019-2024, by Samuel Williams.

require_relative "readable"

module Protocol
	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)
					end
				end
				
				def initialize(body)
					@body = body
				end
				
				# The wrapped body.
				attr :body
				
				def close(error = nil)
					@body.close(error)
					
					# It's a no-op:
					# super
				end
				
				def empty?
					@body.empty?
				end
				
				def ready?
					@body.ready?
				end
				
				def buffered
					@body.buffered
				end
				
				def rewind
					@body.rewind
				end
				
				def rewindable?
					@body.rewindable?
				end
				
				def length
					@body.length
				end
				
				# Read the next available chunk.
				def read
					@body.read
				end
				
				def discard
					@body.discard
				end
				
				def as_json(...)
					{
						class: self.class.name,
						body: @body&.as_json
					}
				end
				
				def to_json(...)
					as_json.to_json(...)
				end
				
				def inspect
					@body.inspect
				end
			end
		end
	end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
protocol-http-0.45.0 lib/protocol/http/body/wrapper.rb
protocol-http-0.44.0 lib/protocol/http/body/wrapper.rb
protocol-http-0.43.0 lib/protocol/http/body/wrapper.rb
protocol-http-0.42.0 lib/protocol/http/body/wrapper.rb
protocol-http-0.41.0 lib/protocol/http/body/wrapper.rb
protocol-http-0.40.0 lib/protocol/http/body/wrapper.rb
protocol-http-0.39.0 lib/protocol/http/body/wrapper.rb
protocol-http-0.38.0 lib/protocol/http/body/wrapper.rb