Parent

Mechanize::Cookie

This class is used to represent an HTTP Cookie.

Attributes

name[R]
value[RW]
version[RW]
domain[RW]
path[RW]
secure[RW]
comment[RW]
max_age[RW]
session[RW]
created_at[RW]
accessed_at[RW]
for_domain[RW]

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.

Public Class Methods

new(name, value) new(name, value, attr_hash) new(attr_hash) click to toggle source

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')
    # File lib/mechanize/cookie.rb, line 32
32:   def initialize(*args)
33:     @version = 0     # Netscape Cookie
34: 
35:     @domain = @path = @secure = @comment = @max_age =
36:       @expires = @comment_url = @discard = @port = nil
37: 
38:     @created_at = @accessed_at = Time.now
39:     case args.size
40:     when 2
41:       @name, @value = *args
42:       @for_domain = false
43:       return
44:     when 3
45:       @name, @value, attr_hash = *args
46:     when 1
47:       attr_hash = args.first
48:     else
49:       raise ArgumentError, "wrong number of arguments (#{args.size} for 1-3)"
50:     end
51:     for_domain = false
52:     attr_hash.each_pair { |key, val|
53:       skey = key.to_s.downcase
54:       skey.sub!(/[!?]\z/, '')
55:       case skey
56:       when 'for_domain'
57:         for_domain = !!val
58:       when 'name'
59:         @name = val
60:       when 'value'
61:         @value = val
62:       else
63:         setter = :"#{skey}="
64:         send(setter, val) if respond_to?(setter)
65:       end
66:     }
67:     @for_domain = for_domain
68:   end
parse(uri, str, log = Mechanize.log) click to toggle source

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.

     # File lib/mechanize/cookie.rb, line 80
 80:     def parse(uri, str, log = Mechanize.log)
 81:       return str.split(/,(?=[^;,]*=)|,$/).map { |c|
 82:         cookie_elem = c.split(/;+/)
 83:         first_elem = cookie_elem.shift
 84:         first_elem.strip!
 85:         key, value = first_elem.split(/\=/, 2)
 86: 
 87:         begin
 88:           cookie = new(key, value.dup)
 89:         rescue
 90:           log.warn("Couldn't parse key/value: #{first_elem}") if log
 91:           next
 92:         end
 93: 
 94:         cookie_elem.each do |pair|
 95:           pair.strip!
 96:           key, value = pair.split(/=/, 2)
 97:           next unless key
 98:           value = WEBrick::HTTPUtils.dequote(value.strip) if value
 99: 
100:           case key.downcase
101:           when 'domain'
102:             next unless value && !value.empty?
103:             begin
104:               cookie.domain = value
105:               cookie.for_domain = true
106:             rescue
107:               log.warn("Couldn't parse domain: #{value}") if log
108:             end
109:           when 'path'
110:             next unless value && !value.empty?
111:             cookie.path = value
112:           when 'expires'
113:             next unless value && !value.empty?
114:             begin
115:               cookie.expires = Time::parse(value)
116:             rescue
117:               log.warn("Couldn't parse expires: #{value}") if log
118:             end
119:           when 'max-age'
120:             next unless value && !value.empty?
121:             begin
122:               cookie.max_age = Integer(value)
123:             rescue
124:               log.warn("Couldn't parse max age '#{value}'") if log
125:             end
126:           when 'comment'
127:             next unless value
128:             cookie.comment = value
129:           when 'version'
130:             next unless value
131:             begin
132:               cookie.version = Integer(value)
133:             rescue
134:               log.warn("Couldn't parse version '#{value}'") if log
135:               cookie.version = nil
136:             end
137:           when 'secure'
138:             cookie.secure = true
139:           end
140:         end
141: 
142:         cookie.path    ||= (uri + './').path
143:         cookie.secure  ||= false
144:         cookie.domain  ||= uri.host
145: 
146:         # RFC 6265 4.1.2.2
147:         cookie.expires   = Time.now + cookie.max_age if cookie.max_age
148:         cookie.session   = !cookie.expires
149: 
150:         # Move this in to the cookie jar
151:         yield cookie if block_given?
152: 
153:         cookie
154:       }
155:     end

Public Instance Methods

acceptable_from_uri?(uri) click to toggle source
     # File lib/mechanize/cookie.rb, line 197
197:   def acceptable_from_uri?(uri)
198:     host = DomainName.new(uri.host)
199: 
200:     # RFC 6265 5.3
201:     # When the user agent "receives a cookie":
202:     return host.hostname == domain unless @for_domain
203: 
204:     if host.cookie_domain?(@domain_name)
205:       true
206:     elsif host.hostname == domain
207:       @for_domain = false
208:       true
209:     else
210:       false
211:     end
212:   end
domain=(domain) click to toggle source

Sets the domain attribute. A leading dot in domain implies turning the for_domain? flag on.

     # File lib/mechanize/cookie.rb, line 162
162:   def domain=(domain)
163:     if DomainName === domain
164:       @domain_name = domain
165:     else
166:       domain.is_a?(String) or
167:         (domain.respond_to?(:to_str) && (domain = domain.to_str).is_a?(String)) or
168:         raise TypeError, "#{domain.class} is not a String"
169:       if domain.start_with?('.')
170:         @for_domain = true
171:         domain = domain[1..1]
172:       end
173:       # Do we really need to support this?
174:       if domain.match(/\A([^:]+):[0-9]+\z/)
175:         domain = $1
176:       end
177:       @domain_name = DomainName.new(domain)
178:     end
179:     set_domain(@domain_name.hostname)
180:   end
Also aliased as: set_domain
expired?() click to toggle source
     # File lib/mechanize/cookie.rb, line 190
190:   def expired?
191:     return false unless expires
192:     Time.now > expires
193:   end
expires() click to toggle source
     # File lib/mechanize/cookie.rb, line 186
186:   def expires
187:     @expires && Time.parse(@expires)
188:   end
expires=(t) click to toggle source
     # File lib/mechanize/cookie.rb, line 182
182:   def expires=(t)
183:     @expires = t && (t.is_a?(Time) ? t.httpdate : t.to_s)
184:   end
init_with(coder) click to toggle source
     # File lib/mechanize/cookie.rb, line 223
223:   def init_with(coder)
224:     yaml_initialize(coder.tag, coder.map)
225:   end
set_domain(domain) click to toggle source
Alias for: domain=
to_s() click to toggle source
     # File lib/mechanize/cookie.rb, line 219
219:   def to_s
220:     "#{@name}=#{@value}"
221:   end
valid_for_uri?(uri) click to toggle source
     # File lib/mechanize/cookie.rb, line 214
214:   def valid_for_uri?(uri)
215:     return false if secure? && uri.scheme != 'https'
216:     acceptable_from_uri?(uri) && uri.path.start_with?(path)
217:   end
yaml_initialize(tag, map) click to toggle source
     # File lib/mechanize/cookie.rb, line 227
227:   def yaml_initialize(tag, map)
228:     @for_domain = true    # for forward compatibility
229:     map.each { |key, value|
230:       case key
231:       when 'domain'
232:         self.domain = value # ditto
233:       else
234:         instance_variable_set(:"@#{key}", value)
235:       end
236:     }
237:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.