Sha256: 76df250d5b54b6a6839c8cd405c9af768bb5a7884f129691a8b4b3eb1ebf6f48

Contents?: true

Size: 1.97 KB

Versions: 7

Compression:

Stored size: 1.97 KB

Contents

require 'time'
require 'webrick/cookie'

module WWW
  class Mechanize
  # This class is used to represent an HTTP Cookie.
    class Cookie < WEBrick::Cookie
      def self.parse(uri, str, log = nil)
        return str.split(/,(?=[^;,]*=)|,$/).collect { |c|
          cookie_elem = c.split(/;/)
          first_elem = cookie_elem.shift
          first_elem.strip!
          key, value = first_elem.split(/=/, 2)
          cookie = new(key, WEBrick::HTTPUtils.dequote(value))
          cookie_elem.each{|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
          }

          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
    
      def to_s
        "#{@name}=#{@value}"
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
mechanize-0.7.0 lib/www/mechanize/cookie.rb
mechanize-0.7.1 lib/www/mechanize/cookie.rb
mechanize-0.7.4 lib/www/mechanize/cookie.rb
mechanize-0.7.6 lib/www/mechanize/cookie.rb
mechanize-0.7.2 lib/www/mechanize/cookie.rb
mechanize-0.7.3 lib/www/mechanize/cookie.rb
mechanize-0.7.5 lib/www/mechanize/cookie.rb