Class: Midori::Response

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

Overview

Class for midori response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code = 200, header = Midori::Const::DEFAULT_HEADER.clone, body = '') ⇒ Response

Returns a new instance of Response.

Parameters:

  • code (Fixnum) (defaults to: 200)

    HTTP response code

  • header (Hash) (defaults to: Midori::Const::DEFAULT_HEADER.clone)

    HTTP response header

  • body (String) (defaults to: '')

    HTTP response body


12
13
14
15
16
# File 'lib/em-midori/response.rb', line 12

def initialize(code = 200, header = Midori::Const::DEFAULT_HEADER.clone, body = '')
  @status = Midori::Const::STATUS_CODE[code]
  @header = header
  @body = body
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body


7
8
9
# File 'lib/em-midori/response.rb', line 7

def body
  @body
end

#headerObject

Returns the value of attribute header


7
8
9
# File 'lib/em-midori/response.rb', line 7

def header
  @header
end

#HTTPString

response status

Returns:

  • (String)

    the current value of HTTP


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

def HTTP
  @HTTP
end

#statusObject

Returns the value of attribute status


7
8
9
# File 'lib/em-midori/response.rb', line 7

def status
  @status
end

Instance Method Details

#generate_headerString

Generate header string from hash

Returns:

  • (String)

    generated string


20
21
22
23
24
# File 'lib/em-midori/response.rb', line 20

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


28
29
30
# File 'lib/em-midori/response.rb', line 28

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