Module: HttpUtilities::Http::ProxySupport

Included in:
Mechanize::Client, Request
Defined in:
lib/http_utilities/http/proxy_support.rb

Instance Method Summary collapse

Instance Method Details

#generate_proxy_optionsObject



85
86
87
88
89
90
91
92
93
# File 'lib/http_utilities/http/proxy_support.rb', line 85

def generate_proxy_options
  proxy_options             =   {}
  
  proxy_options[:uri]       =   "http://#{self.proxy[:host]}:#{self.proxy[:port]}"
  proxy_options[:user]      =   self.proxy[:username] if self.proxy[:username] && self.proxy[:username].present?
  proxy_options[:password]  =   self.proxy[:password] if self.proxy[:password] && self.proxy[:password].present?
  
  return proxy_options
end

#proxy_model_defined?Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
# File 'lib/http_utilities/http/proxy_support.rb', line 74

def proxy_model_defined?
  defined = Module.const_get("Proxy").is_a?(Class) rescue false
  defined = (defined && Proxy.respond_to?(:get_random_proxy))
  
  return defined
end

#set_proxy_credentials(proxy_username, proxy_password, proxy_credentials) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/http_utilities/http/proxy_support.rb', line 51

def set_proxy_credentials(proxy_username, proxy_password, proxy_credentials)
  if (self.using_proxy? && (!self.proxy[:username] || !self.proxy[:password]))
    if (proxy_username && proxy_password)
      self.proxy[:username]       =   proxy_username
      self.proxy[:password]       =   proxy_password

    elsif (proxy_credentials)
      if (proxy_credentials.is_a?(Hash))
        self.proxy[:username]     =   proxy_credentials[:username]
        self.proxy[:password]     =   proxy_credentials[:password]

      elsif (proxy_credentials.is_a?(String))
        parts = proxy_credentials.split(":")

        if (parts && parts.any? && parts.size >= 2)
          self.proxy[:username]   =   parts.first
          self.proxy[:password]   =   parts.second
        end
      end
    end
  end
end

#set_proxy_options(options = {}) ⇒ Object



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
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/http_utilities/http/proxy_support.rb', line 5

def set_proxy_options(options = {})
  use_proxy                 =   options.fetch(:use_proxy, false)
  specific_proxy            =   options.fetch(:proxy, nil)
  proxy_username            =   options.fetch(:proxy_username, nil)
  proxy_password            =   options.fetch(:proxy_password, nil)
  proxy_credentials         =   options.fetch(:proxy_credentials, nil)
  reset_proxy               =   options.fetch(:reset_proxy, true)

  if reset_proxy
    self.proxy              =   {}
    self.proxy[:host]       =   options.fetch(:proxy_host, nil)
    self.proxy[:port]       =   options.fetch(:proxy_port, nil)
    self.proxy[:protocol]   =   options.fetch(:proxy_protocol, :http)
    self.proxy[:type]       =   options.fetch(:proxy_type, :all)
  end
  
  if (use_proxy || (specific_proxy && !self.using_proxy?))
    if (specific_proxy && specific_proxy.is_a?(String))
      specific_proxy        =   specific_proxy.gsub(/^http(s)?:\/\//i, "")
      parts                 =   specific_proxy.split(":")

      if (parts.size.eql?(2))
        self.proxy[:host]   =   parts.first
        self.proxy[:port]   =   parts.second.to_i
      end

    elsif (specific_proxy && specific_proxy.is_a?(Hash) && !specific_proxy.empty? && specific_proxy[:host] && specific_proxy[:port])
      self.proxy            =   specific_proxy

    elsif (proxy_model_defined?)
      proxy_object          =   Proxy.get_random_proxy(protocol: self.proxy[:protocol], proxy_type: self.proxy[:type])
      
      #log(:info, "[HttpUtilities::Http::ProxySupport] - Randomized Proxy object: #{proxy_object.inspect}")

      if (proxy_object)
        self.proxy[:host]   =   proxy_object.host
        self.proxy[:port]   =   proxy_object.port
        proxy_username      =   proxy_object.username.present? ? proxy_object.username : nil
        proxy_password      =   proxy_object.password.present? ? proxy_object.password : nil
      end
    end
  end

  set_proxy_credentials(proxy_username, proxy_password, proxy_credentials)
end

#using_proxy?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/http_utilities/http/proxy_support.rb', line 81

def using_proxy?
  return (self.proxy[:host] && self.proxy[:port] && self.proxy[:port] > 0)
end