File: wrapper.rb

package info (click to toggle)
ruby-protocol-http 0.55.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 840 kB
  • sloc: ruby: 6,904; makefile: 4
file content (109 lines) | stat: -rw-r--r-- 2,307 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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
				
				# Initialize the wrapper with the given body.
				#
				# @parameter body [Readable] The body to wrap.
				def initialize(body)
					@body = body
				end
				
				# @attribute [Readable] The wrapped body.
				attr :body
				
				# Close the body.
				#
				# @parameter error [Exception | Nil] The error that caused this stream to be closed, if any.
				def close(error = nil)
					@body.close(error)
					
					# It's a no-op:
					# super
				end
				
				# Forwards to the wrapped body.
				def empty?
					@body.empty?
				end
				
				# Forwards to the wrapped body.
				def ready?
					@body.ready?
				end
				
				# Forwards to the wrapped body.
				def buffered
					@body.buffered
				end
				
				# Forwards to the wrapped body.
				def rewind
					@body.rewind
				end
				
				# Forwards to the wrapped body.
				def rewindable?
					@body.rewindable?
				end
				
				# Forwards to the wrapped body.
				def length
					@body.length
				end
				
				# Forwards to the wrapped body.
				def read
					@body.read
				end
				
				# Forwards to the wrapped body.
				def discard
					@body.discard
				end
				
				# Convert the body to a hash suitable for serialization.
				#
				# @returns [Hash] The body as a hash.
				def as_json(...)
					{
						class: self.class.name,
						body: @body&.as_json
					}
				end
				
				# Convert the body to JSON.
				#
				# @returns [String] The body as JSON.
				def to_json(...)
					as_json.to_json(...)
				end
				
				# Inspect the wrapped body. The wrapper, by default, is transparent.
				#
				# @returns [String] a string representation of the wrapped body.
				def inspect
					@body.inspect
				end
			end
		end
	end
end