Class: FTW::Cookies::Cookie

Inherits:
Object
  • Object
show all
Defined in:
lib/ftw/cookies.rb

Overview

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.

Constant Summary collapse

STANDARD_ATTRIBUTES =

List of standard cookie attributes

[:domain, :path, :comment, :expires, :secure, :httponly]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value = nil, attributes = {}) ⇒ Cookie

A new cookie. Value and attributes are optional.



28
29
30
31
32
33
34
35
36
37
38
39
# 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

Instance Attribute Details

#commentObject

Returns the value of attribute comment.



16
17
18
# File 'lib/ftw/cookies.rb', line 16

def comment
  @comment
end

#domainObject

Returns the value of attribute domain.



14
15
16
# File 'lib/ftw/cookies.rb', line 14

def domain
  @domain
end

#expiresObject

covers both ‘expires’ and ‘max-age’ behavior



17
18
19
# File 'lib/ftw/cookies.rb', line 17

def expires
  @expires
end

#httponlyObject

part of RFC6265



19
20
21
# File 'lib/ftw/cookies.rb', line 19

def httponly
  @httponly
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/ftw/cookies.rb', line 11

def name
  @name
end

#pathObject

Returns the value of attribute path.



15
16
17
# File 'lib/ftw/cookies.rb', line 15

def path
  @path
end

#secureObject

Returns the value of attribute secure.



18
19
20
# File 'lib/ftw/cookies.rb', line 18

def secure
  @secure
end

#valueObject

Returns the value of attribute value.



12
13
14
# File 'lib/ftw/cookies.rb', line 12

def value
  @value
end

Class Method Details

.parse(set_cookie_string) ⇒ Object

See RFC6265 section 4.1.1



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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