Sha256: 12c847cd2b0320d7119513ecbe6c76cd87a4c1339e860e1cf3e4fd4ba9a368cc

Contents?: true

Size: 1.33 KB

Versions: 3

Compression:

Stored size: 1.33 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 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 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

3 entries across 3 versions & 1 rubygems

Version Path
protocol-http-0.35.0 lib/protocol/http/body/wrapper.rb
protocol-http-0.34.0 lib/protocol/http/body/wrapper.rb
protocol-http-0.33.0 lib/protocol/http/body/wrapper.rb