Class: Cyberweb::CGI::Cookie

Inherits:
Array
  • Object
show all
Defined in:
lib/cyberweb/cgi/cookie.rb

Overview

Cyberweb::CGI::Cookie

Constant Summary collapse

@@accept_charset =
'UTF-8'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = '', *value) ⇒ Cookie

#

initialize

Create a new CGI::Cookie object.

:call-seq:

Cookie.new(name_string,*value)
Cookie.new(options_hash)
name_string

The name of the cookie; in this form, there is no #domain or #expiration. The #path is gleaned from the SCRIPT_NAME environment variable, and #secure is false.

*value

value or list of values of the cookie

options_hash

A Hash of options to initialize this Cookie. Possible options are:

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 the value of the SCRIPT_NAME environment variable.

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.

#


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/cyberweb/cgi/cookie.rb', line 107

def initialize(
    name = '', *value
  )
  @domain = nil
  @expires = nil
  if name.is_a? String
    @name = name
    %r|^(.*/)|.match(ENV['SCRIPT_NAME'])
    @path = ($1 or '')
    @secure = false
    return super(value)
  end

  options = name
  unless options.has_key? 'name'
    raise ArgumentError, "`name' required"
  end

  @name = options['name']
  value = Array(options["value"])
  # simple support for IE
  if options['path']
    @path = options['path']
  else
    %r|^(.*/)|.match(ENV["SCRIPT_NAME"])
    @path = ($1 or "")
  end

  @domain  = options['domain']
  @expires = options['expires']
  @secure  = options['secure'] == true ? true : false
  super(value)
end

Instance Attribute Details

#domainObject

#

Domain for which this cookie applies, as a String

#


67
68
69
# File 'lib/cyberweb/cgi/cookie.rb', line 67

def domain
  @domain
end

#expiresObject

#

Time at which this cookie expires, as a Time

#


71
72
73
# File 'lib/cyberweb/cgi/cookie.rb', line 71

def expires
  @expires
end

#nameObject

#

Name of this cookie, as a String.

#


57
58
59
# File 'lib/cyberweb/cgi/cookie.rb', line 57

def name
  @name
end

#pathObject

#

The path for which this cookie applies, as a String

#


62
63
64
# File 'lib/cyberweb/cgi/cookie.rb', line 62

def path
  @path
end

#secureObject

#

True if this cookie is secure; false otherwise

#


75
76
77
# File 'lib/cyberweb/cgi/cookie.rb', line 75

def secure
  @secure
end

Class Method Details

.parse(raw_cookie) ⇒ Object

#

Cookie.parse

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

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


194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/cyberweb/cgi/cookie.rb', line 194

def self.parse(raw_cookie)
  cookies = Hash.new([])
  return cookies unless raw_cookie
  raw_cookie.split(/[;,]\s?/).each { |pairs|
    name, values = pairs.split('=', 2)
    next unless name and values
    name = CGI.unescape(name)
    values ||= ''
    values = values.split('&').map {|v| CGI.unescape(v, @@accept_charset) }
    if cookies.has_key?(name)
      values = cookies[name].value + values
    end
    cookies[name] = Cookie.new(name, *values)
  }
  cookies
end

Instance Method Details

#inspectObject

#

inspect

A summary of the cookie string.

#


216
217
218
# File 'lib/cyberweb/cgi/cookie.rb', line 216

def inspect
  "#<Cyberweb::CGI::Cookie: #{self.to_s.inspect}>"
end

#to_sObject

#

to_s

Convert the Cookie to its string representation.

#


176
177
178
179
180
181
182
183
184
# File 'lib/cyberweb/cgi/cookie.rb', line 176

def to_s
  val = map {|v| CGI.escape(v) }.join("&")
  buf = "#{@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
  buf
end

#valueObject

#

value

Returns the value or list of values for this cookie.

#


146
147
148
# File 'lib/cyberweb/cgi/cookie.rb', line 146

def value
  self
end

#value=(i) ⇒ Object

#

value=

Replaces the value of this cookie with a new value or list of values.

#


155
156
157
# File 'lib/cyberweb/cgi/cookie.rb', line 155

def value=(i)
  replace(Array(i))
end