Class: Midori::Response

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

Overview

Class for midori response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Response

Init a Response

Parameters:

  • options (Hash) (defaults to: {})

    HTTP response

Options Hash (options):

  • code (Integer)

    HTTP response code

  • header (Hash)

    HTTP response header

  • body (String)

    HTTP response body



14
15
16
17
18
19
# 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

Instance Attribute Details

#bodyString

HTTP response body

Returns:

  • (String)

    the current value of body



6
7
8
# File 'lib/midori/response.rb', line 6

def body
  @body
end

#headerHash

HTTP response header

Returns:

  • (Hash)

    the current value of header



6
7
8
# File 'lib/midori/response.rb', line 6

def header
  @header
end

#statusString

HTTP response status

Returns:

  • (String)

    the current value of status



6
7
8
# File 'lib/midori/response.rb', line 6

def status
  @status
end

Instance Method Details

#generate_headerString

Generate header string from hash

Returns:

  • (String)

    generated string



23
24
25
26
27
# File 'lib/midori/response.rb', line 23

def generate_header
  @header.map do |key, value|
    "#{key}: #{value}\r\n"
  end.join
end

#to_sString

Convert response to raw string

Returns:

  • (String)

    generated string



31
32
33
# File 'lib/midori/response.rb', line 31

def to_s
  "HTTP/1.1 #{@status}\r\n#{generate_header}\r\n#{@body}"
end