Class: Syro::Response

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

Constant Summary collapse

LOCATION =
"Location".freeze
DEFAULT =
"text/html".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers = {}) ⇒ Response

Returns a new instance of Response.



36
37
38
39
40
41
# File 'lib/syro.rb', line 36

def initialize(headers = {})
  @status  = nil
  @headers = headers
  @body    = []
  @length  = 0
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



33
34
35
# File 'lib/syro.rb', line 33

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



34
35
36
# File 'lib/syro.rb', line 34

def headers
  @headers
end

#statusObject

Returns the value of attribute status.



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

def status
  @status
end

Instance Method Details

#[](key) ⇒ Object



43
44
45
# File 'lib/syro.rb', line 43

def [](key)
  @headers[key]
end

#[]=(key, value) ⇒ Object



47
48
49
# File 'lib/syro.rb', line 47

def []=(key, value)
  @headers[key] = value
end


81
82
83
# File 'lib/syro.rb', line 81

def delete_cookie(key, value = {})
  Rack::Utils.delete_cookie_header!(@headers, key, value)
end

#finishObject



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/syro.rb', line 64

def finish
  if @status.nil?
    if @body.empty?
      @status = 404
    else
      @headers[Rack::CONTENT_TYPE] ||= DEFAULT
      @status = 200
    end
  end

  [@status, @headers, @body]
end

#redirect(path, status = 302) ⇒ Object



59
60
61
62
# File 'lib/syro.rb', line 59

def redirect(path, status = 302)
  @headers[LOCATION] = path
  @status = status
end


77
78
79
# File 'lib/syro.rb', line 77

def set_cookie(key, value)
  Rack::Utils.set_cookie_header!(@headers, key, value)
end

#write(str) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/syro.rb', line 51

def write(str)
  s = str.to_s

  @length += s.bytesize
  @headers[Rack::CONTENT_LENGTH] = @length.to_s
  @body << s
end