http-cookie 0.1.2 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +4 -4
- data/lib/http/cookie/version.rb +1 -1
- data/lib/http/cookie.rb +23 -5
- data/lib/http/cookie_jar/abstract_saver.rb +1 -3
- data/lib/http/cookie_jar/abstract_store.rb +1 -3
- data/lib/http/cookie_jar/yaml_saver.rb +1 -4
- data/lib/http/cookie_jar.rb +17 -12
- metadata +1 -1
data/README.md
CHANGED
@@ -70,12 +70,12 @@ equivalent using `HTTP::Cookie`:
|
|
70
70
|
- `Mechanize::Cookie.parse`
|
71
71
|
|
72
72
|
# before
|
73
|
-
|
74
|
-
|
73
|
+
cookies1 = Mechanize::Cookie.parse(uri, set_cookie1)
|
74
|
+
cookies2 = Mechanize::Cookie.parse(uri, set_cookie2, log)
|
75
75
|
|
76
76
|
# after
|
77
|
-
|
78
|
-
|
77
|
+
cookies1 = HTTP::Cookie.parse(set_cookie1, :origin => uri)
|
78
|
+
cookies2 = HTTP::Cookie.parse(set_cookie2, :origin => uri, :logger => log)
|
79
79
|
|
80
80
|
- `Mechanize::Cookie#set_domain`
|
81
81
|
|
data/lib/http/cookie/version.rb
CHANGED
data/lib/http/cookie.rb
CHANGED
@@ -72,6 +72,10 @@ class HTTP::Cookie
|
|
72
72
|
# is called if defined. Each key can be either a symbol or a
|
73
73
|
# string, downcased or not.
|
74
74
|
#
|
75
|
+
# This methods accepts any attribute name for which a setter method
|
76
|
+
# is defined. Beware, however, any error (typically ArgumentError)
|
77
|
+
# a setter method raises will be passed through.
|
78
|
+
#
|
75
79
|
# e.g.
|
76
80
|
# new("uid", "a12345")
|
77
81
|
# new("uid", "a12345", :domain => 'example.org',
|
@@ -149,7 +153,9 @@ class HTTP::Cookie
|
|
149
153
|
|
150
154
|
# Parses a Set-Cookie header value +set_cookie+ into an array of
|
151
155
|
# Cookie objects. Parts (separated by commas) that are malformed
|
152
|
-
# are ignored.
|
156
|
+
# or invalid are silently ignored. For example, a cookie that a
|
157
|
+
# given origin is not allowed to issue is not included in the
|
158
|
+
# resulted array.
|
153
159
|
#
|
154
160
|
# If a block is given, each cookie object is passed to the block.
|
155
161
|
#
|
@@ -301,10 +307,13 @@ class HTTP::Cookie
|
|
301
307
|
raise NoMethodError, 'HTTP::Cookie equivalent for Mechanize::CookieJar#set_domain() is #domain=().'
|
302
308
|
end
|
303
309
|
|
310
|
+
# Sets the path attribute.
|
304
311
|
def path=(path)
|
305
312
|
@path = HTTP::Cookie.normalize_path(path)
|
306
313
|
end
|
307
314
|
|
315
|
+
# Sets the origin of the cookie. This initializes the `domain` and
|
316
|
+
# `path` attribute values if unknown yet.
|
308
317
|
def origin=(origin)
|
309
318
|
@origin.nil? or
|
310
319
|
raise ArgumentError, "origin cannot be changed once it is set"
|
@@ -316,6 +325,8 @@ class HTTP::Cookie
|
|
316
325
|
@origin = origin
|
317
326
|
end
|
318
327
|
|
328
|
+
# Sets the expires attribute. A `Time` object, a string
|
329
|
+
# representation of date/time, and `nil` are good values to set.
|
319
330
|
def expires=(t)
|
320
331
|
case t
|
321
332
|
when nil, Time
|
@@ -325,11 +336,14 @@ class HTTP::Cookie
|
|
325
336
|
end
|
326
337
|
end
|
327
338
|
|
339
|
+
# Tests if this cookie is expired by now, or by a given time.
|
328
340
|
def expired?(time = Time.now)
|
329
341
|
return false unless @expires
|
330
342
|
time > @expires
|
331
343
|
end
|
332
344
|
|
345
|
+
# Expires this cookie by setting the expires attribute value to a
|
346
|
+
# past date.
|
333
347
|
def expire
|
334
348
|
@expires = UNIX_EPOCH
|
335
349
|
self
|
@@ -339,6 +353,8 @@ class HTTP::Cookie
|
|
339
353
|
alias httponly? httponly
|
340
354
|
alias session? session
|
341
355
|
|
356
|
+
# Tests if it is OK to accept this cookie if it is sent from a given
|
357
|
+
# +uri.
|
342
358
|
def acceptable_from_uri?(uri)
|
343
359
|
uri = URI(uri)
|
344
360
|
return false unless URI::HTTP === uri && uri.host
|
@@ -358,6 +374,8 @@ class HTTP::Cookie
|
|
358
374
|
end
|
359
375
|
end
|
360
376
|
|
377
|
+
# Tests if it is OK to send this cookie to a given +uri+, A runtime
|
378
|
+
# error is raised if the cookie's domain is unknown.
|
361
379
|
def valid_for_uri?(uri)
|
362
380
|
if @domain.nil?
|
363
381
|
raise "cannot tell if this cookie is valid because the domain is unknown"
|
@@ -391,16 +409,16 @@ class HTTP::Cookie
|
|
391
409
|
if (HTTP::Cookie.normalize_path(origin) + './').path != @path
|
392
410
|
string << "; path=#{@path}"
|
393
411
|
end
|
394
|
-
if
|
412
|
+
if @expires
|
395
413
|
string << "; expires=#{@expires.httpdate}"
|
396
414
|
end
|
397
|
-
if
|
415
|
+
if @comment
|
398
416
|
string << "; comment=#{@comment}"
|
399
417
|
end
|
400
|
-
if httponly
|
418
|
+
if @httponly
|
401
419
|
string << "; HttpOnly"
|
402
420
|
end
|
403
|
-
if secure
|
421
|
+
if @secure
|
404
422
|
string << "; secure"
|
405
423
|
end
|
406
424
|
string
|
@@ -1,5 +1,3 @@
|
|
1
|
-
require 'http/cookie_jar'
|
2
|
-
|
3
1
|
class HTTP::CookieJar::AbstractSaver
|
4
2
|
class << self
|
5
3
|
@@class_map = {}
|
@@ -13,7 +11,7 @@ class HTTP::CookieJar::AbstractSaver
|
|
13
11
|
begin
|
14
12
|
require 'http/cookie_jar/%s_saver' % symbol
|
15
13
|
@@class_map.fetch(symbol)
|
16
|
-
rescue LoadError, IndexError
|
14
|
+
rescue LoadError, IndexError
|
17
15
|
raise IndexError, 'cookie saver unavailable: %s' % symbol.inspect
|
18
16
|
end
|
19
17
|
end
|
@@ -1,5 +1,3 @@
|
|
1
|
-
require 'http/cookie_jar'
|
2
|
-
|
3
1
|
class HTTP::CookieJar::AbstractStore
|
4
2
|
class << self
|
5
3
|
@@class_map = {}
|
@@ -13,7 +11,7 @@ class HTTP::CookieJar::AbstractStore
|
|
13
11
|
begin
|
14
12
|
require 'http/cookie_jar/%s_store' % symbol
|
15
13
|
@@class_map.fetch(symbol)
|
16
|
-
rescue LoadError, IndexError
|
14
|
+
rescue LoadError, IndexError
|
17
15
|
raise IndexError, 'cookie store unavailable: %s' % symbol.inspect
|
18
16
|
end
|
19
17
|
end
|
data/lib/http/cookie_jar.rb
CHANGED
@@ -10,6 +10,9 @@ class HTTP::CookieJar
|
|
10
10
|
|
11
11
|
attr_reader :store
|
12
12
|
|
13
|
+
# Generates a new cookie jar. The default store class is `:hash`,
|
14
|
+
# which maps to `HTTP::CookieJar::HashStore`. Any given options are
|
15
|
+
# passed through to the initializer of the specified store class.
|
13
16
|
def initialize(store = :hash, options = nil)
|
14
17
|
case store
|
15
18
|
when Symbol
|
@@ -27,7 +30,9 @@ class HTTP::CookieJar
|
|
27
30
|
@store = other.instance_eval { @store.dup }
|
28
31
|
end
|
29
32
|
|
30
|
-
#
|
33
|
+
# Adds a +cookie+ to the jar and return self. If a given cookie has
|
34
|
+
# no domain or path attribute values and the origin is unknown,
|
35
|
+
# ArgumentError is raised.
|
31
36
|
def add(cookie, *_)
|
32
37
|
_.empty? or
|
33
38
|
raise ArgumentError, 'HTTP::Cookie equivalent for Mechanize::CookieJar#add(uri, cookie) is #add(cookie) after setting cookie.origin = uri.'
|
@@ -46,7 +51,7 @@ class HTTP::CookieJar
|
|
46
51
|
raise NoMethodError, 'HTTP::Cookie equivalent for Mechanize::CookieJar#add!() is #add().'
|
47
52
|
end
|
48
53
|
|
49
|
-
#
|
54
|
+
# Gets an array of cookies that should be sent for the URL/URI.
|
50
55
|
def cookies(url)
|
51
56
|
now = Time.now
|
52
57
|
each(url).select { |cookie|
|
@@ -54,8 +59,8 @@ class HTTP::CookieJar
|
|
54
59
|
}.sort
|
55
60
|
end
|
56
61
|
|
57
|
-
# Tests if the jar is empty. If url is given, tests if there is
|
58
|
-
# cookie for the URL.
|
62
|
+
# Tests if the jar is empty. If +url+ is given, tests if there is
|
63
|
+
# no cookie for the URL.
|
59
64
|
def empty?(url = nil)
|
60
65
|
if url
|
61
66
|
each(url) { return false }
|
@@ -94,9 +99,9 @@ class HTTP::CookieJar
|
|
94
99
|
# jar.save(filename_or_io, **options)
|
95
100
|
# jar.save(filename_or_io, format = :yaml, **options)
|
96
101
|
#
|
97
|
-
#
|
98
|
-
# and return self. If
|
99
|
-
#
|
102
|
+
# Saves the cookie jar into a file or an IO in the format specified
|
103
|
+
# and return self. If a given object responds to #write it is taken
|
104
|
+
# as an IO, or taken as a filename otherwise.
|
100
105
|
#
|
101
106
|
# Available option keywords are below:
|
102
107
|
#
|
@@ -160,9 +165,9 @@ class HTTP::CookieJar
|
|
160
165
|
# jar.load(filename_or_io, **options)
|
161
166
|
# jar.load(filename_or_io, format = :yaml, **options)
|
162
167
|
#
|
163
|
-
#
|
164
|
-
# into the jar and return self. If
|
165
|
-
#
|
168
|
+
# Loads cookies recorded in a file or an IO in the format specified
|
169
|
+
# into the jar and return self. If a given object responds to #read
|
170
|
+
# it is taken as an IO, or taken as a filename otherwise.
|
166
171
|
#
|
167
172
|
# Available option keywords are below:
|
168
173
|
#
|
@@ -212,7 +217,7 @@ class HTTP::CookieJar
|
|
212
217
|
self
|
213
218
|
end
|
214
219
|
|
215
|
-
#
|
220
|
+
# Clears the cookie jar and return self.
|
216
221
|
def clear
|
217
222
|
@store.clear
|
218
223
|
self
|
@@ -223,7 +228,7 @@ class HTTP::CookieJar
|
|
223
228
|
raise NoMethodError, 'HTTP::Cookie equivalent for Mechanize::CookieJar#clear!() is #clear().'
|
224
229
|
end
|
225
230
|
|
226
|
-
#
|
231
|
+
# Removes expired cookies and return self.
|
227
232
|
def cleanup(session = false)
|
228
233
|
@store.cleanup session
|
229
234
|
self
|