Class: Minver::Response

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

Constant Summary collapse

DEFAULT_HEADERS =
{
  "Content-Type" => "text/html; charset=utf-8",
  "Server" => "Minver/1.0",
  "Connection" => "close"
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status, headers, body) ⇒ Response

Returns a new instance of Response.



8
9
10
11
12
13
14
15
# File 'lib/minver/response.rb', line 8

def initialize status, headers, body
  headers["Content-Length"] = body.length
  @status = status
  @headers = DEFAULT_HEADERS.merge(
    "Date" => Time.now.strftime("%a, %d %b %Y %H:%M:%S %Z")
  ).merge(headers)
  @body = body
end

Class Method Details

.from(var) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/minver/response.rb', line 35

def self.from var
  case var
  when String
    new(200, {}, var)
  when Array
    new(*var)
  end
end

Instance Method Details

#bodyObject



17
18
19
# File 'lib/minver/response.rb', line 17

def body
  @body
end

#dataObject



21
22
23
# File 'lib/minver/response.rb', line 21

def data
  [status_line, *header_lines, '', body].join("\n")
end

#header_linesObject



29
30
31
32
33
# File 'lib/minver/response.rb', line 29

def header_lines
  @headers.map do |k, v|
    [k, v].join(": ")
  end
end

#status_lineObject



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

def status_line
  ["HTTP/#{Minver::Base::HTTP_VERSION}", @status, Minver::Base::HTTP_CODES[@status]].join(' ')
end