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



82
83
84
85
86
87
88
89
90
# File 'lib/http_utilities/http/proxy_support.rb', line 82

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)


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

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



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

def set_proxy_credentials(proxy_username, proxy_password, proxy_credentials)
  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

#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
50
51
52
# 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_host                =   options.fetch(:proxy_host, nil)
  proxy_port                =   options.fetch(:proxy_port, nil)
  proxy_username            =   options.fetch(:proxy_username, nil)
  proxy_password            =   options.fetch(:proxy_password, nil)
  proxy_credentials         =   options.fetch(:proxy_credentials, nil)
  proxy_type                =   options.fetch(:proxy_type, :all)
  proxy_protocol            =   options.fetch(:proxy_protocol, :all)
  
  if use_proxy || specific_proxy
    self.proxy[:protocol]   =   proxy_protocol
    self.proxy[:type]       =   proxy_type
    
    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 proxy_host && proxy_port
      self.proxy[:host]     =   proxy_host
      self.proxy[:port]     =   proxy_port

    elsif proxy_model_defined?
      if specific_proxy && specific_proxy.is_a?(::Proxy)
        proxy_object        =   specific_proxy
      else
        proxy_object        =   ::Proxy.get_random_proxy(protocol: self.proxy[:protocol], proxy_type: self.proxy[:type])
      end
      
      #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