Class: Zapp::HTTPContext::Response
- Inherits:
-
Object
- Object
- Zapp::HTTPContext::Response
- Defined in:
- lib/zapp/http_context/response.rb
Overview
Represents an HTTP response being sent back to a client
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
-
#initialize(socket:) ⇒ Response
constructor
A new instance of Response.
- #write(data:, status:, headers:) ⇒ Object
Constructor Details
#initialize(socket:) ⇒ Response
Returns a new instance of Response.
9 10 11 |
# File 'lib/zapp/http_context/response.rb', line 9 def initialize(socket:) @socket = socket end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
7 8 9 |
# File 'lib/zapp/http_context/response.rb', line 7 def data @data end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
7 8 9 |
# File 'lib/zapp/http_context/response.rb', line 7 def headers @headers end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
7 8 9 |
# File 'lib/zapp/http_context/response.rb', line 7 def status @status end |
Instance Method Details
#write(data:, status:, headers:) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/zapp/http_context/response.rb', line 13 def write(data:, status:, headers:) @status = status @data = data @headers = headers response = +"HTTP/1.1 #{status}\n" response << "Content-Length: #{data.size}\n" unless headers["Content-Length"] headers.each do |k, v| response << "#{k}: #{v}\n" end response << "\n#{data}\n" @socket.write(response) end |