Class: Syro::Response
- Inherits:
-
Object
- Object
- Syro::Response
- Defined in:
- lib/syro.rb
Defined Under Namespace
Modules: ContentType
Constant Summary collapse
- LOCATION =
:nodoc:
"Location".freeze
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the body of the response.
-
#headers ⇒ Object
readonly
Returns a hash with the response headers.
-
#status ⇒ Object
The status of the response.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Returns the response header corresponding to ‘key`.
-
#[]=(key, value) ⇒ Object
Sets the given ‘value` with the header corresponding to `key`.
-
#delete_cookie(key, value = {}) ⇒ Object
Deletes given cookie.
-
#finish ⇒ Object
Returns an array with three elements: the status, headers and body.
-
#html(str) ⇒ Object
Write response body as text/html.
-
#initialize(headers = {}) ⇒ Response
constructor
A new instance of Response.
-
#json(str) ⇒ Object
Write response body as application/json.
-
#redirect(path, status = 302) ⇒ Object
Sets the ‘Location` header to `path` and updates the status to `status`.
-
#set_cookie(key, value) ⇒ Object
Sets a cookie into the response.
-
#text(str) ⇒ Object
Write response body as text/plain.
-
#write(str) ⇒ Object
Appends ‘str` to `body` and updates the `Content-Length` header.
Constructor Details
#initialize(headers = {}) ⇒ Response
Returns a new instance of Response.
65 66 67 68 69 70 |
# File 'lib/syro.rb', line 65 def initialize(headers = {}) @status = 404 @headers = headers @body = [] @length = 0 end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the body of the response.
res.body
# => []
res.write("there is")
res.write("no try")
res.body
# => ["there is", "no try"]
56 57 58 |
# File 'lib/syro.rb', line 56 def body @body end |
#headers ⇒ Object (readonly)
Returns a hash with the response headers.
res.headers
# => { "Content-Type" => "text/html", "Content-Length" => "42" }
63 64 65 |
# File 'lib/syro.rb', line 63 def headers @headers end |
#status ⇒ Object
The status of the response.
res.status = 200
res.status # => 200
43 44 45 |
# File 'lib/syro.rb', line 43 def status @status end |
Instance Method Details
#[](key) ⇒ Object
Returns the response header corresponding to ‘key`.
res["Content-Type"] # => "text/html"
res["Content-Length"] # => "42"
77 78 79 |
# File 'lib/syro.rb', line 77 def [](key) @headers[key] end |
#[]=(key, value) ⇒ Object
Sets the given ‘value` with the header corresponding to `key`.
res["Content-Type"] = "application/json"
res["Content-Type"] # => "application/json"
86 87 88 |
# File 'lib/syro.rb', line 86 def []=(key, value) @headers[key] = value end |
#delete_cookie(key, value = {}) ⇒ Object
Deletes given cookie.
res.("foo", "bar")
res["Set-Cookie"]
# => "foo=bar"
res.("foo")
res["Set-Cookie"]
# => "foo=; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 -0000"
206 207 208 |
# File 'lib/syro.rb', line 206 def (key, value = {}) Rack::Utils.(@headers, key, value) end |
#finish ⇒ Object
Returns an array with three elements: the status, headers and body. If the status is not set, the status is set to 404. If a match is found for both path and request method, the status is changed to 200.
res.status = 200
res.finish
# => [200, {}, []]
res.status = nil
res.finish
# => [404, {}, []]
res.status = nil
res.write("syro")
res.finish
# => [200, { "Content-Type" => "text/html" }, ["syro"]]
165 166 167 |
# File 'lib/syro.rb', line 165 def finish [@status, @headers, @body] end |
#html(str) ⇒ Object
Write response body as text/html
118 119 120 121 |
# File 'lib/syro.rb', line 118 def html(str) @headers[Rack::CONTENT_TYPE] = ContentType::HTML write(str) end |
#json(str) ⇒ Object
Write response body as application/json
124 125 126 127 |
# File 'lib/syro.rb', line 124 def json(str) @headers[Rack::CONTENT_TYPE] = ContentType::JSON write(str) end |
#redirect(path, status = 302) ⇒ Object
Sets the ‘Location` header to `path` and updates the status to `status`. By default, `status` is `302`.
res.redirect("/path")
res["Location"] # => "/path"
res.status # => 302
res.redirect("http://syro.ru", 303)
res["Location"] # => "http://syro.ru"
res.status # => 303
142 143 144 145 |
# File 'lib/syro.rb', line 142 def redirect(path, status = 302) @headers[LOCATION] = path @status = status end |
#set_cookie(key, value) ⇒ Object
Sets a cookie into the response.
res.("foo", "bar")
res["Set-Cookie"] # => "foo=bar"
res.("foo2", "bar2")
res["Set-Cookie"] # => "foo=bar\nfoo2=bar2"
res.("bar", {
domain: ".example.com",
path: "/",
# max_age: 0,
# expires: Time.now + 10_000,
secure: true,
httponly: true,
value: "bar"
})
res["Set-Cookie"].split("\n").last
# => "bar=bar; domain=.example.com; path=/; secure; HttpOnly
NOTE: This method doesn’t sign and/or encrypt the value of the cookie.
192 193 194 |
# File 'lib/syro.rb', line 192 def (key, value) Rack::Utils.(@headers, key, value) end |
#text(str) ⇒ Object
Write response body as text/plain
112 113 114 115 |
# File 'lib/syro.rb', line 112 def text(str) @headers[Rack::CONTENT_TYPE] = ContentType::TEXT write(str) end |
#write(str) ⇒ Object
Appends ‘str` to `body` and updates the `Content-Length` header.
res.body # => []
res.write("foo")
res.write("bar")
res.body
# => ["foo", "bar"]
res["Content-Length"]
# => 6
103 104 105 106 107 108 109 |
# File 'lib/syro.rb', line 103 def write(str) s = str.to_s @length += s.bytesize @headers[Rack::CONTENT_LENGTH] = @length.to_s @body << s end |