Class: Strelka::CookieSet
- Inherits:
-
Object
- Object
- Strelka::CookieSet
- Extended by:
- Forwardable, Loggability
- Includes:
- Enumerable
- Defined in:
- lib/strelka/cookieset.rb
Overview
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
- Michael Granger [email protected]
- Jeremiah Jordan [email protected]
Class Method Summary collapse
-
.parse(request) ⇒ Object
Parse the Cookie header of the specified
requestinto Strelka::Cookie objects and return them in a new CookieSet.
Instance Method Summary collapse
-
#<<(cookie) ⇒ Object
Append operator: Add the given
cookieto the set, replacing an existing cookie with the same name if one exists. -
#[](name) ⇒ Object
Index operator method: returns the Strelka::Cookie with the given
nameif it exists in the cookieset. -
#[]=(name, value) ⇒ Object
Index set operator method: set the cookie that corresponds to the given
nametovalue. -
#include?(name_or_cookie) ⇒ Boolean
(also: #key?)
Returns
trueif the CookieSet includes either a cookie with the given name or an Strelka::Cookie object. -
#initialize(*cookies) ⇒ CookieSet
constructor
Create a new CookieSet prepopulated with the given cookies.
Constructor Details
#initialize(*cookies) ⇒ CookieSet
Create a new CookieSet prepopulated with the given cookies
64 65 66 |
# File 'lib/strelka/cookieset.rb', line 64 def initialize( * ) @cookie_set = Set.new( .flatten ) end |
Class Method Details
.parse(request) ⇒ Object
Parse the Cookie header of the specified request into Strelka::Cookie objects
and return them in a new CookieSet.
51 52 53 54 55 56 |
# File 'lib/strelka/cookieset.rb', line 51 def self::parse( request ) self.log.debug "Parsing cookies from header: %p" % [ request.header. ] = Strelka::Cookie.parse( request.header. ) self.log.debug " found %d cookies: %p" % [ .length, ] return new( .values ) end |
Instance Method Details
#<<(cookie) ⇒ Object
Append operator: Add the given cookie to the set, replacing an existing
cookie with the same name if one exists.
109 110 111 112 113 114 |
# File 'lib/strelka/cookieset.rb', line 109 def <<( ) @cookie_set.delete( ) @cookie_set.add( ) return self end |
#[](name) ⇒ Object
Index operator method: returns the Strelka::Cookie with the given name if it
exists in the cookieset.
79 80 81 82 |
# File 'lib/strelka/cookieset.rb', line 79 def []( name ) name = name.to_s return @cookie_set.find {|| .name == name } end |
#[]=(name, value) ⇒ Object
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.
88 89 90 91 92 93 94 |
# File 'lib/strelka/cookieset.rb', line 88 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 |
#include?(name_or_cookie) ⇒ Boolean Also known as: key?
Returns true if the CookieSet includes either a cookie with the given name or
an Strelka::Cookie object.
99 100 101 102 103 |
# File 'lib/strelka/cookieset.rb', line 99 def include?( ) return true if @cookie_set.include?( ) name = .to_s return self[name] ? true : false end |