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 collapse

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

#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

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#valueObject (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

Parameters:

  • the (String)

    Cookie header

  • whether (Boolean)

    to include duplicate cookie values in the response

Returns:

  • (Hash)

    cookie name/value pairs.



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

#commentObject

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

#domainObject

The domain for which the cookie is valid



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

def domain
  @attributes[:domain]
end

#expiresObject

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

Returns:

  • (Boolean)


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

def http_only?
  @attributes[:httponly]
end

#maxageObject

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

#pathObject

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

Returns:

  • (Boolean)


42
43
44
# File 'lib/webmachine/cookie.rb', line 42

def secure?
  @attributes[:secure]
end

#to_sString

Convert to an RFC2109 valid cookie string

Returns:

  • (String)

    The 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

#versionObject

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