Class: CGI::Cookie
Overview
Class representing an HTTP cookie.
In addition to its specific fields and methods, a Cookie instance is a delegator to the array of its values.
See RFC 2965.
Examples of use
cookie1 = CGI::Cookie::new("name", "value1", "value2", ...)
cookie1 = CGI::Cookie::new("name" => "name", "value" => "value")
cookie1 = CGI::Cookie::new('name' => 'name',
'value' => ['value1', 'value2', ...],
'path' => 'path', # optional
'domain' => 'domain', # optional
'expires' => Time.now, # optional
'secure' => true # optional
)
cgi.out("cookie" => [cookie1, cookie2]) { "string" }
name = cookie1.name
values = cookie1.value
path = cookie1.path
domain = cookie1.domain
expires = cookie1.expires
secure = cookie1.secure
cookie1.name = 'name'
cookie1.value = ['value1', 'value2', ...]
cookie1.path = 'path'
cookie1.domain = 'domain'
cookie1.expires = Time.now + 30
cookie1.secure = true
Instance Attribute Summary collapse
-
#domain ⇒ Object
*** original *def initialize(name = “”, *value) * options = if name.kind_of?(String) * { “name” => name, “value” => value } * else * name * end * unless options.has_key?(“name”) * raise ArgumentError, “‘name’ required” * end * * @name = options * @value = Array(options) * # simple support for IE * if options * @path = options * else * %r|^(.*/)|.match(ENV) * @path = ($1 or “”) * end * @domain = options * @expires = options * @secure = options == true ? true : false * * super(@value) *end *** /original.
-
#expires ⇒ Object
*** original *def initialize(name = “”, *value) * options = if name.kind_of?(String) * { “name” => name, “value” => value } * else * name * end * unless options.has_key?(“name”) * raise ArgumentError, “‘name’ required” * end * * @name = options * @value = Array(options) * # simple support for IE * if options * @path = options * else * %r|^(.*/)|.match(ENV) * @path = ($1 or “”) * end * @domain = options * @expires = options * @secure = options == true ? true : false * * super(@value) *end *** /original.
-
#name ⇒ Object
*** original *def initialize(name = “”, *value) * options = if name.kind_of?(String) * { “name” => name, “value” => value } * else * name * end * unless options.has_key?(“name”) * raise ArgumentError, “‘name’ required” * end * * @name = options * @value = Array(options) * # simple support for IE * if options * @path = options * else * %r|^(.*/)|.match(ENV) * @path = ($1 or “”) * end * @domain = options * @expires = options * @secure = options == true ? true : false * * super(@value) *end *** /original.
-
#path ⇒ Object
*** original *def initialize(name = “”, *value) * options = if name.kind_of?(String) * { “name” => name, “value” => value } * else * name * end * unless options.has_key?(“name”) * raise ArgumentError, “‘name’ required” * end * * @name = options * @value = Array(options) * # simple support for IE * if options * @path = options * else * %r|^(.*/)|.match(ENV) * @path = ($1 or “”) * end * @domain = options * @expires = options * @secure = options == true ? true : false * * super(@value) *end *** /original.
-
#secure ⇒ Object
Returns the value of attribute secure.
-
#value ⇒ Object
*** original *def initialize(name = “”, *value) * options = if name.kind_of?(String) * { “name” => name, “value” => value } * else * name * end * unless options.has_key?(“name”) * raise ArgumentError, “‘name’ required” * end * * @name = options * @value = Array(options) * # simple support for IE * if options * @path = options * else * %r|^(.*/)|.match(ENV) * @path = ($1 or “”) * end * @domain = options * @expires = options * @secure = options == true ? true : false * * super(@value) *end *** /original.
Class Method Summary collapse
-
.parse(raw_cookie) ⇒ Object
Parse a raw cookie string into a hash of cookie-name=>Cookie pairs.
Instance Method Summary collapse
-
#[](*args) ⇒ Object
:nodoc:.
-
#[]=(index, value) ⇒ Object
:nodoc:.
-
#each(&block) ⇒ Object
:nodoc:.
-
#initialize(name = '', *value) ⇒ Cookie
constructor
Create a new CGI::Cookie object.
-
#method_missing(m, *args) ⇒ Object
:nodoc:.
-
#respond_to?(m) ⇒ Boolean
:nodoc:.
-
#to_s ⇒ Object
Convert the Cookie to its string representation.
Constructor Details
#initialize(name = '', *value) ⇒ Cookie
Create a new CGI::Cookie object.
The contents of the cookie can be specified as a name
and one or more value
arguments. Alternatively, the contents can be specified as a single hash argument. The possible keywords of this hash are as follows:
- name
-
the name of the cookie. Required.
- value
-
the cookie’s value or list of values.
- path
-
the path for which this cookie applies. Defaults to the base directory of the CGI script.
- domain
-
the domain for which this cookie applies.
- expires
-
the time at which this cookie expires, as a
Time
object. - secure
-
whether this cookie is a secure cookie or not (default to false). Secure cookies are only transmitted to HTTPS servers.
These keywords correspond to attributes of the cookie object.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/cgialt/cookie.rb', line 76 def initialize(name='', *value) rexp = %r|\A.*/| if name.is_a?(String) @name = name @value = value # value is an Array @path = rexp.match($CGI_ENV['SCRIPT_NAME']) ? $& : '' @secure = false else = name @name = ['name'] or raise ArgumentError, "`name' required" @value = Array(['value']) @path = ['path'] || (rexp.match($CGI_ENV['SCRIPT_NAME']) ? $& : '') # simple support for IE @domain = ['domain'] @expires = ['expires'] @secure = ['secure'] == true end #super(@value) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args) ⇒ Object
:nodoc:
190 191 192 |
# File 'lib/cgialt/cookie.rb', line 190 def method_missing(m, *args) ##:nodoc: @value.respond_to?(m) ? @value.__send__(m, *args) : super end |
Instance Attribute Details
#domain ⇒ Object
*** original *def initialize(name = “”, *value)
-
options = if name.kind_of?(String)
-
{ “name” => name, “value” => value }
-
else
-
name
-
end
-
unless options.has_key?(“name”)
-
raise ArgumentError, “‘name’ required”
-
end
*
-
@name = options
-
@value = Array(options)
-
# simple support for IE
-
if options
-
@path = options
-
else
-
@path = ($1 or “”)
-
end
-
@domain = options
-
@expires = options
-
@secure = options == true ? true : false
*
-
super(@value)
*end *** /original
122 123 124 |
# File 'lib/cgialt/cookie.rb', line 122 def domain @domain end |
#expires ⇒ Object
*** original *def initialize(name = “”, *value)
-
options = if name.kind_of?(String)
-
{ “name” => name, “value” => value }
-
else
-
name
-
end
-
unless options.has_key?(“name”)
-
raise ArgumentError, “‘name’ required”
-
end
*
-
@name = options
-
@value = Array(options)
-
# simple support for IE
-
if options
-
@path = options
-
else
-
@path = ($1 or “”)
-
end
-
@domain = options
-
@expires = options
-
@secure = options == true ? true : false
*
-
super(@value)
*end *** /original
122 123 124 |
# File 'lib/cgialt/cookie.rb', line 122 def expires @expires end |
#name ⇒ Object
*** original *def initialize(name = “”, *value)
-
options = if name.kind_of?(String)
-
{ “name” => name, “value” => value }
-
else
-
name
-
end
-
unless options.has_key?(“name”)
-
raise ArgumentError, “‘name’ required”
-
end
*
-
@name = options
-
@value = Array(options)
-
# simple support for IE
-
if options
-
@path = options
-
else
-
@path = ($1 or “”)
-
end
-
@domain = options
-
@expires = options
-
@secure = options == true ? true : false
*
-
super(@value)
*end *** /original
122 123 124 |
# File 'lib/cgialt/cookie.rb', line 122 def name @name end |
#path ⇒ Object
*** original *def initialize(name = “”, *value)
-
options = if name.kind_of?(String)
-
{ “name” => name, “value” => value }
-
else
-
name
-
end
-
unless options.has_key?(“name”)
-
raise ArgumentError, “‘name’ required”
-
end
*
-
@name = options
-
@value = Array(options)
-
# simple support for IE
-
if options
-
@path = options
-
else
-
@path = ($1 or “”)
-
end
-
@domain = options
-
@expires = options
-
@secure = options == true ? true : false
*
-
super(@value)
*end *** /original
122 123 124 |
# File 'lib/cgialt/cookie.rb', line 122 def path @path end |
#secure ⇒ Object
Returns the value of attribute secure.
123 124 125 |
# File 'lib/cgialt/cookie.rb', line 123 def secure @secure end |
#value ⇒ Object
*** original *def initialize(name = “”, *value)
-
options = if name.kind_of?(String)
-
{ “name” => name, “value” => value }
-
else
-
name
-
end
-
unless options.has_key?(“name”)
-
raise ArgumentError, “‘name’ required”
-
end
*
-
@name = options
-
@value = Array(options)
-
# simple support for IE
-
if options
-
@path = options
-
else
-
@path = ($1 or “”)
-
end
-
@domain = options
-
@expires = options
-
@secure = options == true ? true : false
*
-
super(@value)
*end *** /original
122 123 124 |
# File 'lib/cgialt/cookie.rb', line 122 def value @value end |
Class Method Details
.parse(raw_cookie) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/cgialt/cookie.rb', line 209 def Cookie::parse() = Hash.new([]) return if ! || .empty? for pairs in .split(/[;,]\s?/) name, value = pairs.split('=', 2) next unless name && value name = CGI.unescape(name) values = value.split('&').collect{|v| CGI.unescape(v) } if .has_key?(name) [name].value.concat(values) else [name] = self.new(name, *values) end end return end |
Instance Method Details
#[](*args) ⇒ Object
:nodoc:
181 182 183 |
# File 'lib/cgialt/cookie.rb', line 181 def [](*args) ##:nodoc: @value[*args] end |
#[]=(index, value) ⇒ Object
:nodoc:
184 185 186 |
# File 'lib/cgialt/cookie.rb', line 184 def []=(index, value) ##:nodoc: @value[index] = value end |
#each(&block) ⇒ Object
:nodoc:
187 188 189 |
# File 'lib/cgialt/cookie.rb', line 187 def each(&block) ##:nodoc: @value.each(&block) end |
#respond_to?(m) ⇒ Boolean
:nodoc:
193 194 195 |
# File 'lib/cgialt/cookie.rb', line 193 def respond_to?(m) ##:nodoc: super(m) || @value.respond_to?(m) end |
#to_s ⇒ Object
Convert the Cookie to its string representation.
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/cgialt/cookie.rb', line 138 def to_s val = @value.is_a?(String) ? CGI.escape(@value) \ : @value.collect{|v| CGI.escape(v) }.join('&') buf = "#{CGI.escape(@name)}=#{val}" buf << "; domain=#{@domain}" if @domain buf << "; path=#{@path}" if @path buf << "; expires=#{CGI.rfc1123_date(@expires)}" if @expires buf << "; secure" if @secure == true return buf end |