Class: Shamu::Rack::Cookies

Inherits:
Object
  • Object
show all
Defined in:
lib/shamu/rack/cookies.rb

Overview

Expose the request cookies as a hash.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Cookies

Returns a new instance of Cookies.

Parameters:

  • env (Hash)

    the Rack environment



13
14
15
16
17
# File 'lib/shamu/rack/cookies.rb', line 13

def initialize( env )
  @env = env
  @cookies = {}
  @deleted_cookies = []
end

Class Method Details

.createCookies

Returns:



8
9
10
# File 'lib/shamu/rack/cookies.rb', line 8

def self.create( * )
  fail "Add Shamu::Rack::CookiesMiddleware to use Shamu::Rack::Cookies"
end

Instance Method Details

#apply!(headers) ⇒ Hash

Apply the cookies #set or deleted to the actual rack response headers.

Modifies the headers hash!

Parameters:

  • headers (Hash)

    from rack response

Returns:

  • (Hash)

    the modified headers with cookie values.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/shamu/rack/cookies.rb', line 26

def apply!( headers )
  cookies.each do |key, value|
    ::Rack::Utils.set_cookie_header! headers, key, value
  end

  deleted_cookies.each do |key|
    ::Rack::Utils.delete_cookie_header! headers, key
  end

  headers
end

#delete(key) ⇒ self

Delete a cookie from the browser.

Parameters:

  • key (String)

    or name of the cookie.

Returns:

  • (self)


89
90
91
92
93
# File 'lib/shamu/rack/cookies.rb', line 89

def delete( key )
  cookies.delete( key )
  @deleted_cookies << key if env_cookies.key?( key )
  self
end

#get(key) ⇒ String Also known as: []

Get a cookie value from the browser.

Parameters:

  • key (String)

    or name of the cookie

Returns:

  • (String)

    cookie value



41
42
43
44
45
46
47
48
49
# File 'lib/shamu/rack/cookies.rb', line 41

def get( key )
  key = key.to_s

  if cookie = cookies[ key ]
    cookie[:value]
  else
    env_cookies[ key ]
  end
end

#key?(name) ⇒ Boolean

Returns true if the cookie has been set.

Parameters:

  • name (String)

Returns:

  • (Boolean)

    true if the cookie has been set.



54
55
56
# File 'lib/shamu/rack/cookies.rb', line 54

def key?( name )
  cookies.key?( name ) || env_cookies.key?( name )
end

#set(key, value) ⇒ self #set(key, hash) ⇒ self Also known as: []=

Set or update a cookie in the headers.

Overloads:

  • #set(key, value) ⇒ self

    Parameters:

    • key (String)

      or name of the cookie

    • value (String)

      to assign

  • #set(key, hash) ⇒ self

    Parameters:

    • key (String)

      or name of the cookie

    Options Hash (hash):

    • :value (String)
    • :domain (String)
    • :path (String)
    • :max_age (Integer)
    • :expires (Time)
    • :secure (Boolean)
    • :http_only (Boolean)

Returns:

  • (self)


75
76
77
78
79
80
81
82
83
# File 'lib/shamu/rack/cookies.rb', line 75

def set( key, value )
  key = key.to_s
  deleted_cookies.delete( key )

  value = { value: value } unless value.is_a? Hash
  cookies[key] = value

  self
end