Class: ProxyManager::Main
- Inherits:
-
Object
- Object
- ProxyManager::Main
- Defined in:
- lib/proxy_manager/main.rb
Instance Attribute Summary (collapse)
-
- (Object) bad_list
readonly
Returns the value of attribute bad_list.
-
- (Object) bad_list_file
readonly
Returns the value of attribute bad_list_file.
-
- (Object) list
readonly
Returns the value of attribute list.
-
- (Object) list_file
readonly
Returns the value of attribute list_file.
Instance Method Summary (collapse)
- - (Boolean) connectable?(proxy)
- - (Object) get(count = 1)
-
- (Main) initialize(proxies, bad_proxies)
constructor
A new instance of Main.
- - (Object) load_list_from_array(proxies) private
- - (Object) load_list_from_file(proxies) private
Constructor Details
- (Main) initialize(proxies, bad_proxies)
Returns a new instance of Main
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# 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) end end |
Instance Attribute Details
- (Object) bad_list (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 |
- (Object) bad_list_file (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 |
- (Object) list (readonly)
Returns the value of attribute list
3 4 5 |
# File 'lib/proxy_manager/main.rb', line 3 def list @list end |
- (Object) list_file (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
- (Boolean) connectable?(proxy)
22 23 24 25 |
# File 'lib/proxy_manager/main.rb', line 22 def connectable?(proxy) proxy = proxy.chomp.split(':') if proxy.is_a? String Net::Ping::TCP.new(proxy[0], proxy[1].to_i).ping end |
- (Object) get(count = 1)
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 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 |
# File 'lib/proxy_manager/main.rb', line 27 def get(count = 1) raise 'List is empty' if @list.empty? items = [] @list.each_with_index do |proxy, key| @list.delete_at(key) if connectable? proxy @list << proxy if count == 1 items = proxy break else items << proxy break if items.size == count end else @bad_list << proxy end end 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 |
- (Object) load_list_from_array(proxies) (private)
81 82 83 |
# File 'lib/proxy_manager/main.rb', line 81 def load_list_from_array(proxies) @list = proxies.map { |arg| [arg.split(':')[0], arg.split(':')[1].to_i] } end |
- (Object) load_list_from_file(proxies) (private)
85 86 87 88 89 90 91 92 |
# File 'lib/proxy_manager/main.rb', line 85 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 |