class Webmachine::Cookie
An HTTP Cookie
for a response, including optional attributes
Constants
- ALLOWED_ATTRIBUTES
Allowed keys for the attributes parameter of {Webmachine::Cookie#initialize}
- TBLDECWWWCOMP_
@private
- TBLENCWWWCOMP_
Copied and modified from 1.9.x URI @private
Attributes
Public Class Methods
# File lib/webmachine/cookie.rb, line 76 def initialize(name, value, attributes = {}) @name, @value, @attributes = name, value, attributes end
Parse a Cookie
header, with any number of cookies, into a hash @param [String] the Cookie
header @param [Boolean] whether to include duplicate cookie values in the
response
@return [Hash] cookie name/value pairs.
# File lib/webmachine/cookie.rb, line 11 def self.parse(cstr, include_dups = false) cookies = {} (cstr || '').split(/\s*[;,]\s*/n).each { |c| k, v = c.split(/\s*=\s*/, 2).map { |s| unescape(s) } case cookies[k] when nil cookies[k] = v when Array cookies[k] << v else cookies[k] = [cookies[k], v] if include_dups end } cookies end
Private Class Methods
Unescape a cookie @private
# File lib/webmachine/cookie.rb, line 121 def self.unescape(s, encoding = Encoding::UTF_8) URI.decode_www_form_component(s, encoding) end
Public Instance Methods
A comment allowing documentation on the intended use for the cookie
# File lib/webmachine/cookie.rb, line 57 def comment @attributes[:comment] end
The domain for which the cookie is valid
# File lib/webmachine/cookie.rb, line 52 def domain @attributes[:domain] end
The expiration {DateTime} of the cookie
# File lib/webmachine/cookie.rb, line 72 def expires @attributes[:expires] end
If the cookie is HTTP only
# File lib/webmachine/cookie.rb, line 37 def http_only? @attributes[:httponly] end
The Max-Age, in seconds, for which the cookie is valid
# File lib/webmachine/cookie.rb, line 67 def maxage @attributes[:maxage] end
The path for which the cookie is valid
# File lib/webmachine/cookie.rb, line 47 def path @attributes[:path] end
If the cookie should be treated as a secure one by the client
# File lib/webmachine/cookie.rb, line 42 def secure? @attributes[:secure] end
Convert to an RFC2109 valid cookie string @return [String] The RFC2109 valid cookie string
# File lib/webmachine/cookie.rb, line 82 def to_s attributes = ALLOWED_ATTRIBUTES.select { |a| @attributes[a] }.map do |a| case a when :httponly 'HttpOnly' if @attributes[a] when :secure 'Secure' if @attributes[a] when :maxage 'Max-Age=' + @attributes[a].to_s when :expires 'Expires=' + rfc2822(@attributes[a]) when :comment 'Comment=' + escape(@attributes[a].to_s) else a.to_s.sub(/^\w/) { $&.capitalize } + '=' + @attributes[a].to_s end end ([escape(name) + '=' + escape(value)] + attributes).join('; ') end
Which version of the state management specification the cookie conforms
# File lib/webmachine/cookie.rb, line 62 def version @attributes[:version] end
Private Instance Methods
Escape a cookie
# File lib/webmachine/cookie.rb, line 115 def escape(s) URI.encode_www_form_component(s) end
Format timestamps for the ‘Expires’ portion of the cookie string, as per RFC 2822 and 2616.
@see www.rfc-editor.org/rfc/rfc2616#section-3.3.1 @see developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Expires
# File lib/webmachine/cookie.rb, line 109 def rfc2822(time) time.strftime('%a, %d %b %Y %T GMT') end