Class: HTTP::CookieJar

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/http/cookie_jar.rb,
lib/http/cookie_jar/hash_store.rb

Overview

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

Defined Under Namespace

Classes: AbstractSaver, AbstractStore, CookiestxtSaver, HashStore, YAMLSaver

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store = :hash, options = nil) ⇒ CookieJar

Generates a new cookie jar. The default store class is ‘:hash`, which maps to `HTTP::CookieJar::HashStore`. Any given options are passed through to the initializer of the specified store class.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/http/cookie_jar.rb', line 16

def initialize(store = :hash, options = nil)
  case store
  when Symbol
    @store = AbstractStore.implementation(store).new(options)
  when AbstractStore
    options.empty? or
      raise ArgumentError, 'wrong number of arguments (%d for 1)' % (1 + options.size)
    @store = store
  else
    raise TypeError, 'wrong object given as cookie store: %s' % store.inspect
  end
end

Instance Attribute Details

#storeObject (readonly)

Returns the value of attribute store.



11
12
13
# File 'lib/http/cookie_jar.rb', line 11

def store
  @store
end

Instance Method Details

#add(cookie, *_) ⇒ Object Also known as: <<

Adds a cookie to the jar and return self. If a given cookie has no domain or path attribute values and the origin is unknown, ArgumentError is raised.



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/http/cookie_jar.rb', line 36

def add(cookie, *_)
  _.empty? or
    raise ArgumentError, 'HTTP::Cookie equivalent for Mechanize::CookieJar#add(uri, cookie) is #add(cookie) after setting cookie.origin = uri.'

  if cookie.domain.nil? || cookie.path.nil?
    raise ArgumentError, "a cookie with unknown domain or path cannot be added"
  end

  @store.add(cookie)
  self
end

#add!(cookie) ⇒ Object

Used to exist in Mechanize::CookieJar. Use #add().

Raises:

  • (NoMethodError)


50
51
52
# File 'lib/http/cookie_jar.rb', line 50

def add!(cookie)
  raise NoMethodError, 'HTTP::Cookie equivalent for Mechanize::CookieJar#add!() is #add().'
end

#cleanup(session = false) ⇒ Object

Removes expired cookies and return self.



232
233
234
235
# File 'lib/http/cookie_jar.rb', line 232

def cleanup(session = false)
  @store.cleanup session
  self
end

#clearObject

Clears the cookie jar and return self.



221
222
223
224
# File 'lib/http/cookie_jar.rb', line 221

def clear
  @store.clear
  self
end

#clear!(*args) ⇒ Object

Used to exist in Mechanize::CookieJar. Use #clear().

Raises:

  • (NoMethodError)


227
228
229
# File 'lib/http/cookie_jar.rb', line 227

def clear!(*args)
  raise NoMethodError, 'HTTP::Cookie equivalent for Mechanize::CookieJar#clear!() is #clear().'
end

#cookies(url) ⇒ Object

Gets an array of cookies that should be sent for the URL/URI.



55
56
57
58
59
60
# File 'lib/http/cookie_jar.rb', line 55

def cookies(url)
  now = Time.now
  each(url).select { |cookie|
    !cookie.expired? && (cookie.accessed_at = now)
  }.sort
end

#each(uri = nil, &block) ⇒ Object

Iterates over all cookies that are not expired.

An optional argument uri specifies a URI/URL indicating the destination of the cookies being selected. Every cookie yielded should be good to send to the given URI, i.e. cookie.valid_for_uri?(uri) evaluates to true.

If (and only if) the uri option is given, last access time of each cookie is updated to the current time.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/http/cookie_jar.rb', line 82

def each(uri = nil, &block)
  block_given? or return enum_for(__method__, uri)

  if uri
    uri = URI(uri)
    return self unless URI::HTTP === uri && uri.host
    block = proc { |cookie|
      yield cookie if cookie.valid_for_uri?(uri)
    }
  end

  @store.each(uri, &block)
  self
end

#empty?(url = nil) ⇒ Boolean

Tests if the jar is empty. If url is given, tests if there is no cookie for the URL.

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
# File 'lib/http/cookie_jar.rb', line 64

def empty?(url = nil)
  if url
    each(url) { return false }
    return true
  else
    @store.empty?
  end
end

#initialize_copy(other) ⇒ Object



29
30
31
# File 'lib/http/cookie_jar.rb', line 29

def initialize_copy(other)
  @store = other.instance_eval { @store.dup }
end

#load(readable, *options) ⇒ Object

call-seq:

jar.load(filename_or_io, **options)
jar.load(filename_or_io, format = :yaml, **options)

Loads cookies recorded in a file or an IO in the format specified into the jar and return self. If a given object responds to #read it is taken as an IO, or taken as a filename otherwise.

Available option keywords are below:

  • format

    :yaml

    YAML structure (default)

    :cookiestxt

    Mozilla’s cookies.txt format

All options given are passed through to the underlying cookie saver module.



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/http/cookie_jar.rb', line 182

def load(readable, *options)
  opthash = {
    :format => :yaml,
    :session => false,
  }
  case options.size
  when 0
  when 1
    case options = options.first
    when Symbol
      opthash[:format] = options
    else
      opthash.update(options) if options
    end
  when 2
    opthash[:format], options = options
    opthash.update(options) if options
  else
    raise ArgumentError, 'wrong number of arguments (%d for 1-3)' % (1 + options.size)
  end

  begin
    saver = AbstractSaver.implementation(opthash[:format]).new(opthash)
  rescue IndexError => e
    raise ArgumentError, e.message
  end

  if readable.respond_to?(:write)
    saver.load(readable, self)
  else
    File.open(readable, 'r') { |io|
      saver.load(io, self)
    }
  end

  self
end

#save(writable, *options) ⇒ Object

call-seq:

jar.save(filename_or_io, **options)
jar.save(filename_or_io, format = :yaml, **options)

Saves the cookie jar into a file or an IO in the format specified and return self. If a given object responds to #write it is taken as an IO, or taken as a filename otherwise.

Available option keywords are below:

  • format

    :yaml

    YAML structure (default)

    :cookiestxt

    Mozilla’s cookies.txt format

  • session

    true

    Save session cookies as well.

    false

    Do not save session cookies. (default)

All options given are passed through to the underlying cookie saver module.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/http/cookie_jar.rb', line 121

def save(writable, *options)
  opthash = {
    :format => :yaml,
    :session => false,
  }
  case options.size
  when 0
  when 1
    case options = options.first
    when Symbol
      opthash[:format] = options
    else
      opthash.update(options) if options
    end
  when 2
    opthash[:format], options = options
    opthash.update(options) if options
  else
    raise ArgumentError, 'wrong number of arguments (%d for 1-3)' % (1 + options.size)
  end

  begin
    saver = AbstractSaver.implementation(opthash[:format]).new(opthash)
  rescue IndexError => e
    raise ArgumentError, e.message
  end

  if writable.respond_to?(:write)
    saver.save(writable, self)
  else
    File.open(writable, 'w') { |io|
      saver.save(io, self)
    }
  end

  self
end

#save_as(*args) ⇒ Object

Used to exist in Mechanize::CookieJar. Use #save().

Raises:

  • (NoMethodError)


160
161
162
# File 'lib/http/cookie_jar.rb', line 160

def save_as(*args)
  raise NoMethodError, 'HTTP::Cookie equivalent for Mechanize::CookieJar#save_as() is #save().'
end