Class: ProxyManager::Proxy
- Inherits:
-
Object
- Object
- ProxyManager::Proxy
- Defined in:
- lib/proxy_manager/proxy.rb
Instance Attribute Summary collapse
-
#list ⇒ Object
readonly
Returns the value of attribute list.
Class Method Summary collapse
Instance Method Summary collapse
- #get(count = 1) ⇒ Object
- #get!(count = 1) ⇒ Object
- #get_proxy(count, check_connection = false) ⇒ Object private
-
#initialize(proxies) ⇒ Proxy
constructor
A new instance of Proxy.
- #load_from_file(file) ⇒ Object private
- #load_list_from_array(proxies) ⇒ Object private
- #save_to_file(file, list) ⇒ Object private
Constructor Details
#initialize(proxies) ⇒ Proxy
Returns a new instance of Proxy.
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/proxy_manager/proxy.rb', line 5 def initialize(proxies) @list = [] if proxies.is_a? Array load_list_from_array(proxies) else @list_file = proxies @list = load_from_file(@list_file) end end |
Instance Attribute Details
#list ⇒ Object (readonly)
Returns the value of attribute list.
3 4 5 |
# File 'lib/proxy_manager/proxy.rb', line 3 def list @list end |
Class Method Details
.connectable?(proxy) ⇒ Boolean
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/proxy_manager/proxy.rb', line 24 def self.connectable?(proxy) proxy = proxy.chomp.split(':') if proxy.is_a? String ip, port = proxy connection = Net::HTTP.new("http://google.com", nil, ip, port) connection.open_timeout = 3 connection.read_timeout = 3 connection.start do |http| return true if http.get('/') end false rescue Exception => e false end |
Instance Method Details
#get(count = 1) ⇒ Object
16 17 18 |
# File 'lib/proxy_manager/proxy.rb', line 16 def get(count = 1) get_proxy(count) end |
#get!(count = 1) ⇒ Object
20 21 22 |
# File 'lib/proxy_manager/proxy.rb', line 20 def get!(count = 1) get_proxy(count, true) end |
#get_proxy(count, check_connection = false) ⇒ Object (private)
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/proxy_manager/proxy.rb', line 55 def get_proxy(count, check_connection = false) raise 'List is empty' if @list.empty? items = [] new_list = @list.clone @list.each_with_index do |proxy, key| new_list.shift if !check_connection || self.class.connectable?(proxy) new_list << proxy if count == 1 items = proxy break else items << proxy break if items.size == count end end end @list = new_list raise 'There are no available proxy' if items.empty? save_to_file(@list_file, @list) if @list_file items end |
#load_from_file(file) ⇒ Object (private)
46 47 48 49 50 51 52 53 |
# File 'lib/proxy_manager/proxy.rb', line 46 def load_from_file(file) result = [] IO.readlines(file).each do |line| ip, port = line.chomp.split(':') result << [ip, port.to_i] if ip.is_a?(String) && port.is_a?(String) end result end |
#load_list_from_array(proxies) ⇒ Object (private)
42 43 44 |
# File 'lib/proxy_manager/proxy.rb', line 42 def load_list_from_array(proxies) @list = proxies.map { |arg| [arg.split(':')[0], arg.split(':')[1].to_i] } end |
#save_to_file(file, list) ⇒ Object (private)
86 87 88 89 90 91 |
# File 'lib/proxy_manager/proxy.rb', line 86 def save_to_file(file, list) source = '' list.each { |p| source << "#{p[0]}:#{p[1]}\n" } IO.write(file, source.sub(/\n$/, '')) end |