Class: ResponsePreparer

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

Constant Summary collapse

SERVER_ROOT =
"example/tmp/web-server"

Instance Method Summary collapse

Instance Method Details

#prepare_response(request) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/http.rb', line 4

def prepare_response(request)
  if request.fetch(:path) == "/"
    respond_with(SERVER_ROOT + "index.html")
  else
    respond_with(SERVER_ROOT + request.fetch(:path))
  end
end

#respond_with(path) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/http.rb', line 12

def respond_with(path)
 if File.exists?(path)
   send_ok_response(File.binread(path))
 else
   send_file_not_found
 end
end

#send_file_not_foundObject



24
25
26
# File 'lib/http.rb', line 24

def send_file_not_found
  Response.new(code: 404)
end

#send_ok_response(data) ⇒ Object



20
21
22
# File 'lib/http.rb', line 20

def send_ok_response(data)
  Response.new(code: 200, data: data)
end