Class: Hanami::Action::CookieJar

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

Overview

A set of HTTP Cookies

It acts as an Hash

See Also:

  • Hanami::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/hanami/action/cookie_jar.rb', line 56

def initialize(env, headers, default_options)
  @_headers        = headers
  @cookies         = Utils::Hash.new(extract(env)).deep_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


85
86
87
# File 'lib/hanami/action/cookie_jar.rb', line 85

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):

  • :value (String)
    • Value of the cookie

  • :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


109
110
111
112
# File 'lib/hanami/action/cookie_jar.rb', line 109

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

#each(&blk) {|key, value| ... } ⇒ void

This method returns an undefined value.

Iterates cookies

Examples:

require "hanami/controller"
class MyAction
  include Hanami::Action
  include Hanami::Action::Cookies

  def call(params)
    cookies.each do |key, value|
      # ...
    end
  end
end

Parameters:

  • blk (Proc)

    the block to be yielded

Yields:

  • (key, value)

    the key/value pair for each cookie

Since:

  • 1.1.0


135
136
137
# File 'lib/hanami/action/cookie_jar.rb', line 135

def each(&blk)
  @cookies.each(&blk)
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:

  • Hanami::Action::Cookies#finish

Since:

  • 0.1.0


70
71
72
73
74
75
76
# File 'lib/hanami/action/cookie_jar.rb', line 70

def finish
  @cookies.delete(RACK_SESSION_KEY)
  @cookies.each do |k,v|
    next unless changed?(k)
    v.nil? ? delete_cookie(k) : set_cookie(k, _merge_default_values(v))
  end if changed?
end