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

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

Instance Method Summary collapse

Instance Method Details

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



64
65
66
67
68
# File 'lib/http_utilities/proxies/sql/proxy_module.rb', line 64

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



70
71
72
# File 'lib/http_utilities/proxies/sql/proxy_module.rb', line 70

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

#get_proxies_for_protocol_and_proxy_type(protocol, proxy_type) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/http_utilities/proxies/sql/proxy_module.rb', line 56

def get_proxies_for_protocol_and_proxy_type(protocol, proxy_type)
  proxies     =   ::Proxy.where(nil)
  proxies     =   proxies.where(protocol: protocol)     if (protocol && !protocol.downcase.to_sym.eql?(:all))
  proxies     =   proxies.where(proxy_type: proxy_type) if (proxy_type && !proxy_type.downcase.to_sym.eql?(:all))

  return proxies
end

#get_random_proxy(protocol: :all, proxy_type: :all, maximum_failed_attempts: nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/http_utilities/proxies/sql/proxy_module.rb', line 31

def get_random_proxy(protocol: :all, proxy_type: :all, maximum_failed_attempts: nil)
  proxies     =   get_valid_proxies(protocol: protocol, proxy_type: proxy_type, maximum_failed_attempts: maximum_failed_attempts)

  order_clause = case ActiveRecord::Base.connection.class.name
    when "ActiveRecord::ConnectionAdapters::MysqlAdapter", "ActiveRecord::ConnectionAdapters::Mysql2Adapter"
      "RAND() DESC"
    when "ActiveRecord::ConnectionAdapters::PostgreSQLAdapter"
      "RANDOM() DESC"
    when "ActiveRecord::ConnectionAdapters::SQLite3Adapter"
      "RANDOM() DESC"
    else
      "RAND() DESC"
  end

  proxies     =   proxies.order(order_clause)

  proxy       =   nil

  uncached do
    proxy     =   proxies.limit(1).first
  end

  return proxy
end

#get_valid_proxies(protocol: :all, proxy_type: :all, maximum_failed_attempts: nil) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/http_utilities/proxies/sql/proxy_module.rb', line 23

def get_valid_proxies(protocol: :all, proxy_type: :all, maximum_failed_attempts: nil)
  proxies     =   get_proxies_for_protocol_and_proxy_type(protocol, proxy_type)
  proxies     =   proxies.where(["valid_proxy = ? AND last_checked_at IS NOT NULL", true])
  proxies     =   proxies.where(["failed_attempts <= ?", maximum_failed_attempts]) if maximum_failed_attempts

  return proxies
end

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



13
14
15
16
17
18
19
20
21
# File 'lib/http_utilities/proxies/sql/proxy_module.rb', line 13

def should_be_checked(protocol: :all, proxy_type: :all, date: Time.now, limit: 10, maximum_failed_attempts: 10)
  proxies     =   get_proxies_for_protocol_and_proxy_type(protocol, proxy_type)
  proxies     =   proxies.where(["(last_checked_at IS NULL OR last_checked_at < ?)", date])
  proxies     =   proxies.where(["failed_attempts <= ?", maximum_failed_attempts])
  proxies     =   proxies.order("valid_proxy ASC, failed_attempts ASC, last_checked_at ASC")
  proxies     =   proxies.limit(limit)

  return proxies
end