Sha256: b1917d01e9e896814c081c9c60837ebb4b41ce1dcb5bdbe430b54d34a37503b1

Contents?: true

Size: 1.07 KB

Versions: 6

Compression:

Stored size: 1.07 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? && !@body.empty?
    @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

6 entries across 6 versions & 1 rubygems

Version Path
midori.rb-0.7.0 lib/midori/response.rb
midori.rb-0.6.0 lib/midori/response.rb
midori.rb-0.5.4 lib/midori/response.rb
midori.rb-0.5.3.1 lib/midori/response.rb
midori.rb-0.5.3 lib/midori/response.rb
midori.rb-0.5.2 lib/midori/response.rb