Class: Syro::Response

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

Constant Summary collapse

LOCATION =

:nodoc:

"Location".freeze
DEFAULT =

:nodoc:

"text/html".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers = {}) ⇒ Response

Returns a new instance of Response.



60
61
62
63
64
65
# File 'lib/syro.rb', line 60

def initialize(headers = {})
  @status  = nil
  @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"]


51
52
53
# File 'lib/syro.rb', line 51

def body
  @body
end

#headersObject (readonly)

Returns a hash with the response headers.

res.headers
# => { "Content-Type" => "text/html", "Content-Length" => "42" }


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

def headers
  @headers
end

#statusObject

The status of the response.

res.status = 200
res.status # => 200


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

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"


72
73
74
# File 'lib/syro.rb', line 72

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"


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

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

Deletes cookie.

res.set_cookie("foo", "bar")
res["Set-Cookie"]
# => "foo=; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 -0000"


188
189
190
# File 'lib/syro.rb', line 188

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 empty body, otherwise the status is set to 200 and updates the ‘Content-Type` header to `text/html`.

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"]]


142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/syro.rb', line 142

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

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


119
120
121
122
# File 'lib/syro.rb', line 119

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.



178
179
180
# File 'lib/syro.rb', line 178

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


98
99
100
101
102
103
104
# File 'lib/syro.rb', line 98

def write(str)
  s = str.to_s

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