Class: Spidr::SessionCache

Inherits:
Object
  • Object
show all
Defined in:
lib/spidr/session_cache.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proxy = Spidr.proxy) ⇒ SessionCache

Creates a new session cache.

Parameters:

  • proxy (Hash) (defaults to: Spidr.proxy)

    (Spidr.proxy) Proxy options.

Options Hash (proxy):

  • :host (String)

    The host the proxy is running on.

  • :port (Integer)

    The port the proxy is running on.

  • :user (String)

    The user to authenticate as with the proxy.

  • :password (String)

    The password to authenticate with.

Since:

  • 0.2.2



34
35
36
37
# File 'lib/spidr/session_cache.rb', line 34

def initialize(proxy=Spidr.proxy)
  @proxy = proxy
  @sessions = {}
end

Instance Attribute Details

#proxyObject

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.

Parameters:

  • url (URI::HTTP, String)

    The URL which will be requested later.

Returns:

  • (Net::HTTP)

    The active HTTP session object.



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.

Parameters:

  • url (URI::HTTP, String)

    The URL that represents a session.

Returns:

  • (Boolean)

    Specifies whether there is an active HTTP session.

Since:

  • 0.2.3



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

#clearSessionCache

Clears the session cache.

Returns:

Since:

  • 0.2.2



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.

Parameters:

  • url (URI::HTTP, String)

    The URL of the requested session.

Returns:

  • (nil)

Since:

  • 0.2.2



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