Sha256: 6319e27fc2e795d6a5692b651df837dea0c988b9aa31bfe50ea708b7cdfce512

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

class Tynn
  # It provides convenience methods to construct a Rack response.
  #
  #   res = Tynn::Response.new
  #
  #   res.status = 200
  #   res["Content-Type"] = "text/html"
  #   res.write("foo")
  #
  #   res.finish
  #   # => [200, { "Content-Type" => "text/html", "Content-Length" => 3 }, ["foo"]]
  #
  class Response < Syro::Response
    # 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", "bar", {
    #     domain: ".example.com",
    #     path: "/",
    #     # max_age: 0,
    #     # expires: Time.now + 10_000,
    #     secure: true,
    #     httponly: true,
    #     same_site: :Lax
    #   })
    #
    #   res["Set-Cookie"].split("\n").last
    #   # => "bar=bar; domain=.example.com; path=/; secure; HttpOnly; SameSite=Lax
    #
    # *NOTE.* This method doesn't sign and/or encrypt the value of the cookie.
    #
    def set_cookie(key, value, options = {})
      Rack::Utils.set_cookie_header!(headers, key, options.merge(value: value))
    end

    # Deletes cookie by <tt>key</tt>.
    #
    #   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"
    #
    def delete_cookie(key, options = {})
      Rack::Utils.delete_cookie_header!(headers, options)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tynn-2.0.0.alpha lib/tynn/response.rb