Class: Mechanize::Cookie
- Inherits:
-
Object
- Object
- Mechanize::Cookie
- Defined in:
- lib/mechanize/cookie.rb
Overview
This class is used to represent an HTTP Cookie.
Instance Attribute Summary collapse
-
#accessed_at ⇒ Object
Returns the value of attribute accessed_at.
-
#comment ⇒ Object
Returns the value of attribute comment.
-
#created_at ⇒ Object
Returns the value of attribute created_at.
-
#domain ⇒ Object
Returns the value of attribute domain.
-
#for_domain ⇒ Object
(also: #for_domain?)
If this flag is true, this cookie will be sent to any host in the
domain
. -
#max_age ⇒ Object
Returns the value of attribute max_age.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#path ⇒ Object
Returns the value of attribute path.
-
#secure ⇒ Object
(also: #secure?)
Returns the value of attribute secure.
-
#session ⇒ Object
Returns the value of attribute session.
-
#value ⇒ Object
Returns the value of attribute value.
-
#version ⇒ Object
Returns the value of attribute version.
Class Method Summary collapse
-
.parse(uri, str, log = Mechanize.log) ⇒ Object
Parses a Set-Cookie header line
str
sent fromuri
into an array of Cookie objects.
Instance Method Summary collapse
- #acceptable_from_uri?(uri) ⇒ Boolean
- #expired? ⇒ Boolean
- #expires ⇒ Object
- #expires=(t) ⇒ Object
- #init_with(coder) ⇒ Object
-
#initialize(*args) ⇒ Cookie
constructor
:call-seq: new(name, value) new(name, value, attr_hash) new(attr_hash).
-
#set_domain ⇒ Object
Sets the attribute domain.
- #to_s ⇒ Object
- #valid_for_uri?(uri) ⇒ Boolean
- #yaml_initialize(tag, map) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Cookie
:call-seq:
new(name, value)
new(name, value, attr_hash)
new(attr_hash)
Creates a cookie object. For each key of attr_hash
, the setter is called if defined. Each key can be either a symbol or a string, downcased or not.
e.g.
new("uid", "a12345")
new("uid", "a12345", :domain => 'example.org',
:for_domain => true, :expired => Time.now + 7*86400)
new("name" => "uid", "value" => "a12345", "Domain" => 'www.example.org')
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/mechanize/cookie.rb', line 32 def initialize(*args) @version = 0 # Netscape Cookie @domain = @path = @secure = @comment = @max_age = @expires = @comment_url = @discard = @port = nil @created_at = @accessed_at = Time.now case args.size when 2 @name, @value = *args @for_domain = false return when 3 @name, @value, attr_hash = *args when 1 attr_hash = args.first else raise ArgumentError, "wrong number of arguments (#{args.size} for 1-3)" end for_domain = false attr_hash.each_pair { |key, val| skey = key.to_s.downcase skey.sub!(/[!?]\z/, '') case skey when 'for_domain' for_domain = !!val when 'name' @name = val when 'value' @value = val else setter = :"#{skey}=" send(setter, val) if respond_to?(setter) end } @for_domain = for_domain end |
Instance Attribute Details
#accessed_at ⇒ Object
Returns the value of attribute accessed_at.
15 16 17 |
# File 'lib/mechanize/cookie.rb', line 15 def accessed_at @accessed_at end |
#comment ⇒ Object
Returns the value of attribute comment.
10 11 12 |
# File 'lib/mechanize/cookie.rb', line 10 def comment @comment end |
#created_at ⇒ Object
Returns the value of attribute created_at.
14 15 16 |
# File 'lib/mechanize/cookie.rb', line 14 def created_at @created_at end |
#domain ⇒ Object
Returns the value of attribute domain.
9 10 11 |
# File 'lib/mechanize/cookie.rb', line 9 def domain @domain end |
#for_domain ⇒ Object Also known as: for_domain?
If this flag is true, this cookie will be sent to any host in the domain
. If it is false, this cookie will be sent only to the host indicated by the domain
.
73 74 75 |
# File 'lib/mechanize/cookie.rb', line 73 def for_domain @for_domain end |
#max_age ⇒ Object
Returns the value of attribute max_age.
10 11 12 |
# File 'lib/mechanize/cookie.rb', line 10 def max_age @max_age end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/mechanize/cookie.rb', line 7 def name @name end |
#path ⇒ Object
Returns the value of attribute path.
9 10 11 |
# File 'lib/mechanize/cookie.rb', line 9 def path @path end |
#secure ⇒ Object Also known as: secure?
Returns the value of attribute secure.
9 10 11 |
# File 'lib/mechanize/cookie.rb', line 9 def secure @secure end |
#session ⇒ Object
Returns the value of attribute session.
12 13 14 |
# File 'lib/mechanize/cookie.rb', line 12 def session @session end |
#value ⇒ Object
Returns the value of attribute value.
8 9 10 |
# File 'lib/mechanize/cookie.rb', line 8 def value @value end |
#version ⇒ Object
Returns the value of attribute version.
8 9 10 |
# File 'lib/mechanize/cookie.rb', line 8 def version @version end |
Class Method Details
.parse(uri, str, log = Mechanize.log) ⇒ Object
Parses a Set-Cookie header line str
sent from uri
into an array of Cookie objects. Note that this array may contain nil’s when some of the cookie-pairs are malformed.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 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 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/mechanize/cookie.rb', line 80 def parse(uri, str, log = Mechanize.log) return str.split(/,(?=[^;,]*=)|,$/).map { |c| = c.split(/;+/) first_elem = .shift first_elem.strip! key, value = first_elem.split(/\=/, 2) begin = new(key, value.dup) rescue log.warn("Couldn't parse key/value: #{first_elem}") if log next end .each do |pair| pair.strip! key, value = pair.split(/\=/, 2) next unless key value = WEBrick::HTTPUtils.dequote(value.strip) if value case key.downcase when 'domain' begin .domain = value .for_domain = true rescue log.warn("Couldn't parse domain: #{value}") if log end when 'path' .path = value when 'expires' if value.empty? .session = true next end begin .expires = Time::parse(value) rescue log.warn("Couldn't parse expires: #{value}") if log end when 'max-age' begin .max_age = Integer(value) rescue log.warn("Couldn't parse max age '#{value}'") if log end when 'comment' .comment = value when 'version' begin .version = Integer(value) rescue log.warn("Couldn't parse version '#{value}'") if log .version = nil end when 'secure' .secure = true end end .path ||= (uri + './').path .secure ||= false .domain ||= uri.host # Move this in to the cookie jar yield if block_given? } end |
Instance Method Details
#acceptable_from_uri?(uri) ⇒ Boolean
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/mechanize/cookie.rb', line 191 def acceptable_from_uri?(uri) host = DomainName.new(uri.host) # RFC 6265 5.3 # When the user agent "receives a cookie": return host.hostname == domain unless @for_domain if host.(@domain_name) true elsif host.hostname == domain @for_domain = false true else false end end |
#expired? ⇒ Boolean
184 185 186 187 |
# File 'lib/mechanize/cookie.rb', line 184 def expired? return false unless expires Time.now > expires end |
#expires ⇒ Object
180 181 182 |
# File 'lib/mechanize/cookie.rb', line 180 def expires @expires && Time.parse(@expires) end |
#expires=(t) ⇒ Object
176 177 178 |
# File 'lib/mechanize/cookie.rb', line 176 def expires=(t) @expires = t && (t.is_a?(Time) ? t.httpdate : t.to_s) end |
#init_with(coder) ⇒ Object
217 218 219 |
# File 'lib/mechanize/cookie.rb', line 217 def init_with(coder) yaml_initialize(coder.tag, coder.map) end |
#set_domain ⇒ Object
Sets the attribute domain
152 153 154 |
# File 'lib/mechanize/cookie.rb', line 152 def domain=(value) @domain = value end |
#to_s ⇒ Object
213 214 215 |
# File 'lib/mechanize/cookie.rb', line 213 def to_s "#{@name}=#{@value}" end |
#valid_for_uri?(uri) ⇒ Boolean
208 209 210 211 |
# File 'lib/mechanize/cookie.rb', line 208 def valid_for_uri?(uri) return false if secure? && uri.scheme != 'https' acceptable_from_uri?(uri) && uri.path.start_with?(path) end |
#yaml_initialize(tag, map) ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/mechanize/cookie.rb', line 221 def yaml_initialize(tag, map) @for_domain = true # for forward compatibility map.each { |key, value| case key when 'domain' self.domain = value # ditto else instance_variable_set(:"@#{key}", value) end } end |