Class: Mechanize::Cookie

Inherits:
WEBrick::Cookie
  • Object
show all
Defined in:
lib/mechanize/cookie.rb

Overview

This class is used to represent an HTTP Cookie.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#sessionObject

Returns the value of attribute session.



7
8
9
# File 'lib/mechanize/cookie.rb', line 7

def session
  @session
end

Class Method Details

.parse(uri, str, log = Mechanize.log) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mechanize/cookie.rb', line 9

def self.parse(uri, str, log = Mechanize.log)
  return str.split(/,(?=[^;,]*=)|,$/).map { |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)
      next unless key
      value = WEBrick::HTTPUtils.dequote(value.strip) if value

      case key.downcase
      when "domain" then
        value = ".#{value}" unless value =~ /^\./
        cookie.domain = value
      when "path" then
        cookie.path = value
      when 'expires'
        if value.empty? then
          cookie.session = true
          next
        end

        begin
          cookie.expires = Time::parse(value)
        rescue
          log.warn("Couldn't parse expires: #{value}") if log
        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(%r%[^/]*$%, '')
    cookie.secure  ||= false
    cookie.domain  ||= uri.host
    # Move this in to the cookie jar
    yield cookie if block_given?

    cookie
  }
end

Instance Method Details

#expired?Boolean

Returns:

  • (Boolean)


77
78
79
80
# File 'lib/mechanize/cookie.rb', line 77

def expired?
  return false unless expires
  Time.now > expires
end

#to_sObject



82
83
84
# File 'lib/mechanize/cookie.rb', line 82

def to_s
  "#{@name}=#{@value}"
end