Class: Mechanize::CookieJar

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/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.



12
13
14
# File 'lib/mechanize/cookie_jar.rb', line 12

def initialize
  @jar = {}
end

Instance Attribute Details

#jarObject (readonly)

add_cookie wants something resembling a URI.



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

def jar
  @jar
end

Instance Method Details

#add(uri, cookie) ⇒ Object

Add a cookie to the jar if it is considered acceptable from uri. Return nil if the cookie was not added, otherwise return the cookie added.



23
24
25
26
27
# File 'lib/mechanize/cookie_jar.rb', line 23

def add(uri, cookie)
  return nil unless cookie.acceptable_from_uri?(uri)
  add!(cookie)
  cookie
end

#add!(cookie) ⇒ Object Also known as: <<

Add a cookie to the jar and return self.



30
31
32
33
34
35
36
37
38
39
# File 'lib/mechanize/cookie_jar.rb', line 30

def add!(cookie)
  normal_domain = cookie.domain.downcase

  @jar[normal_domain] ||= {} unless @jar.has_key?(normal_domain)

  @jar[normal_domain][cookie.path] ||= {}
  @jar[normal_domain][cookie.path][cookie.name] = cookie

  self
end

#clear!Object

Clear the cookie jar



132
133
134
# File 'lib/mechanize/cookie_jar.rb', line 132

def clear!
  @jar = {}
end

#cookies(url) ⇒ Object

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



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mechanize/cookie_jar.rb', line 43

def cookies(url)
  cleanup
  url.path = '/' if url.path.empty?
  now = Time.now

  select { |cookie|
    !cookie.expired? && cookie.valid_for_uri?(url) && (cookie.accessed_at = now)
  }.sort_by { |cookie|
    # RFC 6265 5.4
    # Precedence: 1. longer path  2. older creation
    [-cookie.path.length, cookie.created_at]
  }
end

#dump_cookiestxt(io) ⇒ Object

Write cookies to Mozilla cookies.txt-style IO stream



166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/mechanize/cookie_jar.rb', line 166

def dump_cookiestxt(io)
  to_a.each do |cookie|
    io.puts([
      cookie.domain,
      cookie.for_domain? ? "TRUE" : "FALSE",
      cookie.path,
      cookie.secure ? "TRUE" : "FALSE",
      cookie.expires.to_i.to_s,
      cookie.name,
      cookie.value
    ].join("\t"))
  end
end

#eachObject



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mechanize/cookie_jar.rb', line 61

def each
  block_given? or return enum_for(__method__)
  cleanup
  @jar.each { |domain, paths|
    paths.each { |path, hash|
      hash.each_value { |cookie|
        yield cookie
      }
    }
  }
end

#empty?(url) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/mechanize/cookie_jar.rb', line 57

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

#initialize_copy(other) ⇒ Object

:nodoc:



16
17
18
# File 'lib/mechanize/cookie_jar.rb', line 16

def initialize_copy other # :nodoc:
  @jar = Marshal.load Marshal.dump other.jar
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



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/mechanize/cookie_jar.rb', line 103

def load(file, format = :yaml)
  @jar = open(file) { |f|
    case format
    when :yaml then
      load_yaml

      YAML.load(f)
    when :cookiestxt then
      load_cookiestxt(f)
    else
      raise ArgumentError, "Unknown cookie jar file format"
    end
  }

  cleanup

  self
end

#load_cookiestxt(io) ⇒ Object

Read cookies from Mozilla cookies.txt-style IO stream



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/mechanize/cookie_jar.rb', line 137

def load_cookiestxt(io)
  now = Time.now

  io.each_line do |line|
    line.chomp!
    line.gsub!(/#.+/, '')
    fields = line.split("\t")

    next if fields.length != 7

    expires_seconds = fields[4].to_i
    expires = (expires_seconds == 0) ? nil : Time.at(expires_seconds)
    next if expires and (expires < now)

    c = Mechanize::Cookie.new(fields[5], fields[6])
    c.domain = fields[0]
    c.for_domain = (fields[1] == "TRUE") # Whether this cookie is for domain
    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!(c)
  end

  @jar
end

#load_yamlObject

:nodoc:



122
123
124
125
126
127
128
129
# File 'lib/mechanize/cookie_jar.rb', line 122

def load_yaml # :nodoc:
  begin
    require 'psych'
  rescue LoadError
  end

  require 'yaml'
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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/mechanize/cookie_jar.rb', line 78

def save_as(file, format = :yaml)
  jar = dup
  jar.cleanup true

  open(file, 'w') { |f|
    case format
    when :yaml then
      load_yaml

      YAML.dump(jar.jar, f)
    when :cookiestxt then
      jar.dump_cookiestxt(f)
    else
      raise ArgumentError, "Unknown cookie jar file format"
    end
  }

  self
end