CookieSet

class
Superclass
Object
Included Modules
Enumerable
Extended With
Forwardable
Loggability

An object class which provides a convenient way of accessing a set of Strelka::Cookies.

Synopsis

cset = Strelka::CookieSet.new()
cset = Strelka::CookieSet.new( cookies )

cset['cookiename']  # => Strelka::Cookie

cset['cookiename'] = cookie_object
cset['cookiename'] = 'cookievalue'
cset[:cookiename] = 'cookievalue'
cset << Strelka::Cookie.new( *args )

cset.include?( 'cookiename' )
cset.include?( cookie_object )

cset.each do |cookie|
   ...
end

Authors

Public Class Methods

anchor
new( *cookies )

Create a new CookieSet prepopulated with the given cookies

# File lib/strelka/cookieset.rb, line 63
def initialize( *cookies )
        @cookie_set = Set.new( cookies.flatten )
end
anchor
parse( request )

Parse the Cookie header of the specified request into Strelka::Cookie objects and return them in a new CookieSet.

# File lib/strelka/cookieset.rb, line 50
def self::parse( request )
        self.log.debug "Parsing cookies from header: %p" % [ request.header.cookie ]
        cookies = Strelka::Cookie.parse( request.header.cookie )
        self.log.debug "  found %d cookies: %p" % [ cookies.length, cookies ]
        return new( cookies.values )
end

Public Instance Methods

anchor
<<( cookie )

Append operator: Add the given cookie to the set, replacing an existing cookie with the same name if one exists.

# File lib/strelka/cookieset.rb, line 108
def <<( cookie )
        @cookie_set.delete( cookie )
        @cookie_set.add( cookie )

        return self
end
anchor
[]( name )

Index operator method: returns the Strelka::Cookie with the given name if it exists in the cookieset.

# File lib/strelka/cookieset.rb, line 78
def []( name )
        name = name.to_s
        return @cookie_set.find {|cookie| cookie.name == name }
end
anchor
[]=( name, value )

Index set operator method: set the cookie that corresponds to the given name to value. If value is not an Strelka::Cookie, one is created and its value set to value.

# File lib/strelka/cookieset.rb, line 87
def []=( name, value )
        value = Strelka::Cookie.new( name.to_s, value ) unless value.is_a?( Strelka::Cookie )
        raise ArgumentError, "cannot set a cookie named '%s' with a key of '%s'" %
                [ value.name, name ] if value.name.to_s != name.to_s

        self << value
end
anchor
include?( name_or_cookie )

Returns true if the CookieSet includes either a cookie with the given name or an Strelka::Cookie object.

# File lib/strelka/cookieset.rb, line 98
def include?( name_or_cookie )
        return true if @cookie_set.include?( name_or_cookie )
        name = name_or_cookie.to_s
        return self[name] ? true : false
end
Also aliased as: key?
anchor
key?( name_or_cookie )
Alias for: include?