Methods
Attributes
| [RW] | domain | |
| [RW] | expires | |
| [R] | http_only | |
| [RW] | name | |
| [RW] | path | |
| [R] | secure | |
| [RW] | value |
Public Class methods
Creates 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 (defaults to false). Secure cookies are only transmitted to HTTPS servers.
- :http_only - Whether this cookie can be accessed by client side scripts (e.g. document.cookie) or only over HTTP. More details in msdn2.microsoft.com/en-us/library/system.web.httpcookie.httponly.aspx. Defaults to false.
These keywords correspond to attributes of the cookie object.
[ show source ]
# File actionpack/lib/action_controller/cgi_ext/cookie.rb, line 30
30: def initialize(name = '', *value)
31: if name.kind_of?(String)
32: @name = name
33: @value = Array(value)
34: @domain = nil
35: @expires = nil
36: @secure = false
37: @http_only = false
38: @path = nil
39: else
40: @name = name['name']
41: @value = (name['value'].kind_of?(String) ? [name['value']] : Array(name['value'])).delete_if(&:blank?)
42: @domain = name['domain']
43: @expires = name['expires']
44: @secure = name['secure'] || false
45: @http_only = name['http_only'] || false
46: @path = name['path']
47: end
48:
49: raise ArgumentError, "`name' required" unless @name
50:
51: # simple support for IE
52: unless @path
53: %r|^(.*/)|.match(ENV['SCRIPT_NAME'])
54: @path = ($1 or '')
55: end
56:
57: super(@value)
58: end
Parses a raw cookie string into a hash of cookie-name => cookie-object pairs.
cookies = CGI::Cookie::parse("raw_cookie_string")
# => { "name1" => cookie1, "name2" => cookie2, ... }
[ show source ]
# File actionpack/lib/action_controller/cgi_ext/cookie.rb, line 95
95: def self.parse(raw_cookie)
96: cookies = Hash.new([])
97:
98: if raw_cookie
99: raw_cookie.split(/;\s?/).each do |pairs|
100: name, value = pairs.split('=',2)
101: next unless name and value
102: name = CGI::unescape(name)
103: unless cookies.has_key?(name)
104: cookies[name] = new(name, CGI::unescape(value))
105: end
106: end
107: end
108:
109: cookies
110: end
Public Instance methods
Sets whether the Cookie is an HTTP only cookie or not.
[ show source ]
# File actionpack/lib/action_controller/cgi_ext/cookie.rb, line 66
66: def http_only=(val)
67: @http_only = val == true
68: end
FIXME: work around broken 1.8.7 DelegateClass#respond_to?
[ show source ]
# File actionpack/lib/action_controller/cgi_ext/cookie.rb, line 84
84: def respond_to?(method, include_private = false)
85: return true if super(method)
86: return __getobj__.respond_to?(method, include_private)
87: end
Sets whether the Cookie is a secure cookie or not.
[ show source ]
# File actionpack/lib/action_controller/cgi_ext/cookie.rb, line 61
61: def secure=(val)
62: @secure = val == true
63: end
Converts the Cookie to its string representation.
[ show source ]
# File actionpack/lib/action_controller/cgi_ext/cookie.rb, line 71
71: def to_s
72: buf = ''
73: buf << @name << '='
74: buf << (@value.kind_of?(String) ? CGI::escape(@value) : @value.collect{|v| CGI::escape(v) }.join("&"))
75: buf << '; domain=' << @domain if @domain
76: buf << '; path=' << @path if @path
77: buf << '; expires=' << CGI::rfc1123_date(@expires) if @expires
78: buf << '; secure' if @secure
79: buf << '; HttpOnly' if @http_only
80: buf
81: end