module ProxyRotator
Constants
- VERSION
Public Class Methods
base_url(config={})
click to toggle source
# File lib/proxy_rotator/rotator.rb, line 51 def self.base_url(config={}) url = ProxyRotator.configuration.base_url + "?apiKey=" + ProxyRotator.configuration.api_key url = url + "&" + config.to_a.map { |x| "#{x[0]}=#{x[1]}" }.join("&") unless config.empty? return url end
configuration()
click to toggle source
# File lib/proxy_rotator/configuration.rb, line 48 def self.configuration @configuration ||= initialize_configuration! end
configure() { |configuration| ... }
click to toggle source
# File lib/proxy_rotator/configuration.rb, line 52 def self.configure yield(configuration) end
describe_remote(config={})
click to toggle source
# File lib/proxy_rotator/rotator.rb, line 68 def self.describe_remote(config={}) url = self.base_url(config) self.get_proxyrotator_proxy(url) end
get_proxyrotator_proxy(url)
click to toggle source
# File lib/proxy_rotator/rotator.rb, line 73 def self.get_proxyrotator_proxy(url) response = RestClient.get(url) JSON.parse(response.body) end
initialize_configuration!()
click to toggle source
# File lib/proxy_rotator/configuration.rb, line 56 def self.initialize_configuration! @configuration = Configuration.new end
load_file(filepath)
click to toggle source
# File lib/proxy_rotator/rotator.rb, line 9 def self.load_file(filepath) @start_index=0 File.open(filepath, "r") do |f| f.each_line do |line| unless validate_url(line).nil? || @proxies.include?(line) @proxies << line.strip end end end end
remote_first_then_rotate(check_first=false, config={})
click to toggle source
# File lib/proxy_rotator/rotator.rb, line 85 def self.remote_first_then_rotate(check_first=false, config={}) result = self.rotate_remote(config) result = self.rotate(check_first) unless !result.nil? result end
reset_rotate(check_first=false)
click to toggle source
# File lib/proxy_rotator/rotator.rb, line 20 def self.reset_rotate(check_first=false) @start_index = 0 self.rotate(check_first) end
rotate(check_first=false)
click to toggle source
# File lib/proxy_rotator/rotator.rb, line 25 def self.rotate(check_first=false) return nil unless @proxies.count > 0 proxy = @proxies[@start_index] @start_index=@start_index+1 if check_first begin RestClient::Request.new( :method => :get, :url => ProxyRotator.configuration.default_test_url, :proxy => proxy, :timeout => ProxyRotator.configuration.default_timeout ).execute return proxy rescue Exception => e p "Proxy #{proxy} failed : #{e.message}" @failed_proxies << proxy unless @failed_proxies.include? proxy return nil unless @proxies.count > 1 || @proxies.count == @failed_proxies.count self.rotate(true) end else return proxy end end
rotate_first_then_remote(check_first=false, config={})
click to toggle source
# File lib/proxy_rotator/rotator.rb, line 78 def self.rotate_first_then_remote(check_first=false, config={}) result = self.rotate(check_first) result = self.rotate_remote(config) unless !result.nil? result end
rotate_remote(config={})
click to toggle source
# File lib/proxy_rotator/rotator.rb, line 58 def self.rotate_remote(config={}) url = self.base_url(config) json = self.get_proxyrotator_proxy(url) proxy = json["proxy"] return "http://#{proxy}" unless proxy.nil? return nil end
validate_url(url)
click to toggle source
# File lib/proxy_rotator/rotator.rb, line 92 def self.validate_url(url) url =~ URI::regexp(%w(http https)) end