Sha256: df4b02f012ae57c98567101cfbc28bb4ffe0dba3929dac09df6eb1ac6c72837b

Contents?: true

Size: 1.13 KB

Versions: 3

Compression:

Stored size: 1.13 KB

Contents

##
# Class for midori response
# @attr [String] status HTTP response status
# @attr [Hash] header HTTP response header
# @attr [String] body HTTP response body
class Midori::Response
  attr_accessor :status, :header, :body

  # @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
  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

  # Generate header string from hash
  # @return [String] generated string
  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

  # Convert response to raw string
  # @return [String] generated string
  def to_s
    "HTTP/1.1 #{@status}\r\n#{generate_header}\r\n#{@body}"
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
midori.rb-0.9.0 lib/midori/response.rb
midori.rb-0.8.0 lib/midori/response.rb
midori.rb-0.7.1 lib/midori/response.rb