Sha256: 566739f03314c91a2be8524d3a22dcb903f15caaaefd223faf4d36e62e034cda

Contents?: true

Size: 1.77 KB

Versions: 18

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

module ProxyFetcher
  module Client
    # 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 ProxiesRegistry
      class << self
        # Removes proxy from the list of the current proxy manager
        # instance. If no more proxy available, refreshes the list.
        #
        # @param proxy [ProxyFetcher::Proxy]
        #   proxy object to remove
        #
        def invalidate_proxy!(proxy)
          manager.proxies.delete(proxy)
          manager.refresh_list! if manager.proxies.empty?
        end

        # 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.
        #
        # @param url [String]
        #   URL to process with proxy
        #
        # @return [ProxyFetcher::Proxy]
        #   gems proxy object
        #
        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

        # Instantiates or returns <code>ProxyFetcher::Manager</code> instance
        # for current <code>Thread</code>.
        #
        # @return [ProxyFetcher::Manager]
        #   ProxyFetcher manager class
        #
        def manager
          manager = Thread.current[:proxy_fetcher_manager]
          return manager unless manager.nil?

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

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
proxy_fetcher-0.17.0 lib/proxy_fetcher/client/proxies_registry.rb
proxy_fetcher-0.16.0 lib/proxy_fetcher/client/proxies_registry.rb
proxy_fetcher-0.15.1 lib/proxy_fetcher/client/proxies_registry.rb
proxy_fetcher-0.15.0 lib/proxy_fetcher/client/proxies_registry.rb
proxy_fetcher-0.14.0 lib/proxy_fetcher/client/proxies_registry.rb
proxy_fetcher-0.13.0 lib/proxy_fetcher/client/proxies_registry.rb
proxy_fetcher-0.12.0 lib/proxy_fetcher/client/proxies_registry.rb
proxy_fetcher-0.11.0 lib/proxy_fetcher/client/proxies_registry.rb
proxy_fetcher-0.10.2 lib/proxy_fetcher/client/proxies_registry.rb
proxy_fetcher-0.10.1 lib/proxy_fetcher/client/proxies_registry.rb
proxy_fetcher-0.10.0 lib/proxy_fetcher/client/proxies_registry.rb
proxy_fetcher-0.9.0 lib/proxy_fetcher/client/proxies_registry.rb
proxy_fetcher-0.8.0 lib/proxy_fetcher/client/proxies_registry.rb
proxy_fetcher-0.7.1 lib/proxy_fetcher/client/proxies_registry.rb
proxy_fetcher-0.7.0 lib/proxy_fetcher/client/proxies_registry.rb
proxy_fetcher-0.6.5 lib/proxy_fetcher/client/proxies_registry.rb
proxy_fetcher-0.6.4 lib/proxy_fetcher/client/proxies_registry.rb
proxy_fetcher-0.6.3 lib/proxy_fetcher/client/proxies_registry.rb