class Spidr::SessionCache

Stores active HTTP Sessions organized by scheme, host-name and port.

Attributes

proxy[RW]

Proxy to use

Public Class Methods

new(proxy=Spidr.proxy) click to toggle source

Creates a new session cache.

@param [Hash] proxy (Spidr.proxy)

Proxy options.

@option proxy [String] :host

The host the proxy is running on.

@option proxy [Integer] :port

The port the proxy is running on.

@option proxy [String] :user

The user to authenticate as with the proxy.

@option proxy [String] :password

The password to authenticate with.

@since 0.2.2

# File lib/spidr/session_cache.rb, line 34
def initialize(proxy=Spidr.proxy)
  @proxy    = proxy
  @sessions = {}
end

Public Instance Methods

[](url) click to toggle source

Provides an active HTTP session for a given URL.

@param [URI::HTTP, String] url

The URL which will be requested later.

@return [Net::HTTP]

The active HTTP session object.
# 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) click to toggle source

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

# 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() click to toggle source

Clears the session cache.

@return [SessionCache]

The cleared session cache.

@since 0.2.2

# 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) click to toggle source

Destroys an HTTP session for the given scheme, host and port.

@param [URI::HTTP, String] url

The URL of the requested session.

@return [nil]

@since 0.2.2

# 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