Class: Syro::Response
- Inherits:
-
Object
- Object
- Syro::Response
- Defined in:
- lib/syro.rb
Constant Summary collapse
- LOCATION =
"Location".freeze
- DEFAULT =
"text/html".freeze
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#status ⇒ Object
Returns the value of attribute status.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #delete_cookie(key, value = {}) ⇒ Object
- #finish ⇒ Object
-
#initialize(headers = {}) ⇒ Response
constructor
A new instance of Response.
- #redirect(path, status = 302) ⇒ Object
- #set_cookie(key, value) ⇒ Object
- #write(str) ⇒ Object
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
#body ⇒ Object (readonly)
Returns the value of attribute body.
33 34 35 |
# File 'lib/syro.rb', line 33 def body @body end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
34 35 36 |
# File 'lib/syro.rb', line 34 def headers @headers end |
#status ⇒ Object
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 |
#delete_cookie(key, value = {}) ⇒ Object
81 82 83 |
# File 'lib/syro.rb', line 81 def (key, value = {}) Rack::Utils.(@headers, key, value) end |
#finish ⇒ Object
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 |
#set_cookie(key, value) ⇒ Object
77 78 79 |
# File 'lib/syro.rb', line 77 def (key, value) Rack::Utils.(@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 |