lib/spidr/session_cache.rb in spidr-0.2.2 vs lib/spidr/session_cache.rb in spidr-0.2.3
- old
+ new
@@ -1,10 +1,13 @@
require 'spidr/spidr'
require 'net/http'
module Spidr
+ #
+ # Stores active HTTP Sessions organized by scheme, host-name and port.
+ #
class SessionCache
# Proxy to use
attr_accessor :proxy
@@ -32,10 +35,31 @@
@proxy = proxy
@sessions = {}
end
#
+ # Determines if there is an active HTTP session for a given URL.
+ #
+ # @param [URI::HTTP, String] url
+ # The URL that represents a session.
+ #
+ # @return [Boolean]
+ # Specifies whether there is an active HTTP session.
+ #
+ # @since 0.2.3
+ #
+ def active?(url)
+ # normalize the url
+ url = URI(url.to_s) unless url.kind_of?(URI)
+
+ # session key
+ key = [url.scheme, url.host, url.port]
+
+ return @sessions.has_key?(key)
+ end
+
+ #
# Provides an active HTTP session for a given URL.
#
# @param [URI::HTTP, String] url
# The URL which will be requested later.
#
@@ -44,10 +68,11 @@
#
def [](url)
# normalize the url
url = URI(url.to_s) unless url.kind_of?(URI)
+ # session key
key = [url.scheme, url.host, url.port]
unless @sessions[key]
session = Net::HTTP::Proxy(
@proxy[:host],
@@ -79,13 +104,13 @@
#
def kill!(url)
# normalize the url
url = URI(url.to_s) unless url.kind_of?(URI)
+ # session key
key = [url.scheme, url.host, url.port]
if (sess = @sessions[key])
-
begin
sess.finish
rescue IOError
end