Module: HttpUtilities::Proxies::ProxyModule::ClassMethods

Defined in:
lib/http_utilities/proxies/proxy_module.rb

Instance Method Summary collapse

Instance Method Details

#format_proxy_address(proxy_host, proxy_port = 80, include_http = false) ⇒ Object



47
48
49
50
51
# File 'lib/http_utilities/proxies/proxy_module.rb', line 47

def format_proxy_address(proxy_host, proxy_port = 80, include_http = false)
  proxy_address = "#{proxy_host}:#{proxy_port}"
  proxy_address.insert(0, "http://") if (include_http && !proxy_address.start_with?("http://"))
  return proxy_address
end

#format_proxy_credentials(username, password) ⇒ Object



53
54
55
# File 'lib/http_utilities/proxies/proxy_module.rb', line 53

def format_proxy_credentials(username, password)
  return "#{username}:#{password}"
end

#get_random_proxy(protocol = :all, proxy_type = :all) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/http_utilities/proxies/proxy_module.rb', line 20

def get_random_proxy(protocol = :all, proxy_type = :all)
  conditions = set_protocol_and_proxy_type_conditions(protocol, proxy_type)
  conditions << ActiveRecord::Base.send(:sanitize_sql_array, ["valid_proxy = ?", true])
  conditions << "last_checked_at IS NOT NULL"
  query = conditions.join(" AND ")
  
  order_clause = case ActiveRecord::Base.connection.class.name
    when "ActiveRecord::ConnectionAdapters::MysqlAdapter", "ActiveRecord::ConnectionAdapters::Mysql2Adapter" then "RAND() DESC"
    when "ActiveRecord::ConnectionAdapters::SQLite3Adapter" then "RANDOM() DESC"
  end
  
  proxy = nil
  
  uncached do
    proxy = where(query).order(order_clause).limit(1).first
  end

  return proxy
end

#set_protocol_and_proxy_type_conditions(protocol, proxy_type) ⇒ Object



40
41
42
43
44
45
# File 'lib/http_utilities/proxies/proxy_module.rb', line 40

def set_protocol_and_proxy_type_conditions(protocol, proxy_type)
  conditions = []
  conditions << ActiveRecord::Base.send(:sanitize_sql_array, ["protocol = ?", protocol]) if (protocol && !protocol.downcase.to_sym.eql?(:all))
  conditions << ActiveRecord::Base.send(:sanitize_sql_array, ["proxy_type = ?", proxy_type]) if (proxy_type && !proxy_type.downcase.to_sym.eql?(:all))
  return conditions
end

#should_be_checked(protocol = :all, proxy_type = :all, date = Time.now, limit = 10) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/http_utilities/proxies/proxy_module.rb', line 11

def should_be_checked(protocol = :all, proxy_type = :all, date = Time.now, limit = 10)
  conditions = set_protocol_and_proxy_type_conditions(protocol, proxy_type)
  conditions << ActiveRecord::Base.send(:sanitize_sql_array, ["(last_checked_at IS NULL OR last_checked_at < ?)", date])
  conditions << "failed_attempts <= 10"
  query = conditions.join(" AND ")
  
  where(query).order("valid_proxy ASC, failed_attempts ASC, last_checked_at ASC").limit(limit) 
end