| Class | WWW::Mechanize::Cookie |
| In: |
lib/mechanize/cookie.rb
|
| Parent: | WEBrick::Cookie |
# File lib/mechanize/cookie.rb, line 9
9: def self.parse(uri, str, log = nil)
10: return str.split(/,(?=[^;,]*=)|,$/).collect { |c|
11: cookie_elem = c.split(/;/)
12: first_elem = cookie_elem.shift
13: first_elem.strip!
14: key, value = first_elem.split(/=/, 2)
15: cookie = new(key, WEBrick::HTTPUtils.dequote(value))
16: cookie_elem.each{|pair|
17: pair.strip!
18: key, value = pair.split(/=/, 2)
19: if value
20: value = WEBrick::HTTPUtils.dequote(value.strip)
21: end
22: case key.downcase
23: when "domain" then cookie.domain = value.sub(/^\./, '')
24: when "path" then cookie.path = value
25: when 'expires'
26: begin
27: cookie.expires = Time::parse(value)
28: rescue
29: if log
30: log.warn("Couldn't parse expires: #{value}")
31: end
32: end
33: when "max-age" then
34: begin
35: cookie.max_age = Integer(value)
36: rescue
37: log.warn("Couldn't parse max age '#{value}'") if log
38: cookie.max_age = nil
39: end
40: when "comment" then cookie.comment = value
41: when "version" then
42: begin
43: cookie.version = Integer(value)
44: rescue
45: log.warn("Couldn't parse version '#{value}'") if log
46: cookie.version = nil
47: end
48: when "secure" then cookie.secure = true
49: end
50: }
51:
52: cookie.path ||= uri.path.to_s.sub(/[^\/]*$/, '')
53: cookie.secure ||= false
54: cookie.domain ||= uri.host
55: # Move this in to the cookie jar
56: yield cookie if block_given?
57: }
58: end