class Midori::Response

Class for midori response @attr [String] status HTTP response status @attr [Hash] header HTTP response header @attr [String] body HTTP response body

Attributes

body[RW]
header[RW]
status[RW]

Public Class Methods

new(options = {}) click to toggle source

@param [Hash] options HTTP response @option options [Integer] code HTTP response code @option options [Hash] header HTTP response header @option options [String] body HTTP response body Init a Response

# File lib/midori/response.rb, line 14
def initialize(options = {})
  code = options[:status] || 200
  @status = Midori::Const::STATUS_CODE[code]
  @header = options[:header] || Midori::Const::DEFAULT_HEADER.clone
  @body = options[:body] || ''
end

Public Instance Methods

generate_header() click to toggle source

Generate header string from hash @return [String] generated string

# File lib/midori/response.rb, line 23
def generate_header
  @header['Content-Length'] = @body.bytesize if @header['Content-Length'].nil? && @header['Upgrade'].nil? && @header['Content-Type'] != 'text/event-stream'
  @header.map do |key, value|
    "#{key}: #{value}\r\n"
  end.join
end
to_s() click to toggle source

Convert response to raw string @return [String] generated string

# File lib/midori/response.rb, line 32
def to_s
  "HTTP/1.1 #{@status}\r\n#{generate_header}\r\n#{@body}"
end