Class: Protocol::HTTP::Response

Inherits:
Object
  • Object
show all
Includes:
Body::Reader
Defined in:
lib/protocol/http/response.rb

Overview

Represents an HTTP response which can be used both server and client-side.

~~~ ruby require ‘protocol/http’

# Long form: Protocol::HTTP::Response.new(“http/1.1”, 200, Protocol::HTTP::Headers[[“content-type”, “text/html”]], Protocol::HTTP::Body::Buffered.wrap(“Hello, World!”))

# Short form: Protocol::HTTP::Response[200, => “text/html”, [“Hello, World!”]] ~~~

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Body::Reader

#body?, #buffered!, #close, #discard, #each, #finish, #read, #save

Constructor Details

#initialize(version = nil, status = 200, headers = Headers.new, body = nil, protocol = nil) ⇒ Response

Create a new response.



32
33
34
35
36
37
38
# File 'lib/protocol/http/response.rb', line 32

def initialize(version = nil, status = 200, headers = Headers.new, body = nil, protocol = nil)
	@version = version
	@status = status
	@headers = headers
	@body = body
	@protocol = protocol
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



50
51
52
# File 'lib/protocol/http/response.rb', line 50

def body
  @body
end

#headersObject

Returns the value of attribute headers.



47
48
49
# File 'lib/protocol/http/response.rb', line 47

def headers
  @headers
end

#protocolObject

Returns the value of attribute protocol.



53
54
55
# File 'lib/protocol/http/response.rb', line 53

def protocol
  @protocol
end

#statusObject

Returns the value of attribute status.



44
45
46
# File 'lib/protocol/http/response.rb', line 44

def status
  @status
end

#The HTTP status code, e.g. `200`, `404`, etc.(HTTPstatuscode, e.g.`200`, `404`, etc.) ⇒ Object (readonly)



44
# File 'lib/protocol/http/response.rb', line 44

attr_accessor :status

#The HTTP version, usually one of `"HTTP/1.1"`, `"HTTP/2"`, etc.(HTTPversion, usuallyoneof`"HTTP/1.1"`, `"HTTP/2"`, etc.) ⇒ Object (readonly)



41
# File 'lib/protocol/http/response.rb', line 41

attr_accessor :version

#The protocol, e.g. `"websocket"`, etc.(protocol, e.g.`"websocket"`, etc.) ⇒ Object (readonly)



53
# File 'lib/protocol/http/response.rb', line 53

attr_accessor :protocol

#versionObject

Returns the value of attribute version.



41
42
43
# File 'lib/protocol/http/response.rb', line 41

def version
  @version
end

Class Method Details

.[](status, _headers = nil, _body = nil, headers: _headers, body: _body, protocol: nil) ⇒ Object

A short-cut method which exposes the main response variables that you’d typically care about. It follows the same order as the ‘Rack` response tuple, but also includes the protocol.

~~~ ruby Response[200, => “text/html”, [“Hello, World!”]] ~~~



140
141
142
143
144
145
# File 'lib/protocol/http/response.rb', line 140

def self.[](status, _headers = nil, _body = nil, headers: _headers, body: _body, protocol: nil)
	body = Body::Buffered.wrap(body)
	headers = Headers[headers]
	
	self.new(nil, status, headers, body, protocol)
end

.for_exception(exception) ⇒ Object

Create a response for the given exception.



150
151
152
# File 'lib/protocol/http/response.rb', line 150

def self.for_exception(exception)
	Response[500, Headers["content-type" => "text/plain"], ["#{exception.class}: #{exception.message}"]]
end

Instance Method Details

#as_jsonObject

Convert the response to a hash suitable for serialization.



157
158
159
160
161
162
163
164
165
# File 'lib/protocol/http/response.rb', line 157

def as_json(...)
	{
		version: @version,
		status: @status,
		headers: @headers&.as_json,
		body: @body&.as_json,
		protocol: @protocol
	}
end

#bad_request?Boolean

Whether the status is 400 (bad request).

Returns:

  • (Boolean)


119
120
121
# File 'lib/protocol/http/response.rb', line 119

def bad_request?
	@status == 400
end

#continue?Boolean

Whether the status is 100 (continue).

Returns:

  • (Boolean)


68
69
70
# File 'lib/protocol/http/response.rb', line 68

def continue?
	@status == 100
end

#failure?Boolean

Whether the status is considered a failure.

Returns:

  • (Boolean)


114
115
116
# File 'lib/protocol/http/response.rb', line 114

def failure?
	@status and @status >= 400 && @status < 600
end

#final?Boolean

Whether the status is considered final. Note that 101 is considered final.

Returns:

  • (Boolean)


78
79
80
81
# File 'lib/protocol/http/response.rb', line 78

def final?
	# 101 is effectively a final status.
	@status and @status >= 200 || @status == 101
end

#hijack?Boolean

Whether the response is considered a hijack: the connection has been taken over by the application and the server should not send any more data.

Returns:

  • (Boolean)


63
64
65
# File 'lib/protocol/http/response.rb', line 63

def hijack?
	false
end

#informational?Boolean

Whether the status is considered informational.

Returns:

  • (Boolean)


73
74
75
# File 'lib/protocol/http/response.rb', line 73

def informational?
	@status and @status >= 100 && @status < 200
end

#internal_server_error?Boolean Also known as: server_failure?

Whether the status is 500 (internal server error).

Returns:

  • (Boolean)


124
125
126
# File 'lib/protocol/http/response.rb', line 124

def internal_server_error?
	@status == 500
end

#not_modified?Boolean

Whether the status is 304 (not modified).

Returns:

  • (Boolean)


104
105
106
# File 'lib/protocol/http/response.rb', line 104

def not_modified?
	@status == 304
end

#ok?Boolean

Whether the status is 200 (ok).

Returns:

  • (Boolean)


84
85
86
# File 'lib/protocol/http/response.rb', line 84

def ok?
	@status == 200
end

#partial?Boolean

Whether the status is 206 (partial content).

Returns:

  • (Boolean)


94
95
96
# File 'lib/protocol/http/response.rb', line 94

def partial?
	@status == 206
end

#peerObject

A response that is generated by a client, may choose to include the peer (address) associated with the response. It should be implemented by a sub-class.



58
59
60
# File 'lib/protocol/http/response.rb', line 58

def peer
	nil
end

#preserve_method?Boolean

Whether the status is 307 (temporary redirect) and should preserve the method of the request when following the redirect.

Returns:

  • (Boolean)


109
110
111
# File 'lib/protocol/http/response.rb', line 109

def preserve_method?
	@status == 307 || @status == 308
end

#redirection?Boolean

Whether the status is considered a redirection.

Returns:

  • (Boolean)


99
100
101
# File 'lib/protocol/http/response.rb', line 99

def redirection?
	@status and @status >= 300 && @status < 400
end

#success?Boolean

Whether the status is considered successful.

Returns:

  • (Boolean)


89
90
91
# File 'lib/protocol/http/response.rb', line 89

def success?
	@status and @status >= 200 && @status < 300
end

#The body, e.g. `"Hello, World!"`, etc.=(body, e.g.`"Hello, World!"`, etc. = (value)) ⇒ Object



50
# File 'lib/protocol/http/response.rb', line 50

attr_accessor :body

"text/html"}`, etc.=-instance_method"> #The headers, e.g. `{"content-type" => "text/html"}`, etc.=(headers, e.g.`{"content-type" = > "text/html"}`, etc. = (value)) ⇒ Object



47
# File 'lib/protocol/http/response.rb', line 47

attr_accessor :headers

#to_aryObject

Implicit conversion to an array.



184
185
186
# File 'lib/protocol/http/response.rb', line 184

def to_ary
	return @status, @headers, @body
end

#to_jsonObject

Convert the response to JSON.



170
171
172
# File 'lib/protocol/http/response.rb', line 170

def to_json(...)
	as_json.to_json(...)
end

#to_sObject

Summarise the response as a string.



177
178
179
# File 'lib/protocol/http/response.rb', line 177

def to_s
	"#{@status} #{@version}"
end