class ProxyService

Constants

VERSION

Attributes

failure_codes[RW]
failure_limit[RW]
password[RW]
proxies_enabled[RW]
username[RW]
failure_codes[RW]
failure_limit[RW]
proxies_enabled[W]
proxy[R]
source[RW]

Public Class Methods

configure() { |self| ... } click to toggle source
# File lib/proxy_service.rb, line 8
def configure
  yield self
end
new(source, options = {}) click to toggle source

Create a new proxy service with for a specific ODS

@param [String, Symbol] source name of the ODS (e.g. :trip_advisor), will look for a queue with that name “proxy/#{source}” @param [Hash] options @option options [Boolean] :proxies_enabled override the class configuration @option options [Integer] :failure_limit before blocking the proxy @option options [Array] :failure_codes that indicate a proxy was blocked by the site

# File lib/proxy_service.rb, line 24
def initialize(source, options = {})
  @source = source
  @proxies_enabled = options.fetch(:proxies_enabled, !!self.class.proxies_enabled)
  @failure_limit = options.fetch(:failure_limit, self.class.failure_limit || 3)
  @failure_codes = options.fetch(:failure_codes, self.class.failure_codes || %w[403])
end

Public Instance Methods

block_or_increment_proxy() click to toggle source

Private

# File lib/proxy_service.rb, line 50
def block_or_increment_proxy
  if proxy.failures >= failure_limit
    proxy.blocked!
  else
    proxy.increment_failures
  end
end
new_worker() click to toggle source
# File lib/proxy_service.rb, line 77
def new_worker
  if proxies_enabled?
    Worker.new("proxy/#{source}")
  else
    NullWorker.new
  end
end
proxies_enabled?() click to toggle source
# File lib/proxy_service.rb, line 58
def proxies_enabled?
  @proxies_enabled
end
reserve_proxy() click to toggle source

Sleeps until the worker receives a proxy (message)

@return [Proxy] a new proxy object

# File lib/proxy_service.rb, line 65
def reserve_proxy
  worker = new_worker
  worker.subscribe
  loop do
    if worker.ready?
      return Proxy.new(worker)
    else
      sleep(1)
    end
  end
end
with_mechanize() { |agent| ... } click to toggle source

@yield [agent] Passes a [proxied] Mechanize agent to the block

# File lib/proxy_service.rb, line 32
def with_mechanize
  @proxy = reserve_proxy
  agent = MechanizeAgent.new
  agent.set_proxy(proxy)
  yield agent
  proxy.reset_failures
rescue Mechanize::ResponseCodeError => e
  block_or_increment_proxy if failure_codes.include?(e.response_code)
rescue ProxyBlockedError, ReCaptchaError
  block_or_increment_proxy
ensure
  proxy.release
end