Class: Webmachine::Cookie

Inherits:
Object
  • Object
show all
Defined in:
lib/webmachine/cookie.rb

Overview

An HTTP Cookie for a response, including optional attributes

Constant Summary

ALLOWED_ATTRIBUTES =

Allowed keys for the attributes parameter of #initialize

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

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Cookie) initialize(name, value, attributes = {})



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

- (Object) name (readonly)

Returns the value of attribute name



29
30
31
# File 'lib/webmachine/cookie.rb', line 29

def name
  @name
end

- (Object) value (readonly)

Returns the value of attribute value



29
30
31
# File 'lib/webmachine/cookie.rb', line 29

def value
  @value
end

Class Method Details

+ (Hash) parse(cstr, include_dups = false)

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)
  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

Instance Method Details

- (Object) comment

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

- (Object) domain

The domain for which the cookie is valid



52
53
54
# File 'lib/webmachine/cookie.rb', line 52

def domain
  @attributes[:domain]
end

- (Object) expires

The expiration DateTime of the cookie



72
73
74
# File 'lib/webmachine/cookie.rb', line 72

def expires
  @attributes[:expires]
end

- (Boolean) http_only?

If the cookie is HTTP only



37
38
39
# File 'lib/webmachine/cookie.rb', line 37

def http_only?
  @attributes[:httponly]
end

- (Object) maxage

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

- (Object) path

The path for which the cookie is valid



47
48
49
# File 'lib/webmachine/cookie.rb', line 47

def path
  @attributes[:path]
end

- (Boolean) secure?

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

- (String) to_s

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

- (Object) version

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