Class: Syro::Response

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

Defined Under Namespace

Modules: ContentType

Constant Summary collapse

LOCATION =

:nodoc:

"Location".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#bodyObject (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

#headersObject (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

#statusObject

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

Deletes given cookie.

res.set_cookie("foo", "bar")
res["Set-Cookie"]
# => "foo=bar"

res.delete_cookie("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 delete_cookie(key, value = {})
  Rack::Utils.delete_cookie_header!(@headers, key, value)
end

#finishObject

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

Sets a cookie into the response.

res.set_cookie("foo", "bar")
res["Set-Cookie"] # => "foo=bar"

res.set_cookie("foo2", "bar2")
res["Set-Cookie"] # => "foo=bar\nfoo2=bar2"

res.set_cookie("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 set_cookie(key, value)
  Rack::Utils.set_cookie_header!(@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