class Spidr::SessionCache

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

Public Class Methods

new(options={}) click to toggle source

Creates a new session cache.

@param [Hash] options

Configuration options.

@option [Hash] :proxy (Spidr.proxy)

Proxy options.

@option [Integer] :open_timeout (Spidr.open_timeout)

Optional open timeout.

@option [Integer] :ssl_timeout (Spidr.ssl_timeout)

Optional ssl timeout.

@option [Integer] :read_timeout (Spidr.read_timeout)

Optional read timeout.

@option [Integer] :continue_timeout (Spidr.continue_timeout)

Optional `Continue` timeout.

@option [Integer] :keep_alive_timeout (Spidr.keep_alive_timeout)

Optional `Keep-Alive` timeout.

@since 0.6.0

# File lib/spidr/session_cache.rb, line 43
def initialize(options={})
  @proxy = options.fetch(:proxy,Spidr.proxy)

  @open_timeout       = options.fetch(:open_timeout,Spidr.open_timeout)
  @ssl_timeout        = options.fetch(:ssl_timeout,Spidr.ssl_timeout)
  @read_timeout       = options.fetch(:read_timeout,Spidr.read_timeout)
  @continue_timeout   = options.fetch(:continue_timeout,Spidr.continue_timeout)
  @keep_alive_timeout = options.fetch(:keep_alive_timeout,Spidr.keep_alive_timeout)

  @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 85
def [](url)
  # normalize the url
  url = URI(url)

  # session key
  key = key_for(url)

  unless @sessions[key]
    session = Net::HTTP::Proxy(
      @proxy.host,
      @proxy.port,
      @proxy.user,
      @proxy.password
    ).new(url.host,url.port)

    session.open_timeout       = @open_timeout       if @open_timeout
    session.read_timeout       = @read_timeout       if @read_timeout
    session.continue_timeout   = @continue_timeout   if @continue_timeout
    session.keep_alive_timeout = @keep_alive_timeout if @keep_alive_timeout

    if url.scheme == 'https'
      session.use_ssl     = true
      session.verify_mode = OpenSSL::SSL::VERIFY_NONE
      session.ssl_timeout = @ssl_timeout
      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 66
def active?(url)
  # normalize the url
  url = URI(url)

  # session key
  key = key_for(url)

  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 153
def clear
  @sessions.each_value do |session|
    begin
      session.finish
    rescue IOError
    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 128
def kill!(url)
  # normalize the url
  url = URI(url)

  # session key
  key = key_for(url)

  if (sess = @sessions[key])
    begin 
      sess.finish
    rescue IOError
    end

    @sessions.delete(key)
  end
end

Private Instance Methods

key_for(url) click to toggle source

Creates a session key based on the URL.

@param [URI::HTTP] url

The given URL.

@return [Array]

The session key containing the scheme, host and port.
# File lib/spidr/session_cache.rb, line 176
def key_for(url)
  [url.scheme, url.host, url.port]
end