Class: Spidr::SessionCache
- Inherits:
-
Object
- Object
- Spidr::SessionCache
- Defined in:
- lib/spidr/session_cache.rb
Overview
Stores active HTTP Sessions organized by scheme, host-name and port.
Instance Attribute Summary collapse
-
#proxy ⇒ Object
Proxy to use.
Instance Method Summary collapse
-
#[](url) ⇒ Net::HTTP
Provides an active HTTP session for a given URL.
-
#active?(url) ⇒ Boolean
Determines if there is an active HTTP session for a given URL.
-
#clear ⇒ SessionCache
Clears the session cache.
-
#initialize(proxy = Spidr.proxy) ⇒ SessionCache
constructor
Creates a new session cache.
-
#kill!(url) ⇒ nil
Destroys an HTTP session for the given scheme, host and port.
Constructor Details
#initialize(proxy = Spidr.proxy) ⇒ SessionCache
Creates a new session cache.
34 35 36 37 |
# File 'lib/spidr/session_cache.rb', line 34 def initialize(proxy=Spidr.proxy) @proxy = proxy @sessions = {} end |
Instance Attribute Details
#proxy ⇒ Object
Proxy to use
12 13 14 |
# File 'lib/spidr/session_cache.rb', line 12 def proxy @proxy end |
Instance Method Details
#[](url) ⇒ Net::HTTP
Provides an active HTTP session for a given URL.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/spidr/session_cache.rb', line 69 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], @proxy[:port], @proxy[:user], @proxy[:password] ).new(url.host,url.port) if url.scheme == 'https' session.use_ssl = true session.verify_mode = OpenSSL::SSL::VERIFY_NONE session.start end @sessions[key] = session end return @sessions[key] end |
#active?(url) ⇒ Boolean
Determines if there is an active HTTP session for a given URL.
50 51 52 53 54 55 56 57 58 |
# File 'lib/spidr/session_cache.rb', line 50 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 |
#clear ⇒ SessionCache
Clears the session cache.
131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/spidr/session_cache.rb', line 131 def clear @sessions.each_value do |sess| begin sess.finish rescue IOError nil end end @sessions.clear return self end |
#kill!(url) ⇒ nil
Destroys an HTTP session for the given scheme, host and port.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/spidr/session_cache.rb', line 106 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 @sessions.delete(key) end end |