lib/spidr/cookie_jar.rb in spidr-0.2.6 vs lib/spidr/cookie_jar.rb in spidr-0.2.7
- old
+ new
@@ -120,34 +120,51 @@
#
def for_host(host)
if @dirty.include?(host)
values = []
- @params[host].each do |name,value|
+ cookies_for_host(host).each do |name,value|
values << "#{name}=#{value}"
end
@cookies[host] = values.join('; ')
@dirty.delete(host)
end
- hdomain = host.split('.')
+ return @cookies[host]
+ end
- if hdomain.length > 2
- parent_cookies = for_host(hdomain[1..-1].join('.'))
+ #
+ # Returns raw cookie value pairs for a given host. Includes cookies set on
+ # parent domain(s).
+ #
+ # @param [String] host
+ # The name of the host.
+ #
+ # @return [Hash{String => String}]
+ # Cookie params.
+ #
+ # @since 0.2.7
+ #
+ def cookies_for_host(host)
+ host_cookies = (@params[host] || {})
+ sub_domains = host.split('.')
- unless (parent_cookies.nil? || parent_cookies.empty?)
- @cookies[host] = if @cookies[host].nil?
- # inherit the parent cookies
- parent_cookies
- else
- # merge the parent cookies with any host-specific cookies
- "#{parent_cookies}; #{@cookies[host]}"
- end
+ while sub_domains.length > 2
+ sub_domains.shift
+
+ if (parent_cookies = @params[sub_domains.join('.')])
+ parent_cookies.each do |name,value|
+ # copy in the parent cookies, only if they haven't been
+ # overridden yet.
+ unless host_cookies.has_key?(name)
+ host_cookies[name] = value
+ end
+ end
end
end
- return @cookies[host]
+ return host_cookies
end
#
# Clear out the jar, removing all stored cookies.
#