class Marten::HTTP::Cookies

Overview

Represents a set of cookies.

Included Modules

Defined in:

marten/http/cookies.cr
marten/http/cookies/sub_store/base.cr
marten/http/cookies/sub_store/encrypted.cr
marten/http/cookies/sub_store/signed.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(cookies : ::HTTP::Cookies) #

[View source]
def self.new #

[View source]

Instance Method Detail

def ==(other : self) #

Returns true if the other cookies object corresponds to the current cookies.


[View source]
def [](name : String | Symbol) #

Returns the value associated with the passed cookie name.


[View source]
def []=(name, value) #

Allows to set a new cookie associated with the specified name.

The string representation of the passed value object will be used as the cookie value.


[View source]
def []?(name : String | Symbol) #

Returns the value associated with the passed cookie name or nil if the cookie is not present.


[View source]
def delete(name : String | Symbol, path : String = "/", domain : String | Nil = nil, same_site : Nil | String | Symbol = nil) : String | Nil #

Deletes a specific cookie and return its value, or nil if the cookie does not exist.

Appart from the name of the cookie to delete, this method allows to define some additional cookie properties:

  • the cookie path
  • the associated domain (useful in order to define cross-domain cookies)
  • the same_site policy (accepted values are "lax" or "strict")

The path, domain, and same_site values should always be the same that were used to create the cookie. Otherwise the cookie might not be deleted properly.


[View source]
def each(&) #
Description copied from module Enumerable({String, Array(String)})

Must yield this collection's elements to the block.


[View source]
def empty?(*args, **options) #

Returns true if there are no cookies.


[View source]
def empty?(*args, **options, &) #

Returns true if there are no cookies.


[View source]
def encrypted #

Returns the encrypted cookies store.

The returned object allows to set or fetch encrypted cookies. This means that whenever a cookie is requested from this store, the raw value of this cookie will be decrypted. This is useful to create cookies whose values can't be read nor tampered by users.

cookies.encrypted["foo"] = "bar"
cookies.encrypted["foo"] # => "bar"

[View source]
def fetch(name : String | Symbol, default = nil) #

Returns the value associated with the passed cookie name, or the passed default if the cookie is not present.


[View source]
def fetch(name : String | Symbol, &) #

Returns the value associated with the passed cookie name, or calls a block with the name when not found.


[View source]
def has_key?(name : String | Symbol) #

Returns true if the cookie with the provided name exists.


[View source]
def set(name : String | Symbol, value, expires : Time | Nil = nil, path : String = "/", domain : String | Nil = nil, secure : Bool = false, http_only : Bool = false, same_site : Nil | String | Symbol = nil) : Nil #

Allows to set a new cookie.

The string representation of the passed value object will be used as the cookie value. Appart from the cookie name and value, this method allows to define some additional cookie properties:

  • the cookie expiry datetime (expires argument)
  • the cookie path
  • the associated domain (useful in order to define cross-domain cookies)
  • whether or not the cookie should be sent for HTTPS requests only (secure argument)
  • whether or not client-side scripts should have access to the cookie (http_only argument)
  • the same_site policy (accepted values are "lax" or "strict")

[View source]
def signed #

Returns the signed cookies store.

The returned object allows to set or fetch signed cookies. This means that whenever a cookie is requested from this store, the signed representation of the corresponding value will be verified. This is useful to create cookies that can't be tampered by users.

cookies.signed["foo"] = "bar"
cookies.signed["foo"] # => "bar"

[View source]
def size(*args, **options) #

Returns the number of cookies.


[View source]
def size(*args, **options, &) #

Returns the number of cookies.


[View source]