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

";,"

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



28
29
30
31
32
# File 'lib/hanami/action/cookie_jar.rb', line 28

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



60
61
62
# File 'lib/hanami/action/cookie_jar.rb', line 60

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



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

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 < Hanami::Action
  include Hanami::Action::Cookies

  def handle(req, res)
    # read cookies
    req.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



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

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



42
43
44
45
46
47
48
49
50
51
# File 'lib/hanami/action/cookie_jar.rb', line 42

def finish
  @cookies.delete(Action::RACK_SESSION)
  if changed?
    @cookies.each do |k, v|
      next unless changed?(k)

      v.nil? ? delete_cookie(k) : set_cookie(k, _merge_default_values(v))
    end
  end
end