Sha256: 1a9f1d81f5984d60d4b5d37c57b7429e7350209af85187da5f89fc722c649ecc

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 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 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.3 lib/protocol/http/body/wrapper.rb