Class: ProxyManager::Main
- Inherits:
-
Object
- Object
- ProxyManager::Main
- Defined in:
- lib/proxy_manager/main.rb
Instance Attribute Summary collapse
-
#bad_list ⇒ Object
readonly
Returns the value of attribute bad_list.
-
#bad_list_file ⇒ Object
readonly
Returns the value of attribute bad_list_file.
-
#list ⇒ Object
readonly
Returns the value of attribute list.
-
#list_file ⇒ Object
readonly
Returns the value of attribute list_file.
Instance Method Summary collapse
- #connectable?(proxy) ⇒ Boolean
- #get(count = 1) ⇒ Object
-
#initialize(proxies, bad_proxies) ⇒ Main
constructor
A new instance of Main.
- #load_bad_list_from_file(bad_proxies) ⇒ Object private
- #load_list_from_array(proxies) ⇒ Object private
- #load_list_from_file(proxies) ⇒ Object private
Constructor Details
#initialize(proxies, bad_proxies) ⇒ Main
Returns a new instance of Main.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/proxy_manager/main.rb', line 5 def initialize(proxies, bad_proxies) @list = [] @bad_list = [] if proxies.is_a? Array load_list_from_array(proxies) else if proxies.empty? or bad_proxies.empty? raise 'Both arguments "proxies" and "bad_proxies" required' end @list_file, @bad_list_file = proxies, bad_proxies load_list_from_file(proxies) load_bad_list_from_file(bad_proxies) end end |
Instance Attribute Details
#bad_list ⇒ Object (readonly)
Returns the value of attribute bad_list.
3 4 5 |
# File 'lib/proxy_manager/main.rb', line 3 def bad_list @bad_list end |
#bad_list_file ⇒ Object (readonly)
Returns the value of attribute bad_list_file.
3 4 5 |
# File 'lib/proxy_manager/main.rb', line 3 def bad_list_file @bad_list_file end |
#list ⇒ Object (readonly)
Returns the value of attribute list.
3 4 5 |
# File 'lib/proxy_manager/main.rb', line 3 def list @list end |
#list_file ⇒ Object (readonly)
Returns the value of attribute list_file.
3 4 5 |
# File 'lib/proxy_manager/main.rb', line 3 def list_file @list_file end |
Instance Method Details
#connectable?(proxy) ⇒ Boolean
23 24 25 26 |
# File 'lib/proxy_manager/main.rb', line 23 def connectable?(proxy) proxy = proxy.chomp.split(':') if proxy.is_a? String Net::Ping::TCP.new(proxy[0], proxy[1].to_i).ping end |
#get(count = 1) ⇒ Object
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 53 54 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 |
# File 'lib/proxy_manager/main.rb', line 28 def get(count = 1) raise 'List is empty' if @list.empty? items = [] new_list = @list.clone @list.each_with_index do |proxy, key| new_list.shift if connectable? proxy new_list << proxy if count == 1 items = proxy break else items << proxy break if items.size == count end else @bad_list << proxy end end @list = new_list raise 'There are no available proxy' if items.empty? if @list_file && @bad_list_file File.open(@list_file, "w+") do |f| source = '' @list.each_with_index do |p, index| source << "#{p[0]}:#{p[1]}" source << "\n" if @list[index + 1] end f.write(source) end File.open(@bad_list_file, "w+") do |f| source = '' @bad_list.each_with_index do |p, index| source << "#{p[0]}:#{p[1]}" source << "\n" if @bad_list[index + 1] end f.write(source) end end items end |
#load_bad_list_from_file(bad_proxies) ⇒ Object (private)
98 99 100 101 102 103 104 105 |
# File 'lib/proxy_manager/main.rb', line 98 def load_bad_list_from_file(bad_proxies) File.open(bad_proxies, "r").each do |line| line = line.chomp.split(':') if line[0].is_a? String and line[1].is_a? String @bad_list << [line[0], line[1].to_i] end end end |
#load_list_from_array(proxies) ⇒ Object (private)
85 86 87 |
# File 'lib/proxy_manager/main.rb', line 85 def load_list_from_array(proxies) @list = proxies.map { |arg| [arg.split(':')[0], arg.split(':')[1].to_i] } end |
#load_list_from_file(proxies) ⇒ Object (private)
89 90 91 92 93 94 95 96 |
# File 'lib/proxy_manager/main.rb', line 89 def load_list_from_file(proxies) File.open(proxies, "r").each do |line| line = line.chomp.split(':') if line[0].is_a? String and line[1].is_a? String @list << [line[0], line[1].to_i] end end end |