class FTW::Cookies::Cookie
This is a Cookie
. It expires, has a value, a name, etc. I could have used stdlib CGI::Cookie, but it actually parses cookie strings incorrectly and also lacks the 'httponly' attribute.
Constants
- STANDARD_ATTRIBUTES
List of standard cookie attributes
Attributes
comment[RW]
domain[RW]
expires[RW]
httponly[RW]
name[RW]
path[RW]
secure[RW]
value[RW]
Public Class Methods
new(name, value=nil, attributes={})
click to toggle source
A new cookie. Value and attributes are optional.
# File lib/ftw/cookies.rb, line 28 def initialize(name, value=nil, attributes={}) @name = name @value = value STANDARD_ATTRIBUTES.each do |iv| instance_variable_set("@#{iv.to_s}", attributes.delete(iv)) end if !attributes.empty? raise InvalidArgument.new("Invalid Cookie attributes: #{attributes.inspect}") end end
parse(set_cookie_string)
click to toggle source
See RFC6265 section 4.1.1
# File lib/ftw/cookies.rb, line 42 def self.parse(set_cookie_string) @logger ||= Cabin::Channel.get($0) # TODO(sissel): Implement # grammar is: # set-cookie-string = cookie-pair *( ";" SP cookie-av ) # cookie-pair = cookie-name "=" cookie-value # cookie-name = token # cookie-value = *cookie-octet / ( DQUOTE *cookie-octet DQUOTE ) pair, *attributes = set_cookie_string.split(/\s*;\s*/) name, value = pair.split(/\s*=\s*/) extra = {} attributes.each do |attr| case attr when /^Expires=/ #extra[:expires] = when /^Max-Age=/ # TODO(sissel): Parse the Max-Age value and convert it to 'expires' #extra[:expires] = when /^Domain=/ extra[:domain] = attr[7..-1] when /^Path=/ extra[:path] = attr[5..-1] when /^Secure/ extra[:secure] = true when /^HttpOnly/ extra[:httponly] = true else end end end