Module: ProxyPoke

Defined in:
lib/proxypoke.rb

Class Method Summary collapse

Class Method Details

.poke(url, proxy_host, proxy_port, read_timeout, search_string) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/proxypoke.rb', line 4

def self.poke(url, proxy_host, proxy_port, read_timeout, search_string)
  uri = URI(url)

  client = Net::HTTP.new(
    address = uri.host,
    port = uri.port,
    p_addr = proxy_host,
    p_port = proxy_port,
  )

  client.use_ssl = uri.scheme == "https" ? true : false
  client.read_timeout = read_timeout
  
  begin
    response = client.get(url)
  rescue Net::ReadTimeout
    result, message = "Error", "connection timed out after #{client.read_timeout} seconds"
  rescue Errno::ECONNREFUSED
    result, message = "Error", "could not connect to proxy #{proxy_host} on port #{proxy_port}"
  else
    response_body = response.read_body

    if search_string == ""
      result, message = "Success", "#{proxy_host}:#{proxy_port} is working"
    elsif response_body.include? search_string
      result, message = "Success", "#{proxy_host}:#{proxy_port} is working and the search string was found"
    else
      result, message = "Error", "#{proxy_host}:#{proxy_port} is working but the search string was not found"
    end

  end

  return result, message
end