From 94d28bdfbba58fa8ce857985f0a50778ad8757c0 Mon Sep 17 00:00:00 2001 From: Akinori MUSHA Date: Mon, 15 Apr 2013 09:09:04 +0900 Subject: [PATCH] Update examples again. --- README.md | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 06e205c..5dd354f 100644 --- a/README.md +++ b/README.md @@ -34,15 +34,20 @@ Or install it yourself as: # Load from a file jar.load(filename) if File.exist?(filename) - # Store received cookies - jar.parse(set_cookie_header_value, uri) + # Store received cookies, where uri is the origin of this header + header["Set-Cookie"].each { |value| + jar.parse(value, uri) + } - # Get the value for the Cookie field of a request header - cookie_header_value = HTTP::Cookie.cookie_value(jar.cookies(uri)) + # ... + + # Set the Cookie header value, where uri is the destination URI + header["Cookie"] = HTTP::Cookie.cookie_value(jar.cookies(uri)) # Save to a file jar.save(filename) + ######################## # Client side example 2 ######################## @@ -50,35 +55,39 @@ Or install it yourself as: # Initialize a cookie jar using a Mozilla compatible SQLite3 backend jar = HTTP::CookieJar.new(store: :mozilla, filename: 'cookies.sqlite') - # Store received cookies - jar.parse(set_cookie_header_value, uri) - - # Get the value for the Cookie field of a request header - cookie_header_value = HTTP::Cookie.cookie_value(jar.cookies(uri)) - # There is no need for load & save in this backend. + # Store received cookies, where uri is the origin of this header + header["Set-Cookie"].each { |value| + jar.parse(value, uri) + } + + # ... + + # Set the Cookie header value, where uri is the destination URI + header["Cookie"] = HTTP::Cookie.cookie_value(jar.cookies(uri)) + ######################## # Server side example ######################## # Generate a domain cookie - cookie2 = HTTP::Cookie.new("uid", "u12345", domain: 'example.org', + cookie1 = HTTP::Cookie.new("uid", "u12345", domain: 'example.org', for_domain: true, path: '/', max_age: 7*86400) + # Add it to the Set-Cookie response header + header['Set-Cookie'] = cookie1.set_cookie_value + # Generate a host-only cookie - cookie1 = HTTP::Cookie.new("aid", "a12345", origin: my_url, + cookie2 = HTTP::Cookie.new("aid", "a12345", origin: my_url, path: '/', max_age: 7*86400) - # Set the Set-Cookie response header value - header['Set-Cookie'] = [ - cookie1.set_cookie_value, - cookie2.set_cookie_value, - ] + # Add it to the Set-Cookie response header + header['Set-Cookie'] = cookie2.set_cookie_value ## Incompatibilities with Mechanize::Cookie/CookieJar