Class: Cyberweb::CGI::Cookie
- Inherits:
-
Array
- Object
- Array
- Cyberweb::CGI::Cookie
- Defined in:
- lib/cyberweb/cgi/cookie.rb
Overview
Cyberweb::CGI::Cookie
Constant Summary collapse
- @@accept_charset =
'UTF-8'
Instance Attribute Summary collapse
-
#domain ⇒ Object
# Domain for which this cookie applies, as a
String
========================================================================= #. -
#expires ⇒ Object
# Time at which this cookie expires, as a
Time
========================================================================= #. -
#name ⇒ Object
# Name of this cookie, as a
String
. -
#path ⇒ Object
# The path for which this cookie applies, as a
String
========================================================================= #. -
#secure ⇒ Object
# True if this cookie is secure; false otherwise ========================================================================= #.
Class Method Summary collapse
-
.parse(raw_cookie) ⇒ Object
# === Cookie.parse.
Instance Method Summary collapse
-
#initialize(name = '', *value) ⇒ Cookie
constructor
# === initialize.
-
#inspect ⇒ Object
# === inspect.
-
#to_s ⇒ Object
# === to_s.
-
#value ⇒ Object
# === value.
-
#value=(i) ⇒ Object
# === value=.
Constructor Details
#initialize(name = '', *value) ⇒ Cookie
#
initialize
Create a new CGI::Cookie object.
:call-seq:
Cookie.new(name_string,*value)
Cookie.new()
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 = name unless .has_key? 'name' raise ArgumentError, "`name' required" end @name = ['name'] value = Array(["value"]) # simple support for IE if ['path'] @path = ['path'] else %r|^(.*/)|.match(ENV["SCRIPT_NAME"]) @path = ($1 or "") end @domain = ['domain'] @expires = ['expires'] @secure = ['secure'] == true ? true : false super(value) end |
Instance Attribute Details
#domain ⇒ Object
#
Domain for which this cookie applies, as a String
#
67 68 69 |
# File 'lib/cyberweb/cgi/cookie.rb', line 67 def domain @domain end |
#expires ⇒ Object
#
Time at which this cookie expires, as a Time
#
71 72 73 |
# File 'lib/cyberweb/cgi/cookie.rb', line 71 def expires @expires end |
#name ⇒ Object
#
Name of this cookie, as a String
.
#
57 58 59 |
# File 'lib/cyberweb/cgi/cookie.rb', line 57 def name @name end |
#path ⇒ Object
#
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 |
#secure ⇒ Object
#
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
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() = Hash.new([]) return unless .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 .has_key?(name) values = [name].value + values end [name] = Cookie.new(name, *values) } end |
Instance Method Details
#inspect ⇒ Object
#
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_s ⇒ Object
#
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 |
#value ⇒ Object
#
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 |