Class: CGI::Cookie

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/cgialt/cookie.rb

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

Class Method Summary collapse

Instance Method Summary collapse

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
    options = name
    @name  = options['name']  or raise ArgumentError, "`name' required"
    @value = Array(options['value'])
    @path  = options['path'] || (rexp.match($CGI_ENV['SCRIPT_NAME']) ? $& : '')  # simple support for IE
    @domain  = options['domain']
    @expires = options['expires']
    @secure  = options['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

#domainObject

*** 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

*

*

  • super(@value)

*end *** /original



122
123
124
# File 'lib/cgialt/cookie.rb', line 122

def domain
  @domain
end

#expiresObject

*** 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

*

*

  • super(@value)

*end *** /original



122
123
124
# File 'lib/cgialt/cookie.rb', line 122

def expires
  @expires
end

#nameObject

*** 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

*

*

  • super(@value)

*end *** /original



122
123
124
# File 'lib/cgialt/cookie.rb', line 122

def name
  @name
end

#pathObject

*** 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

*

*

  • super(@value)

*end *** /original



122
123
124
# File 'lib/cgialt/cookie.rb', line 122

def path
  @path
end

#secureObject

Returns the value of attribute secure.



123
124
125
# File 'lib/cgialt/cookie.rb', line 123

def secure
  @secure
end

#valueObject

*** 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

*

*

  • 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

Parse a raw cookie string into a hash of cookie-name=>Cookie pairs.

cookies = CGI::Cookie::parse("raw_cookie_string")
  # { "name1" => cookie1, "name2" => cookie2, ... }


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(raw_cookie)
  cookies = Hash.new([])
  return cookies if !raw_cookie || raw_cookie.empty?
  for pairs in raw_cookie.split(/[;,]\s?/)
    name, value = pairs.split('=', 2)
    next unless name && value
    name = CGI.unescape(name)
    values = value.split('&').collect{|v| CGI.unescape(v) }
    if cookies.has_key?(name)
      cookies[name].value.concat(values)
    else
      cookies[name] = self.new(name, *values)
    end
  end
  return cookies
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:

Returns:

  • (Boolean)


193
194
195
# File 'lib/cgialt/cookie.rb', line 193

def respond_to?(m)  ##:nodoc:
  super(m) || @value.respond_to?(m)
end

#to_sObject

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