Class: Rosendo::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/rosendo/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Response

Returns a new instance of Response.



4
5
6
7
# File 'lib/rosendo/response.rb', line 4

def initialize(io)
  @io = io
  @headers = {}
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



3
4
5
# File 'lib/rosendo/response.rb', line 3

def body
  @body
end

#headersObject

Returns the value of attribute headers.



3
4
5
# File 'lib/rosendo/response.rb', line 3

def headers
  @headers
end

#statusObject

Returns the value of attribute status.



3
4
5
# File 'lib/rosendo/response.rb', line 3

def status
  @status
end

Instance Method Details

#header_linesObject



29
30
31
# File 'lib/rosendo/response.rb', line 29

def header_lines
  {'Content-Length' => body.size}.merge(@headers).map { |k, v| "#{k}: #{v}" }.join("\n")
end

#respondObject



17
18
19
20
21
22
23
# File 'lib/rosendo/response.rb', line 17

def respond
  @io.puts status_line
  @io.puts header_lines
  @io.puts
  @io.write body # write instead of puts for no extra newline
  @io.close
end

#status_lineObject



25
26
27
# File 'lib/rosendo/response.rb', line 25

def status_line
  "HTTP/1.1 #{status}"
end