Parent

Mechanize::Cookie

This class is used to represent an HTTP Cookie.

Public Class Methods

parse(uri, str, log = Mechanize.log) click to toggle source
# File lib/mechanize/cookie.rb, line 7
def self.parse(uri, str, log = Mechanize.log)
  return str.split(/,(?=[^;,]*=)|,$/).collect { |c|
    cookie_elem = c.split(/;+/)
    first_elem = cookie_elem.shift
    first_elem.strip!
    key, value = first_elem.split(/\=/, 2)

    cookie = nil
    begin
      cookie = new(key, value.dup)
    rescue
      log.warn("Couldn't parse key/value: #{first_elem}") if log
    end
    next unless cookie

    cookie_elem.each do |pair|
      pair.strip!
      key, value = pair.split(/\=/, 2)
      if value
        value = WEBrick::HTTPUtils.dequote(value.strip)
      end
      case key.downcase
      when "domain"  then cookie.domain  = value.sub(/^\./, '')
      when "path"    then cookie.path    = value
      when 'expires'
        begin
          cookie.expires = Time::parse(value)
        rescue
          if log
            log.warn("Couldn't parse expires: #{value}")
          end
        end
      when "max-age" then
        begin
          cookie.max_age = Integer(value)
        rescue
          log.warn("Couldn't parse max age '#{value}'") if log
          cookie.max_age = nil
        end
      when "comment" then cookie.comment = value
      when "version" then
        begin
          cookie.version = Integer(value)
        rescue
          log.warn("Couldn't parse version '#{value}'") if log
          cookie.version = nil
        end
      when "secure"  then cookie.secure = true
      end
    end

    cookie.path    ||= uri.path.to_s.sub(%[^/]*$%, '')
    cookie.secure  ||= false
    cookie.domain  ||= uri.host
    # Move this in to the cookie jar
    yield cookie if block_given?
  }
end

Public Instance Methods

expired?() click to toggle source
# File lib/mechanize/cookie.rb, line 66
def expired?
  if expires.nil?
    false
  else
    Time.now > expires
  end
end
to_s() click to toggle source
# File lib/mechanize/cookie.rb, line 74
def to_s
  "#{@name}=#{@value}"
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.