Class: Webmachine::Cookie
- Inherits:
-
Object
- Object
- Webmachine::Cookie
- Defined in:
- lib/webmachine/cookie.rb
Overview
An HTTP Cookie for a response, including optional attributes
Constant Summary collapse
- ALLOWED_ATTRIBUTES =
Allowed keys for the attributes parameter of #initialize
[:secure, :httponly, :path, :domain, :comment, :maxage, :expires, :version]
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
-
.parse(cstr, include_dups = false) ⇒ Hash
Parse a Cookie header, with any number of cookies, into a hash.
Instance Method Summary collapse
-
#comment ⇒ Object
A comment allowing documentation on the intended use for the cookie.
-
#domain ⇒ Object
The domain for which the cookie is valid.
-
#expires ⇒ Object
The expiration DateTime of the cookie.
-
#http_only? ⇒ Boolean
If the cookie is HTTP only.
-
#initialize(name, value, attributes = {}) ⇒ Cookie
constructor
A new instance of Cookie.
-
#maxage ⇒ Object
The Max-Age, in seconds, for which the cookie is valid.
-
#path ⇒ Object
The path for which the cookie is valid.
-
#secure? ⇒ Boolean
If the cookie should be treated as a secure one by the client.
-
#to_s ⇒ String
Convert to an RFC2109 valid cookie string.
-
#version ⇒ Object
Which version of the state management specification the cookie conforms.
Constructor Details
#initialize(name, value, attributes = {}) ⇒ Cookie
Returns a new instance of Cookie.
76 77 78 |
# File 'lib/webmachine/cookie.rb', line 76 def initialize(name, value, attributes = {}) @name, @value, @attributes = name, value, attributes end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
29 30 31 |
# File 'lib/webmachine/cookie.rb', line 29 def name @name end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
29 30 31 |
# File 'lib/webmachine/cookie.rb', line 29 def value @value end |
Class Method Details
.parse(cstr, include_dups = false) ⇒ Hash
Parse a Cookie header, with any number of cookies, into a hash
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/webmachine/cookie.rb', line 11 def self.parse(cstr, include_dups = false) = {} (cstr || '').split(/\s*[;,]\s*/n).each { |c| k,v = c.split(/\s*=\s*/, 2).map { |s| unescape(s) } case [k] when nil [k] = v when Array [k] << v else [k] = [[k], v] if include_dups end } end |
Instance Method Details
#comment ⇒ Object
A comment allowing documentation on the intended use for the cookie
57 58 59 |
# File 'lib/webmachine/cookie.rb', line 57 def comment @attributes[:comment] end |
#domain ⇒ Object
The domain for which the cookie is valid
52 53 54 |
# File 'lib/webmachine/cookie.rb', line 52 def domain @attributes[:domain] end |
#expires ⇒ Object
The expiration DateTime of the cookie
72 73 74 |
# File 'lib/webmachine/cookie.rb', line 72 def expires @attributes[:expires] end |
#http_only? ⇒ Boolean
If the cookie is HTTP only
37 38 39 |
# File 'lib/webmachine/cookie.rb', line 37 def http_only? @attributes[:httponly] end |
#maxage ⇒ Object
The Max-Age, in seconds, for which the cookie is valid
67 68 69 |
# File 'lib/webmachine/cookie.rb', line 67 def maxage @attributes[:maxage] end |
#path ⇒ Object
The path for which the cookie is valid
47 48 49 |
# File 'lib/webmachine/cookie.rb', line 47 def path @attributes[:path] end |
#secure? ⇒ Boolean
If the cookie should be treated as a secure one by the client
42 43 44 |
# File 'lib/webmachine/cookie.rb', line 42 def secure? @attributes[:secure] end |
#to_s ⇒ String
Convert to an RFC2109 valid cookie string
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# 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 |
#version ⇒ Object
Which version of the state management specification the cookie conforms
62 63 64 |
# File 'lib/webmachine/cookie.rb', line 62 def version @attributes[:version] end |