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.



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

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.



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

def body
  @body
end

#headersObject

Returns the value of attribute headers.



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

def headers
  @headers
end

#protocolObject

Returns the value of attribute protocol.



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

def protocol
  @protocol
end

#statusObject

Returns the value of attribute status.



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

def status
  @status
end

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



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

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)



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

attr_accessor :version

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



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

attr_accessor :protocol

#versionObject

Returns the value of attribute version.



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

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!”]] ~~~



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

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.



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

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.



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

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)


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

def bad_request?
  @status == 400
end

#continue?Boolean

Whether the status is 100 (continue).

Returns:

  • (Boolean)


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

def continue?
  @status == 100
end

#failure?Boolean

Whether the status is considered a failure.

Returns:

  • (Boolean)


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

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)


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

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)


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

def hijack?
  false
end

#informational?Boolean

Whether the status is considered informational.

Returns:

  • (Boolean)


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

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)


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

def internal_server_error?
  @status == 500
end

#not_modified?Boolean

Whether the status is 304 (not modified).

Returns:

  • (Boolean)


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

def not_modified?
  @status == 304
end

#ok?Boolean

Whether the status is 200 (ok).

Returns:

  • (Boolean)


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

def ok?
  @status == 200
end

#partial?Boolean

Whether the status is 206 (partial content).

Returns:

  • (Boolean)


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

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.



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

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)


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

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

#redirection?Boolean

Whether the status is considered a redirection.

Returns:

  • (Boolean)


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

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

#success?Boolean

Whether the status is considered successful.

Returns:

  • (Boolean)


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

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

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



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

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



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

attr_accessor :headers

#to_aryObject

Implicit conversion to an array.



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

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

#to_jsonObject

Convert the response to JSON.



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

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

#to_sObject

Summarise the response as a string.



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

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