Class: Lotus::Action::CookieJar

Inherits:
Object
  • Object
show all
Defined in:
lib/lotus/action/cookie_jar.rb

Overview

A set of HTTP Cookies

It acts as an Hash

See Also:

  • Lotus::Action::Cookies#cookies

Since:

  • 0.1.0

Constant Summary collapse

HTTP_HEADER =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The key that returns raw cookies from the Rack env

Since:

  • 0.1.0

'HTTP_COOKIE'.freeze
RACK_SESSION_KEY =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The key used by Rack to set the session cookie

We let CookieJar to NOT take care of this cookie, but it leaves the responsibility to the Rack middleware that handle sessions.

This prevents Set-Cookie to be sent twice.

:'rack.session'
'rack.request.cookie_hash'.freeze
'rack.request.cookie_string'.freeze
';,'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(env, headers, default_options) ⇒ CookieJar

Initialize the CookieJar

Parameters:

  • env (Hash)

    a raw Rack env

  • headers (Hash)

    the response headers

Since:

  • 0.1.0



56
57
58
59
60
# File 'lib/lotus/action/cookie_jar.rb', line 56

def initialize(env, headers, default_options)
  @_headers        = headers
  @cookies         = Utils::Hash.new(extract(env)).symbolize!
  @default_options = default_options
end

Instance Method Details

#[](key) ⇒ Object?

Returns the object associated with the given key

Parameters:

  • key (Symbol)

    the key

Returns:

  • (Object, nil)

    return the associated object, if found

Since:

  • 0.2.0



82
83
84
# File 'lib/lotus/action/cookie_jar.rb', line 82

def [](key)
  @cookies[key]
end

#[]=(key, value) ⇒ void

This method returns an undefined value.

Associate the given value with the given key and store them

Parameters:

  • key (Symbol)

    the key

  • value (#to_s, Hash)

    value that can be serialized as a string or expressed as a Hash

Options Hash (value):

  • :domain (String)
    • The domain

  • :path (String)
    • The path

  • :max_age (Integer)
    • Duration expressed in seconds

  • :expires (Time)
    • Expiration time

  • :secure (TrueClass, FalseClass)
    • Restrict cookie to secure

    connections

  • :httponly (TrueClass, FalseClass)
    • Restrict JavaScript

    access

See Also:

Since:

  • 0.2.0



105
106
107
# File 'lib/lotus/action/cookie_jar.rb', line 105

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

#finishvoid

This method returns an undefined value.

Finalize itself, by setting the proper headers to add and remove cookies, before the response is returned to the webserver.

See Also:

  • Lotus::Action::Cookies#finish

Since:

  • 0.1.0



70
71
72
73
# File 'lib/lotus/action/cookie_jar.rb', line 70

def finish
  @cookies.delete(RACK_SESSION_KEY)
  @cookies.each { |k,v| v.nil? ? delete_cookie(k) : set_cookie(k, _merge_default_values(v)) }
end