Sha256: 79dc0eb3be1e3a2779e9ffd53d055a40c8e8675851d9fdd5d6018ae7582e5b79

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

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

require_relative 'readable'

module Protocol
	module HTTP
		module Body
			# Wrapping body instance. Typically you'd override `#read`.
			class Wrapper < Readable
				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
				
				# Buffer any remaining body.
				def finish
					@body.finish
				end
				
				def close(error = nil)
					@body.close(error)
					
					super
				end
				
				def empty?
					@body.empty?
				end
				
				def ready?
					@body.ready?
				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
				
				def stream?
					@body.stream?
				end
				
				def call(stream)
					@body.call(stream)
				end
			end
		end
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
protocol-http-0.26.4 lib/protocol/http/body/wrapper.rb