class Response
Constants
- CONTENT_TYPE_MAPPING
- DEFAULT_CONTENT_TYPE
- NOT_FOUND
- RESPONSE_CODE
Attributes
body[R]
header[R]
Public Class Methods
build(path)
click to toggle source
# File lib/response.rb, line 27 def self.build(path) if File.exist?(path) && !File.directory?(path) body, body_size = build_body(path) header = build_header(RESPONSE_CODE.rassoc('OK').join, content_type(path), body_size) Response.new(header, body) else body, body_size = build_body(NOT_FOUND) header = build_header(RESPONSE_CODE.rassoc('Not Found').join, content_type(path), body_size) Response.new(header, body) end end
build_body(path)
click to toggle source
# File lib/response.rb, line 55 def self.build_body(path) File.open(path, "rb") do |file| return file, file.size end end
build_header(code, type, size)
click to toggle source
# File lib/response.rb, line 48 def self.build_header(code, type, size) "HTTP/1.1 #{code}\r\n" + "Content-Type: #{type}\r\n" + "Content-Length: #{size}\r\n" + "Connection: close\r\n\r\n" end
content_type(path)
click to toggle source
# File lib/response.rb, line 43 def self.content_type(path) ext = File.extname(path).split('.').last CONTENT_TYPE_MAPPING.fetch(ext, DEFAULT_CONTENT_TYPE) end
new(header, body)
click to toggle source
# File lib/response.rb, line 6 def initialize(header, body) @header = header @body = body end
Public Instance Methods
stream()
click to toggle source
# File lib/response.rb, line 61 def stream File.read(self.body) end