Skip to content

Commit 476bd63

Browse files
author
Kirill Platonov
committed
refactor get method
1 parent bde0ce4 commit 476bd63

File tree

1 file changed

+31
-51
lines changed

1 file changed

+31
-51
lines changed

lib/proxy_manager/proxy.rb

+31-51
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,45 @@ def initialize(proxies)
1414
end
1515

1616
def get(count = 1)
17-
raise 'List is empty' if @list.empty?
17+
get_proxy(count)
18+
end
1819

19-
items = []
20-
new_list = @list.clone
20+
def get!(count = 1)
21+
get_proxy(count, true)
22+
end
2123

22-
@list.each_with_index do |proxy, key|
23-
new_list.shift
24-
new_list << proxy
25-
26-
if count == 1
27-
items = proxy
28-
break
29-
else
30-
items << proxy
31-
break if items.size == count
32-
end
24+
def self.connectable?(proxy)
25+
proxy = proxy.chomp.split(':') if proxy.is_a? String
26+
ip, port = proxy
27+
connection = Net::HTTP.new("http://google.com", nil, ip, port)
28+
connection.open_timeout = 3
29+
connection.read_timeout = 3
30+
31+
connection.start do |http|
32+
return true if http.get('/')
3333
end
3434

35-
@list = new_list
35+
false
36+
rescue Exception => e
37+
false
38+
end
3639

37-
raise 'There are no available proxy' if items.empty?
40+
private
3841

39-
save_to_file(@list_file, @list) if @list_file
42+
def load_list_from_array(proxies)
43+
@list = proxies.map { |arg| [arg.split(':')[0], arg.split(':')[1].to_i] }
44+
end
4045

41-
items
46+
def load_from_file(file)
47+
result = []
48+
IO.readlines(file).each do |line|
49+
ip, port = line.chomp.split(':')
50+
result << [ip, port.to_i] if ip.is_a?(String) && port.is_a?(String)
51+
end
52+
result
4253
end
4354

44-
def get!(count = 1)
55+
def get_proxy(count, check_connection = false)
4556
raise 'List is empty' if @list.empty?
4657

4758
items = []
@@ -50,7 +61,7 @@ def get!(count = 1)
5061
@list.each_with_index do |proxy, key|
5162
new_list.shift
5263

53-
if self.class.connectable? proxy
64+
if !check_connection || self.class.connectable?(proxy)
5465
new_list << proxy
5566

5667
if count == 1
@@ -72,37 +83,6 @@ def get!(count = 1)
7283
items
7384
end
7485

75-
def self.connectable?(proxy)
76-
proxy = proxy.chomp.split(':') if proxy.is_a? String
77-
ip, port = proxy
78-
connection = Net::HTTP.new("http://google.com", nil, ip, port)
79-
connection.open_timeout = 3
80-
connection.read_timeout = 3
81-
82-
connection.start do |http|
83-
return true if http.get('/')
84-
end
85-
86-
false
87-
rescue Exception => e
88-
false
89-
end
90-
91-
private
92-
93-
def load_list_from_array(proxies)
94-
@list = proxies.map { |arg| [arg.split(':')[0], arg.split(':')[1].to_i] }
95-
end
96-
97-
def load_from_file(file)
98-
result = []
99-
IO.readlines(file).each do |line|
100-
ip, port = line.chomp.split(':')
101-
result << [ip, port.to_i] if ip.is_a?(String) && port.is_a?(String)
102-
end
103-
result
104-
end
105-
10686
def save_to_file(file, list)
10787
source = ''
10888
list.each { |p| source << "#{p[0]}:#{p[1]}\n" }

0 commit comments

Comments
 (0)