Class: ProxyFetcher::Client::ProxiesRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/proxy_fetcher/client/proxies_registry.rb

Overview

ProxyFetcher proxies registry for managing proxy lists used by the Client. It is used to fetch proxy lists and instantiate Manager object that will handle proxies.

Class Method Summary collapse

Class Method Details

.find_proxy_for(url) ⇒ ProxyFetcher::Proxy

Searches for valid proxy or required type (HTTP or secure) for requested URL. If no proxy found, than it refreshes proxy list and tries again.

Parameters:

  • url (String)

    URL to process with proxy

Returns:



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/proxy_fetcher/client/proxies_registry.rb', line 31

def find_proxy_for(url)
  proxy = if URI.parse(url).is_a?(URI::HTTPS)
            manager.proxies.detect(&:ssl?)
          else
            manager.get
          end

  return proxy unless proxy.nil?

  manager.refresh_list!
  find_proxy_for(url)
end

.invalidate_proxy!(proxy) ⇒ Object

Removes proxy from the list of the current proxy manager instance. If no more proxy available, refreshes the list.

Parameters:



16
17
18
19
# File 'lib/proxy_fetcher/client/proxies_registry.rb', line 16

def invalidate_proxy!(proxy)
  manager.proxies.delete(proxy)
  manager.refresh_list! if manager.proxies.empty?
end

.managerProxyFetcher::Manager

Instantiates or returns ProxyFetcher::Manager instance for current Thread.

Returns:



50
51
52
53
54
55
# File 'lib/proxy_fetcher/client/proxies_registry.rb', line 50

def manager
  manager = Thread.current[:proxy_fetcher_manager]
  return manager unless manager.nil?

  Thread.current[:proxy_fetcher_manager] = ProxyFetcher::Manager.new
end