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