Class: WWW::Mechanize::CookieJar

Inherits:
Object
  • Object
show all
Defined in:
lib/www/mechanize/cookie_jar.rb

Overview

This class is used to manage the Cookies that have been returned from any particular website.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCookieJar

Returns a new instance of CookieJar.



10
11
12
# File 'lib/www/mechanize/cookie_jar.rb', line 10

def initialize
  @jar = {}
end

Instance Attribute Details

#jarObject (readonly)

Returns the value of attribute jar.



8
9
10
# File 'lib/www/mechanize/cookie_jar.rb', line 8

def jar
  @jar
end

Instance Method Details

#add(uri, cookie) ⇒ Object

Add a cookie to the Jar.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/www/mechanize/cookie_jar.rb', line 15

def add(uri, cookie)
  return unless uri.host =~ /#{CookieJar.strip_port(cookie.domain)}$/i
  normal_domain = cookie.domain.downcase
  unless @jar.has_key?(normal_domain)
    @jar[normal_domain] = Hash.new
  end
    
  @jar[normal_domain][cookie.name] = cookie
  cleanup()
  cookie
end

#clear!Object

Clear the cookie jar



100
101
102
# File 'lib/www/mechanize/cookie_jar.rb', line 100

def clear!
  @jar = {}
end

#cookies(url) ⇒ Object

Fetch the cookies that should be used for the URI object passed in.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/www/mechanize/cookie_jar.rb', line 28

def cookies(url)
  cleanup
  cookies = []
  url.path = '/' if url.path.empty?
  @jar.each_key do |domain|
    if url.host =~ /#{CookieJar.strip_port(domain)}$/i
      @jar[domain].each_key do |name|
        if url.path =~ /^#{@jar[domain][name].path}/
          if @jar[domain][name].expires.nil?
            cookies << @jar[domain][name]
          elsif Time.now < @jar[domain][name].expires
            cookies << @jar[domain][name]
          end
        end
      end
    end
  end
    
  cookies
end

#dump_cookiestxt(io) ⇒ Object

Write cookies to Mozilla cookies.txt-style IO stream



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/www/mechanize/cookie_jar.rb', line 142

def dump_cookiestxt(io)
  @jar.each_pair do |domain, cookies|
    cookies.each_pair do |name, cookie|
      fields = []
      fields[0] = cookie.domain
      
      if cookie.domain =~ /^\./
        fields[1] = "TRUE"
      else
        fields[1] = "FALSE"
      end
      
      fields[2] = cookie.path
      
      if cookie.secure == true
        fields[3] = "TRUE"
      else
        fields[3] = "FALSE"
      end
      
      fields[4] = cookie.expires.to_i.to_s
      
      fields[5] = cookie.name
      fields[6] = cookie.value
      io.puts(fields.join("\t"))
    end 
  end
end

#empty?(url) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/www/mechanize/cookie_jar.rb', line 49

def empty?(url)
  cookies(url).length > 0 ? false : true
end

#load(file, format = :yaml) ⇒ Object

Load cookie jar from a file in the format specified.

Available formats: :yaml <- YAML structure. :cookiestxt <- Mozilla’s cookies.txt format



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/www/mechanize/cookie_jar.rb', line 86

def load(file, format = :yaml)
  @jar = ::File.open(file) { |f|
    case format
    when :yaml then
      YAML::load(f)
    when :cookiestxt then
      load_cookiestxt(f)
    else
      raise "Unknown cookie jar file format"
    end
  }
end

#load_cookiestxt(io) ⇒ Object

Read cookies from Mozilla cookies.txt-style IO stream



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/www/mechanize/cookie_jar.rb', line 106

def load_cookiestxt(io)
  now = Time.now
  fakeuri = Struct.new(:host)    # add_cookie wants something resembling a URI.
  
  io.each_line do |line|
    line.chomp!
    line.gsub!(/#.+/, '')
    fields = line.split("\t")
    
    next if fields.length != 7
    
    expires_seconds = fields[4].to_i
    begin
      expires = Time.at(expires_seconds)
    rescue
      next
      # Just in case we ever decide to support DateTime...
      # expires = DateTime.new(1970,1,1) + ((expires_seconds + 1) / (60*60*24.0))
    end
    next if expires < now
    
    c = WWW::Mechanize::Cookie.new(fields[5], fields[6])
    c.domain = fields[0]
    # Field 1 indicates whether the cookie can be read by other machines at the same domain.
    # This is computed by the cookie implementation, based on the domain value.
    c.path = fields[2]               # Path for which the cookie is relevant
    c.secure = (fields[3] == "TRUE") # Requires a secure connection
    c.expires = expires             # Time the cookie expires.
    c.version = 0                   # Conforms to Netscape cookie spec.
    
    add(fakeuri.new(c.domain), c)
  end
  @jar
end

#save_as(file, format = :yaml) ⇒ Object

Save the cookie jar to a file in the format specified.

Available formats: :yaml <- YAML structure :cookiestxt <- Mozilla’s cookies.txt format



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/www/mechanize/cookie_jar.rb', line 68

def save_as(file, format = :yaml)
  ::File.open(file, "w") { |f|
    case format
    when :yaml then
      YAML::dump(@jar, f)
    when :cookiestxt then
      dump_cookiestxt(f)
    else
      raise "Unknown cookie jar file format"
    end
  }
end

#to_aObject



53
54
55
56
57
58
59
60
61
# File 'lib/www/mechanize/cookie_jar.rb', line 53

def to_a
  cookies = []
  @jar.each_key do |domain|
    @jar[domain].each_key do |name|
      cookies << @jar[domain][name]
    end
  end
  cookies
end